Wintermute Engine Forum
Wintermute Engine => Technical forum => Topic started by: Pipec on July 25, 2009, 05:41:26 PM
-
Sorry for bad English. This is a computer translation from Russian language.
I want to load the DLL library. I use script:
external "kernel32.dll" int LoadLibraryA(string);
external "kernel32.dll" FreeLibrary(int);
external "kernel32.dll" int GetLastError();
var hDll = LoadLibraryA("magic.dll");
Game.Msg(GetLastError());
FreeLibrary(hDll);
Game.Msg(GetLastError());
But "LoadLibrary" return error 2 - ERROR_FILE_NOT_FOUND, or 126 - ERROR_MOD_NOT_FOUND.
I placed the dll in the folders: system32, WME folder, project folder, data folder. I used the absolute and relative path. But in all cases I get an error.
library, of course, working, I took her here astralax.ru (http://astralax.ru).
Working example of the developer (executable only, dll present) http://letitbit.net/download/0990.02a8ad5bcd8c88c3a093bfa61/demo.zip.html (http://letitbit.net/download/0990.02a8ad5bcd8c88c3a093bfa61/demo.zip.html)
Example with source code in C++ (without visualization, dll present) http://www.astralax.ru/hlam/LoadLibraryTest.rar (http://www.astralax.ru/hlam/LoadLibraryTest.rar)
Why can not load library? When I change magic.dll, for example, kernel32.dll, LoadLibrary returns 0 - ERROR_SUCCESS
Thank you for your help!
-
I'm afraid you can't rely on GetLastError in scripts, because it's pretty likely WME calls some API functions internally between the script lines. Instead use Game.Msg() to display the hDll variable. If it contains zero, LoadLibrary failed. If not, the library is loaded.
-
Thank you! This really works! ::rock
-
Then another question. DLL have function:
int Magic_OpenFile(
const char* file_name,
HM_FILE* hmFile
);
Сalling convention - cdecl
In the engine, I declare it so:
external "magic.dll" cdecl int Magic_OpenFile(string, int);
That is right?
Because when I is calling:
var PtcFile;
Magic_OpenFile("test.ptc", PtcFile);
WME crushing with application error.
-----------------------------------------------------------------
---------- wme 1.8.010 crash report: 27-07-2009, 12:55 ----------
-----------------------------------------------------------------
wme.exe caused a EXCEPTION_ACCESS_VIOLATION in module magic.dll at 001B:6A581334
EAX=00000000 EBX=00000001 ECX=6A5D1060 EDX=0000000A ESI=003E4880
EDI=0012FDA4 EBP=0012FD74 ESP=0012FD0C EIP=6A581334 FLG=00010213
CS=001B DS=0023 SS=0023 ES=0023 FS=003B GS=0000
Stack trace:
001B:6A581334 (0x6A5D1060 0x024E0FF0 0x00000000 0x00BE7AE0) magic.dll
001B:6A58B9F1 (0x024E0FF0 0x00000000 0xB677B752 0x00000010) magic.dll, Magic_OpenFile()+33 byte(s)
001B:00455D48 (0x00BE7D90 0x00BE7CE0 0x00BE7AE0 0x00000010) wme.exe
001B:00456A82 (0x00AB43B8 0xFFFFFFFF 0x00BE6F40 0x00000000) wme.exe
001B:00AB0101 (0x00454790 0x00455920 0x69726353 0x63207470) <UNKNOWN>
001B:00455FD0 (0x8B550C41 0x5614246C 0x5718718D 0x18247C8B) wme.exe
001B:8D53128B (0x00000000 0x00000000 0x00000000 0x00000000) <UNKNOWN>
-
Realized their mistake! ::)
"HM_FILE* hmFile" this is not a variable, this pointer. Solved the problem using MemBuffer
-
And yet the question ;)
The external function is called in a loop and returns a pointer at structure (membuffer object) or null. Null - a sign of the end of the loop. I use script:
external "magic.dll" cdecl membuffer Magic_GetNextParticle();
var doPart = true;
var struc = new MemBuffer(20);
while(doPart)
{
struc = Magic_GetNextParticle();
if (struc == null)
{
doPart = false;
}
else
{
... operations on the data from the structure ...
}
}
But the condition if (struc == null) is not fulfilled, when external function return null.
I used the condition if (struc.GetInt == null), but received an error, when external function return null
11:43: Cannot use Set/Get methods on an uninitialized memory buffer
Question: How in my case, to determine that the external function returns a null?
-
You could test null by comparing it with zero. But it won't work anyway. The DLL function apparently returns a pointer to some structure. It's not possible to access such structure from scripts.
-
There is no way to identify the null pointer ???
-
Yes there is, null pointer is zero.
if (struc == 0) ...
What I'm saying is that you will not be able to use a structure returned as a pointer from the scripts.