0

I have got MyDll.dll and its function defined as below

void pascal Myfunction(BOOL);

when I'm trying to use the function in another project i am unable get the address of the function with GetProcAddress(). Here is my code:

void callMyDll()
{
 HINSTANCE hDll;

 hDll=LoadLibrary(_T("MyDll.dll");

 if(hDll!=NULL)
 {
  cout<<"\n DLL Loaded \n";
 }
 else
  cout<<"\n DLL Not loaded\n"

 typedef void (__stdcall *MyFunction)(bool)

 Myfunction mf1 = (MyFunction) GetProcAddress(hDll, "MyFunction");

 if (mf1!=NULL)
  cout<<"\n Function Loaded Successfully \n";
 else
  cout<<"\n Function not loaded \n";

 FreeLibrary(hDll);
}

I'm getting output as:

DLL Loaded
Function not loaded

But when I'm trying with known DLLs like glut32.dll and its functions it is working fine.

I think it may be problem with its function like

void pascal MyFunction(BOOL);

Can anybody help me in this regard?

unwind
  • 391,730
  • 64
  • 469
  • 606

4 Answers4

2

You need to use extern "C" to prevent name mangling and ensure the function is exported:

extern "C" __declspec(dllexport) void Myfunction(BOOL);

To view the exports from your DLL you can use dumpbin.exe utility that is shipped with Visual Studio:

dumpbin.exe /EXPORTS MyDll.dll

This will list the names of all exported symbols.

In addition to this do not have either of the following compiler switches specified:

Gz __stdcall calling convention: "Myfunction" would be exported as Myfunction@4
Gr __fastcall caling convention: "Myfunction" would be exported as @Myfunction@4

Note: I think last symbol is dependent on compiler version but is still not just "Myfunction".

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 1
    The function is declared as `pascal` which is the same as `__stdcall` on x86 and so `__declspec(dllexport)` will decorate no matter what the compiler options are. So it's going to be `_Myfunction@4` unless a .def file is used. – David Heffernan Jan 25 '12 at 11:27
  • @DavidHeffernan, thanks for pointing that out. I had omitted the `pascal` calling convention in my answer but maybe this is necessary for the OP. – hmjd Jan 25 '12 at 11:32
1

The DLL export process is subject to name mangling and decoration. The long obsolete 16 bit pascal calling convention is equivalent to stdcall on 32 bit platforms.

First of all you should use extern "C" to specify C linkage and disable name mangling.

However, your function will still be subject to name decoration. If you export it with __declspec(dllexport) then it will in fact be exported with the name _Myfunction@4. If you wish to export it by its true name then you need to use a .def file.

However, the possibility still remains that you did not export the function from the DLL at all. Use Dependency Walker to check whether it was exported, and if so by what name.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Why are you using the pascal calling-convention? Perhaps that alters the names of symbols, and if so you might need to take that into account.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

The symbol is going to be decorated, so it will never be called MyFunction, its more likely _MyFunction@4. you can quickly check this using something like dumpbin.

You can read up more on mangling here, if you want to avoid mangling, you need to use a def file to specify symbol names (or ordinals).

Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • It's unclear in the original post, but it appears that `extern "C"` is in use, which would prevent the function name from being mangled (provided the calling convention isn't being overridden) but I've asked for the exported function declaration just to be sure. – Adam Maras Jan 25 '12 at 13:16
  • @AdamMaras: it would still get a convention prefix at minimum on windows (see my links), `extern "C"` just says that it must to C style decoration, but he's using C anyways, its redundant – Necrolis Jan 25 '12 at 13:29
  • Thanks a lot i can able to load the functions.. that's great – srinivasporam Jan 25 '12 at 15:34
  • @srinivasporam: if its the right answer you should mark it as such (click the green tick next to the question) – Necrolis Jan 25 '12 at 17:00