| 1 | n/a | /* FreezeDLLMain.cpp |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This is a DLLMain suitable for frozen applications/DLLs on |
|---|
| 4 | n/a | a Windows platform. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | The general problem is that many Python extension modules may define |
|---|
| 7 | n/a | DLL main functions, but when statically linked together to form |
|---|
| 8 | n/a | a frozen application, this DLLMain symbol exists multiple times. |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | The solution is: |
|---|
| 11 | n/a | * Each module checks for a frozen build, and if so, defines its DLLMain |
|---|
| 12 | n/a | function as "__declspec(dllexport) DllMain%module%" |
|---|
| 13 | n/a | (eg, DllMainpythoncom, or DllMainpywintypes) |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | * The frozen .EXE/.DLL links against this module, which provides |
|---|
| 16 | n/a | the single DllMain. |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | * This DllMain attempts to locate and call the DllMain for each |
|---|
| 19 | n/a | of the extension modules. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | * This code also has hooks to "simulate" DllMain when used from |
|---|
| 22 | n/a | a frozen .EXE. |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | At this stage, there is a static table of "possibly embedded modules". |
|---|
| 25 | n/a | This should change to something better, but it will work OK for now. |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | Note that this scheme does not handle dependencies in the order |
|---|
| 28 | n/a | of DllMain calls - except it does call pywintypes first :-) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | As an example of how an extension module with a DllMain should be |
|---|
| 31 | n/a | changed, here is a snippet from the pythoncom extension module. |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | // end of example code from pythoncom's DllMain.cpp |
|---|
| 34 | n/a | #ifndef BUILD_FREEZE |
|---|
| 35 | n/a | #define DLLMAIN DllMain |
|---|
| 36 | n/a | #define DLLMAIN_DECL |
|---|
| 37 | n/a | #else |
|---|
| 38 | n/a | #define DLLMAIN DllMainpythoncom |
|---|
| 39 | n/a | #define DLLMAIN_DECL __declspec(dllexport) |
|---|
| 40 | n/a | #endif |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | extern "C" DLLMAIN_DECL |
|---|
| 43 | n/a | BOOL WINAPI DLLMAIN(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) |
|---|
| 44 | n/a | // end of example code from pythoncom's DllMain.cpp |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | ***************************************************************************/ |
|---|
| 47 | n/a | #include "windows.h" |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | static char *possibleModules[] = { |
|---|
| 50 | n/a | "pywintypes", |
|---|
| 51 | n/a | "pythoncom", |
|---|
| 52 | n/a | "win32ui", |
|---|
| 53 | n/a | NULL, |
|---|
| 54 | n/a | }; |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | BOOL CallModuleDllMain(char *modName, DWORD dwReason); |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | /* |
|---|
| 60 | n/a | Called by a frozen .EXE only, so that built-in extension |
|---|
| 61 | n/a | modules are initialized correctly |
|---|
| 62 | n/a | */ |
|---|
| 63 | n/a | void PyWinFreeze_ExeInit(void) |
|---|
| 64 | n/a | { |
|---|
| 65 | n/a | char **modName; |
|---|
| 66 | n/a | for (modName = possibleModules;*modName;*modName++) { |
|---|
| 67 | n/a | /* printf("Initialising '%s'\n", *modName); */ |
|---|
| 68 | n/a | CallModuleDllMain(*modName, DLL_PROCESS_ATTACH); |
|---|
| 69 | n/a | } |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | /* |
|---|
| 73 | n/a | Called by a frozen .EXE only, so that built-in extension |
|---|
| 74 | n/a | modules are cleaned up |
|---|
| 75 | n/a | */ |
|---|
| 76 | n/a | void PyWinFreeze_ExeTerm(void) |
|---|
| 77 | n/a | { |
|---|
| 78 | n/a | // Must go backwards |
|---|
| 79 | n/a | char **modName; |
|---|
| 80 | n/a | for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2; |
|---|
| 81 | n/a | modName >= possibleModules; |
|---|
| 82 | n/a | *modName--) { |
|---|
| 83 | n/a | /* printf("Terminating '%s'\n", *modName);*/ |
|---|
| 84 | n/a | CallModuleDllMain(*modName, DLL_PROCESS_DETACH); |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | } |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) |
|---|
| 89 | n/a | { |
|---|
| 90 | n/a | BOOL ret = TRUE; |
|---|
| 91 | n/a | switch (dwReason) { |
|---|
| 92 | n/a | case DLL_PROCESS_ATTACH: |
|---|
| 93 | n/a | { |
|---|
| 94 | n/a | char **modName; |
|---|
| 95 | n/a | for (modName = possibleModules;*modName;*modName++) { |
|---|
| 96 | n/a | BOOL ok = CallModuleDllMain(*modName, dwReason); |
|---|
| 97 | n/a | if (!ok) |
|---|
| 98 | n/a | ret = FALSE; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | break; |
|---|
| 101 | n/a | } |
|---|
| 102 | n/a | case DLL_PROCESS_DETACH: |
|---|
| 103 | n/a | { |
|---|
| 104 | n/a | // Must go backwards |
|---|
| 105 | n/a | char **modName; |
|---|
| 106 | n/a | for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2; |
|---|
| 107 | n/a | modName >= possibleModules; |
|---|
| 108 | n/a | *modName--) |
|---|
| 109 | n/a | CallModuleDllMain(*modName, DLL_PROCESS_DETACH); |
|---|
| 110 | n/a | break; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | } |
|---|
| 113 | n/a | return ret; |
|---|
| 114 | n/a | } |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | BOOL CallModuleDllMain(char *modName, DWORD dwReason) |
|---|
| 117 | n/a | { |
|---|
| 118 | n/a | BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID); |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | char funcName[255]; |
|---|
| 121 | n/a | HMODULE hmod = GetModuleHandleW(NULL); |
|---|
| 122 | n/a | strcpy(funcName, "_DllMain"); |
|---|
| 123 | n/a | strcat(funcName, modName); |
|---|
| 124 | n/a | strcat(funcName, "@12"); // stdcall convention. |
|---|
| 125 | n/a | pfndllmain = (BOOL (WINAPI *)(HINSTANCE, DWORD, LPVOID))GetProcAddress(hmod, funcName); |
|---|
| 126 | n/a | if (pfndllmain==NULL) { |
|---|
| 127 | n/a | /* No function by that name exported - then that module does |
|---|
| 128 | n/a | not appear in our frozen program - return OK |
|---|
| 129 | n/a | */ |
|---|
| 130 | n/a | return TRUE; |
|---|
| 131 | n/a | } |
|---|
| 132 | n/a | return (*pfndllmain)(hmod, dwReason, NULL); |
|---|
| 133 | n/a | } |
|---|
| 134 | n/a | |
|---|