Unable to load DLL

Feb 29, 2012 at 11:12pm
I made a recent post about not being able to load my C++ DLL in a C# application, now having tried to load it in C++ I found I can do so, but am unable to load Get the function pointers from it using GetProcAddress.

This post contains the DLL code which does the exporting of the functions

http://cplusplus.com/forum/general/63237/

The exports seem to be defined correctly as dumpbin shows the functions I exported

This is the code to load the DLL and use a function from it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
include <iostream>
#include <Windows.h>

typedef bool (__stdcall *func)();

int main()
{
    HINSTANCE hDLL;    // Handle to DLL
    func pFunc;    // Function pointer
    bool b;
 
    hDLL = LoadLibrary("VGSLib.dll");
    if (hDLL == NULL)
    {
        std::cout << "Cannot load DLL\n";
    } 
    else
    {
	pFunc = (func)GetProcAddress(hDLL,"Formatted");
	if (!pFunc)
        {
	    FreeLibrary(hDLL);
	    std::cout << "Cannot load DLL function\n";
	}
	else
	{
	    b = pFunc();
            std::cout << "Result was: " << b;
	} 
    }
}


The output of this is "Cannot load DLL function"
Clearly I'm doing something wrong but I don't think I understand just how DLLs work enough to know what.

Halp.
Last edited on Feb 29, 2012 at 11:13pm
Feb 29, 2012 at 11:52pm
Download depends from here.
http://www.dependencywalker.com/

Use it to open your DLL, you'll see all the exported functions,

I suspect your function name is mangled, but depends will tell all.
Mar 1, 2012 at 8:30am
Ah yes my function names are mangled, Formatted turned to"_Formatted@0". Thanks kbw, I thought that using extern "C" in the DLL header file stopped the function names from being mangled though, how can I stop it?

I have one more question too. In the DLL header file is a global (but not static) instance of the VGSLib class which is used by the exported functions, this has private members which need to be unique to each application that loads the DLL, I just wanted to make sure that all applications that use the DLL will not be sharing the instance of VGSLib will they?

Thankyou.
Mar 1, 2012 at 11:06am
extern "C" does cause C linkage. If yours doesn't work, can you post a sample so we can try to work out what's going on please.
Mar 1, 2012 at 12:43pm
@ OP: To answer your other question no, programs will not share instances of objects from a DLL with eachother like that.
Topic archived. No new replies allowed.