Charger une dll avec MinGW
Je compile avec MinGW un programme sensé créer et charger une DLL. Apparemment la création et le chargement se passe bien, mais GetProcAddress() ne trouve pas la fonction.
Code:
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
| #include <windows.h>
using namespace std ;
typedef int (*MYPROC)(int);
static HMODULE hdll = NULL;
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {
// création du fichier c qui sera compilé en dll:
string dllstr("__declspec( dllexport ) int MyFuncInc(int a) { return ++a; }" );
ofstream out("dll.c");
out << dllstr << endl ;
out.close();
// Compilation pour créer la dll:
system( "g++ -shared -o dll.dll dll.c" );
// Chargement de la DLL:
hdll = LoadLibrary("dll.dll");
if (hdll == NULL) { cout << "LoadLibrary failed" << endl; return 1; }
// chargement de la fonction:
MYPROC MyFuncInc = (MYPROC)GetProcAddress( hdll, "MyFuncInc" );
if (MyFuncInc == NULL) { cout << "GetProcAddress failed" << endl; return 1; }
cout << MyFuncInc(41) << endl ;
return 0;
} |