blob: fbb47138790848fe9cf02fc635cb25e5820a1017 [file] [log] [blame]
Guido van Rossum11af5a41998-03-20 17:34:26 +00001/* FreezeDLLMain.cpp
2
3This is a DLLMain suitable for frozen applications/DLLs on
4a Windows platform.
5
6The general problem is that many Python extension modules may define
7DLL main functions, but when statically linked together to form
8a frozen application, this DLLMain symbol exists multiple times.
9
10The 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)
14
15* The frozen .EXE/.DLL links against this module, which provides
16 the single DllMain.
17
18* This DllMain attempts to locate and call the DllMain for each
19 of the extension modules.
20
21* This code also has hooks to "simulate" DllMain when used from
22 a frozen .EXE.
23
24At this stage, there is a static table of "possibly embedded modules".
25This should change to something better, but it will work OK for now.
26
27Note that this scheme does not handle dependencies in the order
28of DllMain calls - except it does call pywintypes first :-)
29
30As an example of how an extension module with a DllMain should be
31changed, here is a snippet from the pythoncom extension module.
32
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
41
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
45
46***************************************************************************/
47#include "windows.h"
48
49static char *possibleModules[] = {
50 "pywintypes",
51 "pythoncom",
52 "win32ui",
53 NULL,
54};
55
56BOOL CallModuleDllMain(char *modName, DWORD dwReason);
57
58
59/*
60 Called by a frozen .EXE only, so that built-in extension
61 modules are initialized correctly
62*/
Thomas Wouters78890102000-07-22 19:25:51 +000063void PyWinFreeze_ExeInit(void)
Guido van Rossum11af5a41998-03-20 17:34:26 +000064{
65 char **modName;
66 for (modName = possibleModules;*modName;*modName++) {
Mark Hammond4779a0a2000-10-05 22:10:33 +000067/* printf("Initialising '%s'\n", *modName); */
Guido van Rossum11af5a41998-03-20 17:34:26 +000068 CallModuleDllMain(*modName, DLL_PROCESS_ATTACH);
69 }
70}
71
72/*
73 Called by a frozen .EXE only, so that built-in extension
74 modules are cleaned up
75*/
Thomas Wouters78890102000-07-22 19:25:51 +000076void PyWinFreeze_ExeTerm(void)
Guido van Rossum11af5a41998-03-20 17:34:26 +000077{
78 // Must go backwards
79 char **modName;
80 for (modName = possibleModules+(sizeof(possibleModules) / sizeof(char *))-2;
81 modName >= possibleModules;
82 *modName--) {
Mark Hammond4779a0a2000-10-05 22:10:33 +000083/* printf("Terminating '%s'\n", *modName);*/
Guido van Rossum11af5a41998-03-20 17:34:26 +000084 CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
85 }
86}
87
88BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
89{
90 BOOL ret = TRUE;
91 switch (dwReason) {
92 case DLL_PROCESS_ATTACH:
93 {
94 char **modName;
95 for (modName = possibleModules;*modName;*modName++) {
96 BOOL ok = CallModuleDllMain(*modName, dwReason);
97 if (!ok)
98 ret = FALSE;
99 }
100 break;
101 }
102 case DLL_PROCESS_DETACH:
103 {
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;
111 }
112 }
113 return ret;
114}
115
116BOOL CallModuleDllMain(char *modName, DWORD dwReason)
117{
118 BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
119
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
129 */
130 return TRUE;
131 }
132 return (*pfndllmain)(hmod, dwReason, NULL);
133}
134