Issue #7701: Fix crash in binascii.b2a_uu() in debug mode when given a
[python.git] / PC / frozen_dllmain.c
blobfbb47138790848fe9cf02fc635cb25e5820a1017
1 /* FreezeDLLMain.cpp
3 This is a DLLMain suitable for frozen applications/DLLs on
4 a Windows platform.
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.
10 The solution is:
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
16 the single DllMain.
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
22 a frozen .EXE.
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
34 #ifndef BUILD_FREEZE
35 #define DLLMAIN DllMain
36 #define DLLMAIN_DECL
37 #else
38 #define DLLMAIN DllMainpythoncom
39 #define DLLMAIN_DECL __declspec(dllexport)
40 #endif
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 ***************************************************************************/
47 #include "windows.h"
49 static char *possibleModules[] = {
50 "pywintypes",
51 "pythoncom",
52 "win32ui",
53 NULL,
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)
65 char **modName;
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)
78 // Must go backwards
79 char **modName;
80 for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
81 modName >= possibleModules;
82 *modName--) {
83 /* printf("Terminating '%s'\n", *modName);*/
84 CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
88 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
90 BOOL ret = TRUE;
91 switch (dwReason) {
92 case DLL_PROCESS_ATTACH:
94 char **modName;
95 for (modName = possibleModules;*modName;*modName++) {
96 BOOL ok = CallModuleDllMain(*modName, dwReason);
97 if (!ok)
98 ret = FALSE;
100 break;
102 case DLL_PROCESS_DETACH:
104 // Must go backwards
105 char **modName;
106 for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
107 modName >= possibleModules;
108 *modName--)
109 CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
110 break;
113 return ret;
116 BOOL CallModuleDllMain(char *modName, DWORD dwReason)
118 BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
120 char funcName[255];
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
130 return TRUE;
132 return (*pfndllmain)(hmod, dwReason, NULL);