Dll's
-
hey
Ich habe eine Anwendung geschrieben, die DLL's dynamisch laden soll. Sie lädt die DLL's, jedoch nicht die in ihr enthaltenen Funktionen:
#undef UNICODE #include <windows.h> int WINAPI DllMain(HINSTANCE handle, unsigned long reason, void* reserved) { return 1; } __declspec(dllexport) void test(void) { Beep(1000, 100); }Dll wurde ins system32 verzeichnis kopiert.
Hier meine "Lade"-Anwendung:
#undef UNICODE #include <windows.h> #include <iostream> using namespace std; typedef void (CALLBACK* DllFunction)(void); int main(void) { HMODULE dll; DllFunction initialize; dll = LoadLibrary("TempPointInterface.dll"); if(!dll) { cout << "kann dll nicht laden" << endl; } else { initialize = (DllFunction)(GetProcAddress(dll, "test")); if(!initialize) { cout << "kann Funktion aus dll nicht laden" << endl; } else { initialize(); } } FreeLibrary(dll); char a; cin >> a; return 0; }die zweite Fehlermeldung (kann Funktion aus dll nicht laden) wird merkwürdigerweise ausgegeben. Weiß jemand wieso?
mfg
-
wenn du die dll als c++ kompilierst, musst du deine exportfunktion wegen dem name-mangeling (oder so ähnlich heißt das) mit extern "C" deklarieren. Dann gehen zwar keine Überladungen, aber du kannst sie wenigstens laden:
extern "C" __declspec(dllexport) void test(); int DllMain(...) { ... } __declspec(dllexport) void test() { beep(); }
-
Benutze den Dependecy Walker um zu sehen, was wirklich exportiert wird.
Simon
-
Vielen Dank!!!
Und nochmals vielen dank für die schnelle Antwort, da muss ich heute wohl keine Überstunden machen^^mfg
-
Hmm vllt. öffnest du mal mit Dependency Walker deine DLL und stellst dann etwas fest. Was ist mit dem Namen deiner Funktion los?!
#include <windows.h> #include <iostream> typedef void (*fnFoo)(); int main() { // open dynamic linking library file handle ::HMODULE hDLL (::LoadLibraryA("TempPointInterface.dll")); // could open dll? if (hDLL == NULL) { std::cerr << "\"TempPointerInterface.dll\" could not be loaded!" << std::endl; return 1; } // try to load function fnFoo foo(reinterpret_cast<fnFoo>(::GetProcAddressA(hDLL, "foo"))); // function could be loaded? if (foo == NULL) { std::cerr << "Funktion \"foo\" konnte nicht gefunden werden!" << std::endl; return 1; } // call loaded function foo(); // free dynamic linking library ::FreeLibrary(hDLL); // await user input std::cin.get(); }#include <windows.h> #include <iostream> int WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return 1; } // #if TEMPPOINTER_EXPORTS #define TEMPPOINTER_API __declspec(dllexport) // #else // #define TEMPPOINTER_API __declspec(dllimport) // #endif extern "C" { TEMPPOINTER_API void foo() { std::clog << "foo() function called!" << std::endl; } }sollte es dir möglich machen, die Funktion korrekt zu laden
