Discussion:
Problems with DLLs (using Visual Studio C++ 6.0)
(too old to reply)
Dave
2007-01-24 03:39:53 UTC
Permalink
Hi everyone. Okay, I'm trying to dynamically link some dlls and I'm
having some problems. I originally had a project that seems to work,
but for the life of me I can't repeat the results.

I used app wizard to create a simple Win32 Dynamic Linked library. the
code is as follows:


#include "stdafx.h"
#include "stdio.h"

int i = 0;

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

void _stdcall InitDll(){
i = 1;
}



I build that and get 'TestDll.dll'

The program I use to open this dll looks like this:


#include "stdafx.h"
#include <windows.h>


typedef void(__stdcall *InitDllOperation)();


int main(int argc, char* argv[])
{

InitDllOperation initDll;

HINSTANCE dllLib = LoadLibrary("TestDll.dll");

if( dllLib == NULL )
{
printf( "Unable to load library\n");
return 1;
}
else
{
printf( "loaded library successfully\n");
}

initDll =
(InitDllOperation)GetProcAddress((HINSTANCE)dllLib,"InitDll");

if( initDll == NULL )
{
printf( "initDll failed to load\n");
return 1;
}
else
{
printf( "init Success\n");
}

return 0;
}


Every time the library loads successfully, but the 'initDll' object is
ALWAYS Null. The return from GetProcAddress seems to always come up
null. Any suggestions?

Thanks
Alf P. Steinbach
2007-01-24 04:03:13 UTC
Permalink
Post by Dave
initDll =
(InitDllOperation)GetProcAddress((HINSTANCE)dllLib,"InitDll");
Every time the library loads successfully, but the 'initDll' object is
ALWAYS Null. The return from GetProcAddress seems to always come up
null. Any suggestions?
You're probably up against name mangling. Use dumpbin to check the
actual exported names. Declare the function as 'extern "C"' to force a
simple name mangling scheme. If you use __declspec dllexport then name
mangling becomes less of an issue because it introduces a number of name
variations and apparently does some magic, but I'm not sure about how it
works out with GetProcAddress. An alternative, for when you really want
to charge of the exported names, is to use a module definition file (a
[.def]-file) when building the DLL.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Loading...