I am having problems importing my C ++ functions. If I declare them as C functions, I can import them successfully. When explicitly loading, if any of the functions is missing, extern, as a decoration for C, I get the following exception:
First-chance exception at 0x00000000 in cpp.exe: 0xC0000005: Access violation.
DLL.h:
extern "C" __declspec(dllimport) int addC(int a, int b); __declspec(dllimport) int addCpp(int a, int b);
DLL.cpp:
main.cpp:
#include "..DLL/DLL.h" #include <stdio.h> #include <windows.h> int main() { int a = 2; int b = 1; typedef int (*PFNaddC)(int,int); typedef int (*PFNaddCpp)(int,int); HMODULE hDLL = LoadLibrary(TEXT("../Debug/DLL.dll")); if (hDLL != NULL) { PFNaddC pfnAddC = (PFNaddC)GetProcAddress(hDLL, "addC"); PFNaddCpp pfnAddCpp = (PFNaddCpp)GetProcAddress(hDLL, "addCpp"); printf("a=%d, b=%d\n", a,b); printf("pfnAddC: %d\n", pfnAddC(a,b)); printf("pfnAddCpp: %d\n", pfnAddCpp(a,b)); //EXCEPTION ON THIS LINE } getchar(); return 0; }
How to import C ++ functions for dynamic loading? I found that the following code works with implicit loading, referencing * .lib, but I would like to know about dynamic loading.
Thanks to everyone in advance.
Update: bindump / exports
1 00011109 ?addCpp@ @ YAHHH@Z = @ILT+260( ?addCpp@ @ YAHHH@Z ) 2 00011136 addC = @ILT+305(_addC)
Decision
- Create the transformation structure as found here
Take a look at the file exports and copies the c ++ mangle naming convention.
PFNaddCpp pfnAddCpp = (PFNaddCpp) GetProcAddress (hDLL, "? AddCpp @@ YAHHH @Z");
user295190
source share