Hi everyone,
Ive been just learning how to make/use dll files and Ive come up with thiss error (through getLastError() func). The code which creates the dll is almost the same as the IDE generated, I just changed the function name and what the function does, this is the code:
header file
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern"C"
{
#endif
int DLL_EXPORT getDungeonsNumber();
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
#include "main.h"
// a sample exported function
int DLL_EXPORT getDungeonsNumber()
{
return 5;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
this makes the MW.dll file (CodeBlocks IDE used here).
Then the main program code is made in Qt IDE:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w; //creates the main window of the app
w.show();
return a.exec();
}
//heres the cĀ“tor of the main window where Im trying to load the dll and there comes the error
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
DungeonsNumber(0),
getDungeonsNumber(NULL)
/*getDungeonsNumber is pointer to func of type FUNCPTgetDungeonsNumber which I have defined in another header included in the mainwindow.h
its defined like this typedef int (*FUNCPTgetDungeonsNumber)(); */
{
ui->setupUi(this);
SetDllDirectory(NULL);
hinstDLL = LoadLibrary((LPCWSTR)"MW.dll");
if(hinstDLL!=0)
getDungeonsNumber = (FUNCPTgetDungeonsNumber)GetProcAddress(hinstDLL, "getDungeonsNumber");
else{
Error::cannotLoadDLL(); //just my function to handle the error
}
if(getDungeonsNumber==NULL)
Error::nullFunction(); //just another my function to handle the error
DungeonsNumber = getDungeonsNumber();
QMessageBox::information(this, "a", QString::number(DungeonsNumber));
FreeLibrary(hinstDLL);
}
.
.
.
staticvoid Error::cannotLoadDLL(){
int e = GetLastError();
QString str = "Your DLLs seems to be broken... ";
str += "getLastError() returned value: ";
str += QString::number(e);
QMessageBox::critical(NULL, "Error", str);
}
the function Error::cannotLoadDLL() is called and the messagebox sais the getLastError returned value 126.
I have the dll file in the axact place as the executable. Also i have tried to load some profi made dlls like msvcr100d.dll and get the same error. This is very frustating, hope someone can help :D
The cast here is absolutely wrong. It will cause LoadLibrary to always fail. Either use LoadLibraryW directly and L"" prefix for literal strings if you must pass a Unicode string or use _T() macro and include <tchar.h>.
oh yees, god I hate microsoft and their redefined variable names.
in the tutorial there was only hinstDLL = LoadLibrary("MW.dll"); and it worked but I get the error that I canoot pass char* to LoadLibrary that there must be LPCWSTR so I just used the cast and I forget the L, thank you so much :D