I’m trying to compile DLL from console, without using any IDE and faced with next error.
I wrote this code:
test_dll.cpp
#include <windows.h>
#define DLL_EI __declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){
return 1;
}
extern "C" int DLL_EI func (int a, int b){
return a + b;
}
Then compiled with command icl /LD test_dll.cpp. And I’m trying to call this func from another program:
prog.cpp
int main(){
HMODULE hLib;
hLib = LoadLibrary("test_dll.dll");
double (*pFunction)(int a, int b);
(FARPROC &)pFunction = GetProcAddress(hLib, "Function");
printf("begin\n");
Rss = pFunction(1, 2);
}
Compile it with icl prog.cpp. Then I run it, and it fails with standard window “Program isn’t working”. Maybe there is a segmentation fault error.
What am I doing wrong?
Check that both
LoadLibrary()andGetProcAddress()succeed, in this case they definitely will not as the exported function is calledfunc, not"Function"as specified in the argument toGetProcAddress()meaning the function pointer will beNULLwhen the attempt to invoke it is made.The signature of the function pointer also does not match the signature of the exported function, the exported function returns an
intand the function pointer is expecting adouble.For example: