From: <th...@us...> - 2013-06-23 19:33:37
|
Revision: 732 http://sourceforge.net/p/py2exe/svn/732 Author: theller Date: 2013-06-23 19:33:35 +0000 (Sun, 23 Jun 2013) Log Message: ----------- Add some print statements for easier debugging. Fix some bugs. Modified Paths: -------------- trunk/py2exe/ChangeLog trunk/py2exe/source/MyLoadLibrary.c Modified: trunk/py2exe/ChangeLog =================================================================== --- trunk/py2exe/ChangeLog 2013-06-23 19:29:56 UTC (rev 731) +++ trunk/py2exe/ChangeLog 2013-06-23 19:33:35 UTC (rev 732) @@ -1,3 +1,8 @@ +2013-06-23 Thomas Heller <th...@ct...> + + * Updated the code with the current MemoryLoadLibrary module which + will also work on 64-bit windows. + 2013-05-24 Thomas Heller <th...@ct...> * properly implemented the activation context handling like it Modified: trunk/py2exe/source/MyLoadLibrary.c =================================================================== --- trunk/py2exe/source/MyLoadLibrary.c 2013-06-23 19:29:56 UTC (rev 731) +++ trunk/py2exe/source/MyLoadLibrary.c 2013-06-23 19:33:35 UTC (rev 732) @@ -10,6 +10,8 @@ #include "MemoryModule.h" #include "MyLoadLibrary.h" +/* #define VERBOSE /* enable to print debug output */ + /* Windows API: @@ -56,6 +58,28 @@ static LIST *libraries; +int level; + +static int dprintf(char *fmt, ...) +{ +#ifdef VERBOSE + va_list marker; + int i; + + va_start(marker, fmt); + for (i = 0; i < level; ++i) { + putchar(' '); + putchar(' '); + } + return vprintf(fmt, marker) + 2*level; +#else + return 0; +#endif +} + +#define PUSH() level++ +#define POP() level-- + /**************************************************************** * Search for a loaded MemoryModule in the linked list, either by name * or by module handle. @@ -65,16 +89,15 @@ LIST *lib = libraries; while (lib) { if (name && 0 == _stricmp(name, lib->name)) { -// printf("_FindMemoryModule(%s, %p) -> %s\n", name, module, lib->name); + dprintf("_FindMemoryModule(%s, %p) -> %s[%d]\n", name, module, lib->name, lib->refcount); return lib; } else if (module == lib->module) { -// printf("_FindMemoryModule(%s, %p) -> %s\n", name, module, lib->name); + dprintf("_FindMemoryModule(%s, %p) -> %s[%d]\n", name, module, lib->name, lib->refcount); return lib; } else { lib = lib->next; } } -// printf("_FindMemoryModule(%s, %p) -> NULL\n", name, module); return NULL; } @@ -90,7 +113,8 @@ entry->prev = NULL; entry->refcount = 1; libraries = entry; -// printf("_AddMemoryModule(%s, %p) -> %p\n", name, module, entry); + dprintf("_AddMemoryModule(%s, %p) -> %p[%d]\n", + name, module, entry, entry->refcount); return entry; } @@ -99,30 +123,26 @@ */ static FARPROC _GetProcAddress(HCUSTOMMODULE module, LPCSTR name, void *userdata) { - FARPROC res; - res = (FARPROC)GetProcAddress((HMODULE)module, name); - if (res == NULL) { - SetLastError(0); - return MemoryGetProcAddress(module, name); - } else - return res; + return MyGetProcAddress(module, name); } static void _FreeLibrary(HCUSTOMMODULE module, void *userdata) { - LIST *lib = _FindMemoryModule(NULL, module); - if (lib && --lib->refcount == 0) - MemoryFreeLibrary(module); - else - FreeLibrary((HMODULE) module); + MyFreeLibrary(module); } static HCUSTOMMODULE _LoadLibrary(LPCSTR filename, void *userdata) { HCUSTOMMODULE result; - LIST *lib = _FindMemoryModule(filename, NULL); + LIST *lib; + dprintf("_LoadLibrary(%s, %p)\n", filename, userdata); + PUSH(); + lib = _FindMemoryModule(filename, NULL); if (lib) { lib->refcount += 1; + POP(); + dprintf("_LoadLibrary(%s, %p) -> %s[%d]\n\n", + filename, userdata, lib->name, lib->refcount); return lib->module; } if (userdata) { @@ -133,14 +153,24 @@ _LoadLibrary, _GetProcAddress, _FreeLibrary, userdata); Py_DECREF(res); - lib = _AddMemoryModule(filename, result); - return lib->module; + if (result) { + lib = _AddMemoryModule(filename, result); + POP(); + dprintf("_LoadLibrary(%s, %p) -> %s[%d]\n\n", + filename, userdata, lib->name, lib->refcount); + return lib->module; + } else { + dprintf("_LoadLibrary(%s, %p) failed with error %d\n", + filename, userdata, GetLastError()); + } } else { PyErr_Clear(); } - PyErr_Clear(); } - return (HCUSTOMMODULE)LoadLibraryA(filename); + result = (HCUSTOMMODULE)LoadLibraryA(filename); + POP(); + dprintf("LoadLibraryA(%s) -> %p\n\n", filename, result); + return result; } /**************************************************************** @@ -155,23 +185,14 @@ return GetModuleHandle(name); } -HMODULE MyLoadLibrary(LPCSTR name, void *data, void *userdata) +HMODULE MyLoadLibrary(LPCSTR name, void *bytes, void *userdata) { - LIST *lib; -// printf("MyLoadLibrary(%s, %p, %p)\n", name, data, userdata); - lib = _FindMemoryModule(name, NULL); - if (lib) { - ++lib->refcount; - return lib->module; - } if (userdata) { HCUSTOMMODULE mod = _LoadLibrary(name, userdata); - if (mod) { - LIST *lib = _AddMemoryModule(name, mod); - return lib->module; - } - } else if (data) { - HCUSTOMMODULE mod = MemoryLoadLibraryEx(data, + if (mod) + return mod; + } else if (bytes) { + HCUSTOMMODULE mod = MemoryLoadLibraryEx(bytes, _LoadLibrary, _GetProcAddress, _FreeLibrary, @@ -188,10 +209,8 @@ { LIST *lib = _FindMemoryModule(NULL, module); if (lib) { - if (--lib->refcount == 0) { + if (--lib->refcount == 0) MemoryFreeLibrary(module); - /* remove lib entry from linked list */ - } return TRUE; } else return FreeLibrary(module); @@ -199,9 +218,15 @@ FARPROC MyGetProcAddress(HMODULE module, LPCSTR procname) { + FARPROC proc; LIST *lib = _FindMemoryModule(NULL, module); - if (lib) - return MemoryGetProcAddress(lib->module, procname); - else + if (lib) { +// dprintf("MyGetProcAddress(%p, %s)\n", module, procname); +// PUSH(); + proc = MemoryGetProcAddress(lib->module, procname); +// POP(); +// dprintf("MyGetProcAddress(%p, %s) -> %p\n", module, procname, proc); + return proc; + } else return GetProcAddress(module, procname); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |