PDA

Click to See Complete Forum and Search --> : dll files in microsoft visual c++ 6


KnIgHt
Mar 18th, 2001, 03:49 AM
ok....i'm trying to build a dll file in c++. I'm just a beginner here and i want to make functions in c++, incorporate them into a .dll file and then use those functions in vb via a .dll file.

heres my c++ dll code:

test.h

// test.h : main header file for the TEST DLL
//

#if !defined(AFX_TEST_H__5456A6C6_1B56_11D5_9D42_E0ACEAB76910__INCLUDED_)
#define AFX_TEST_H__5456A6C6_1B56_11D5_9D42_E0ACEAB76910__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////
// CTestApp
// See test.cpp for the implementation of this class
//

class CTestApp : public CWinApp
{
public:
CTestApp();
char* hi();

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestApp)
//}}AFX_VIRTUAL

//{{AFX_MSG(CTestApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TEST_H__5456A6C6_1B56_11D5_9D42_E0ACEAB76910__INCLUDED_)

----------------------------------------------------------
test.cpp

// test.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CTestApp

BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
//{{AFX_MSG_MAP(CTestApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestApp construction

CTestApp::CTestApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

char* CTestApp::hi()
{
return "Hello";
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestApp object

CTestApp theApp;
----------------------------------

now all this code compiles fine and creates the dll file but when i want to use it in visual basic I create a module and add the following line of code to it.

Public Declare Function Test Lib "test.dll" Alias "hi" () As String

then in my main form i have

Private Sub Form_Load()
MsgBox Test
End Sub


when i run the program it gives me this error:

Run-time error '453':
Can't find DLL entry point hi in test.dll


could someone help me out here!!!

parksie
Mar 18th, 2001, 05:32 AM
The main problem here is that you've created an MFC DLL, not a proper DLL. You need to select project type "Win32 DLL". This won't create any source files for you, allowing you to create your own functions. I've attached an example.

There are two things to watch out for:
1. All exported functions must have the calling convention "__stdcall", as seen in the source file.
2. The exported functions must be named in the .DEF file