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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shark351Commented:
Excuse the typo.
First option looks like this

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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.