3 This is a DLLMain suitable for frozen applications/DLLs on
6 The general problem is that many Python extension modules may define
7 DLL main functions, but when statically linked together to form
8 a frozen application, this DLLMain symbol exists multiple times.
11 * Each module checks for a frozen build, and if so, defines its DLLMain
12 function as "__declspec(dllexport) DllMain%module%"
13 (eg, DllMainpythoncom, or DllMainpywintypes)
15 * The frozen .EXE/.DLL links against this module, which provides
18 * This DllMain attempts to locate and call the DllMain for each
19 of the extension modules.
21 * This code also has hooks to "simulate" DllMain when used from
24 At this stage, there is a static table of "possibly embedded modules".
25 This should change to something better, but it will work OK for now.
27 Note that this scheme does not handle dependencies in the order
28 of DllMain calls - except it does call pywintypes first :-)
30 As an example of how an extension module with a DllMain should be
31 changed, here is a snippet from the pythoncom extension module.
33 // end of example code from pythoncom's DllMain.cpp
35 #define DLLMAIN DllMain
38 #define DLLMAIN DllMainpythoncom
39 #define DLLMAIN_DECL __declspec(dllexport)
42 extern "C" DLLMAIN_DECL
43 BOOL WINAPI DLLMAIN(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
44 // end of example code from pythoncom's DllMain.cpp
46 ***************************************************************************/
49 static char *possibleModules
[] = {
56 BOOL
CallModuleDllMain(char *modName
, DWORD dwReason
);
60 Called by a frozen .EXE only, so that built-in extension
61 modules are initialized correctly
63 void PyWinFreeze_ExeInit(void)
66 for (modName
= possibleModules
;*modName
;*modName
++) {
67 /* printf("Initialising '%s'\n", *modName); */
68 CallModuleDllMain(*modName
, DLL_PROCESS_ATTACH
);
73 Called by a frozen .EXE only, so that built-in extension
74 modules are cleaned up
76 void PyWinFreeze_ExeTerm(void)
80 for (modName
= possibleModules
+(sizeof(possibleModules
) / sizeof(char *))-2;
81 modName
>= possibleModules
;
83 /* printf("Terminating '%s'\n", *modName);*/
84 CallModuleDllMain(*modName
, DLL_PROCESS_DETACH
);
88 BOOL WINAPI
DllMain(HINSTANCE hInstance
, DWORD dwReason
, LPVOID lpReserved
)
92 case DLL_PROCESS_ATTACH
:
95 for (modName
= possibleModules
;*modName
;*modName
++) {
96 BOOL ok
= CallModuleDllMain(*modName
, dwReason
);
102 case DLL_PROCESS_DETACH
:
106 for (modName
= possibleModules
+(sizeof(possibleModules
) / sizeof(char *))-2;
107 modName
>= possibleModules
;
109 CallModuleDllMain(*modName
, DLL_PROCESS_DETACH
);
116 BOOL
CallModuleDllMain(char *modName
, DWORD dwReason
)
118 BOOL (WINAPI
* pfndllmain
)(HINSTANCE
, DWORD
, LPVOID
);
121 HMODULE hmod
= GetModuleHandle(NULL
);
122 strcpy(funcName
, "_DllMain");
123 strcat(funcName
, modName
);
124 strcat(funcName
, "@12"); // stdcall convention.
125 pfndllmain
= (BOOL (WINAPI
*)(HINSTANCE
, DWORD
, LPVOID
))GetProcAddress(hmod
, funcName
);
126 if (pfndllmain
==NULL
) {
127 /* No function by that name exported - then that module does
128 not appear in our frozen program - return OK
132 return (*pfndllmain
)(hmod
, dwReason
, NULL
);