Funktion aus einer DLL lesen
-
Hallo
Ich möchte aus einer DLL eine Funktion starten. In meiner DLL sieht es so aus:
// plugin.h #define DLL_API __declspec(dllexport) typedef struct { CString strName; CString strDescription; CString strCopyright; CString strWebsite; CString strText; int iLetters; int iLines; } PLX_Module; extern "C" DLL_ void apiMain(PLX_Module *Module);
// plugin.cpp THOPETO_API void apiMain(PLX_Module *Module) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); PLX_Module Modul; Modul.strName = _T("sfsdf"); Modul.strDescription = _T("sdfsdfsdf"); Modul.strCopyright = _T("sdfdsfdsf"); Modul.strWebsite = _T("sdfdsfdsfdsfsf"); Modul.strText = _T("Das ist der source"); *Module = Modul; }
Laden Tu ich die DLL so
// In meiner anwendung typedef struct { CString strName; CString strDescription; CString strCopyright; CString strWebsite; CString strText; int iLetters; int iLines; } PLX_Module; BOOL CDlgModule::OnInitModule() { SetCurrentDirectory(GetAppPath() + "/Module/"); CFileFind finder; CString strWildCard = _T("*.dll"); BOOL bWorking = finder.FindFile(strWildCard); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots() || finder.IsDirectory()) continue; HMODULE hm = LoadLibrary(finder.GetFilePath()); if(!hm) { AfxMessageBox("Fehler: Kann folgendes Modul nicht korrekt laden: " + finder.GetFilePath()); } else { m_dwa.Add((DWORD)hm); PFUNC pFunc = (PFUNC)GetProcAddress((HINSTANCE)m_dwa.GetAt(finder.FindNextFile()), _T("apiMain")); if(pFunc != NULL) { PLX_Module Modul; pFunc(&Modul); // hier kommt dann ein fehler } } } return TRUE; }
sobald ich dann die anwendung starte, findet er zwar die dll und startet sie ohne weiters, aber bei pFunc(&Module) kommt dann ein fehler. Führ ich dir Funktion in meiner anwendung falsch aus oder liegt es an der dll???
-
das gehört auch noch in meine anwendung:
typedef void (*PFUNC)(PLX_Module*);
-
sollte das hier:
extern "C" DLL_ void apiMain(PLX_Module *Module);
nicht so sein:
extern "C" DLL_API void apiMain(PLX_Module *Module);
?
Die Zeile würde ich einfacher machen:PFUNC pFunc = (PFUNC)GetProcAddress((HINSTANCE)m_dwa.GetAt(finder.FindNextFile()), _T("apiMain"));
und zwar:
PFUNC pFunc = (PFUNC)GetProcAddress(hm, _T("apiMain"));