[ctypes-users] Different behavior in ctypes vs. straight C
Brought to you by:
theller
|
From: J. K. <jky...@ec...> - 2007-06-22 03:25:11
|
Hello all.
I have been attempting to use ctypes to use the functions in a DLL with
moderate success. Although the DLL doesn't have function names, I can use
ordinals to access functions and I have a definition of the particular
ordinals that I need. I had initially started to write a python extension
to access this library but whether I call the functions from an extension
or from python by way of ctypes, I am not able to duplicate behavior that
I can get from a straight C program.
To ensure that the functions in this DLL behave as I expect, I wrote a
console app that calls a couple of them. The calls I make go like this:
int main(int argc, char* argv[])
{
char* archive_filename = "archive.mpq";
char* filename = "(listfile)";
HANDLE hMPQ = NULL;
HANDLE hFile = NULL;
int ok;
ok = SFILE(OpenArchive)(archive_filename, 0, 0, &hMPQ);
ok = SFILE(OpenFileEx)(hMPQ, filename, 0, &hFile);
SFILE(CloseFile)(hFile);
SFILE(CloseArchive)(hMPQ);
return 0;
}
Now, I use some error checking and the like but basically the first
function opens an archive named by archive_filename (a C string) and
stores a HANDLE in hMPQ. The second function takes that HANDLE and opens
a file (identified by filename) within that archive. The HANDLE for this
file is stored in hFile. Using this program, I get a valid HANDLE in both
cases and the return value indicates success.
Here is my equivalent in python:
import ctypes as C
dll = C.WinDLL('lib.dll')
OpenArchive = dll[266]
OpenFileEx = dll[268]
hMPQ = c_void_p()
hFile = c_void_p()
archive_file = r'archive.mpq'
filename = r'(listfile)'
OpenArchive(archive_file, 0, 0, byref(hMPQ))
OpenFileEx(hMPQ, filename, 0, byref(hFile))
Now in this case the first call succeeds (returns 1) and hMPQ is set
however the second call fails (returns 0) and hFile doesn't get set. This
behavior is the same when I implemented the above in a python extension
using C and the exact same code as above in a single function callable
from python.
So my question, ctype-users, is this: Am I just missing something about
how this normally works (like not using the right types) or is this just a
problem with the dll that I am trying to access and it just won't work
from python?
My thanks for any thoughts or ideas on this one.
Regards,
Jeff
|