c++dlldllimportgetprocaddress

Creating a DLL in C++ to import C++ DLL in VS 2005


I'm trying to link C++ DLL into a new C++ DLL which i will create,

I've followed the below tutorial step by step and many others but something wrong the "GetProcAddress" function returns NULL "http://www.dreamincode.net/forums/topic/118076-dlls-explicit-linking/"

This is the Prototype of the function i try to call from the DLL :

int RemoveAllDataFile( unsigned int id );

the function returns with 1 so the DLL is loaded successfully.

typedef int (*funcRemoveAllDataFile) (int);

int load_dll_ARbnet(int x)
{
    /* Retrieve DLL handle.*/
    HINSTANCE hDLL = LoadLibrary("ArbNet2Remote.dll");   
    if (hDLL == NULL)
    {
        return 0;
    }    
    else
    {
    }
    /*Get the function address*/
    funcRemoveAllDataFile RemoveAllDataFile = (funcRemoveAllDataFile)GetProcAddress(hDLL, "RemoveAllDataFile");
    if (RemoveAllDataFile)
    {
        return 2;
    }
    else
    {
        return 1;
    }

}


Solution

  • Look at the DLL's actual exports, such as with a reporting tool like TDUMP or similar. The function you are looking for is not being exported as "RemoveAllDataFile" like you are expecting. It is actually being exported like "_RemoveAllDataFile" instead, or even something like "_RemoveAllDataFile@4".

    If you are compiling the original DLL, and want the function exported as "RemoveAllDataFile", you will have to wrap the declaration of the exported function with extern "C" to remove any C++ name mangling from the exported name. Depending on your C++ compiler, you may also need to use a .def file to remove the leading underscore that is imposed by the __cdecl calling convention. When using C linkage, some C++ compilers export __cdecl functions with the leading underscore (such as Borland) and some do not (such as Microsoft).

    But if you are not recompiling the original DLL, then you have no choice but to look at the DLL's exports and change your GetProcAddress() call to use the proper name that is actually being exported.