Dll loaded but can't find the function.... HELP!!

This is a function inside my dll...

extern "C" _declspec( dllexport) void WINAPI PromptMsg()
{
#ifdef _AFXDLL
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
TRY
{
AfxMessageBox("dsada");
}
END_TRY
}

when i try to call it from another application with this...

typedef bool (CALLBACK* LPFNDLLFUNC1)();
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;
hDLL = LoadLibrary("SecBond.dll");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"PromptMsg");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return false;
}
else
{
// call the function
lpfnDllFunc1();

}
}

the dll is loaded successfully but the PromptMsg function is not called, the handle of the function lpfnDllFunc1 is null.

Anyone please help.. thanks in advance.

I suspect there is something wrong with my coding.
Is it possible to send me 2 simple projects which will guide me in writing dll?
1 project which creates a dll (MFC AppWizard(dll))
and another project which calls the function in the dll.

Many thanks......



cyuancAsked:
Who is Participating?
 
shark351Connect With a Mentor Commented:
Using the Dependancy Utility you can get the decorated symbol which in your case is "_PromptMsg@0". So you can get the pointer to your function like this.

lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,")PromptMsg@0");

Alternatively (although discouraged) you can get the address by using the ordinal number itself. Look up the ordinal number for the function using dependancy viewer. In this case PromptMsg Ordinal = 1.

Use this method like so...

lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,MAKEINTRESOURCE(1));
0
 
jclanzCommented:
Hello,
Simply add a .def file in your project like this.

-------------
; SecBond.def : Declares the module parameters for the DLL.

LIBRARY      "SecBond"
DESCRIPTION  'SecBond dll'

EXPORTS
    PromptMsg         @001

-------------
this force the PromptMsg to be correctly exported.

You can also look into the DLL to see how the function is exported. You will probably found somthing like "_PromptMsg".

use "C:\Program Files\Microsoft Visual Studio\Common\Tools\DEPENDS.EXE" to look the DLL.

regards, jean-claude.
0
 
koskiaCommented:
yep,
jclanz is right
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
arikkaCommented:
declaring functions as dllexport eliminates the need for a module-definition (.DEF) file, at least with respect to the specification of exported functions.  
So it theoretically should work without DEF file as well but I usually prefer to work with DEF file.
Generalyy speaking jclanz is right...
0
 
jclanzCommented:
Hello,
If we are in this discussion, can somebody tell me why without .def file the function is always exported with an underscore and with the .def file not?

---- header ------
#define SECBOND_API __declspec(dllexport)

SECBOND_API void WINAPI PromptMsg(void);
0
 
gambisticsCommented:
You can also do it this way:
_declspec(dllimport) void WINAPI PromptMsg();

You can use this forward declaration in your 'client' application and linking statically to the .lib file generating while compiling the dll.
0
 
shark351Commented:
Excuse the typo.
First option looks like this

lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"_PromptMsg@0");

0
All Courses

From novice to tech pro — start learning today.