blob: 0156c5008bc93a905665ea2651a2fd8dc224edf9 [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012 function as "__declspec(dllexport) DllMain%module%"
Guido van Rossum11af5a41998-03-20 17:34:26 +000013 (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[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 "pywintypes",
51 "pythoncom",
52 "win32ui",
53 NULL,
Guido van Rossum11af5a41998-03-20 17:34:26 +000054};
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 char **modName;
66 for (modName = possibleModules;*modName;*modName++) {
67/* printf("Initialising '%s'\n", *modName); */
68 CallModuleDllMain(*modName, DLL_PROCESS_ATTACH);
69 }
Guido van Rossum11af5a41998-03-20 17:34:26 +000070}
71
72/*
73 Called by a frozen .EXE only, so that built-in extension
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 modules are cleaned up
Guido van Rossum11af5a41998-03-20 17:34:26 +000075*/
Thomas Wouters78890102000-07-22 19:25:51 +000076void PyWinFreeze_ExeTerm(void)
Guido van Rossum11af5a41998-03-20 17:34:26 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 // Must go backwards
79 char **modName;
Victor Stinner63941882011-09-29 00:42:28 +020080 for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 modName >= possibleModules;
82 *modName--) {
83/* printf("Terminating '%s'\n", *modName);*/
84 CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
85 }
Guido van Rossum11af5a41998-03-20 17:34:26 +000086}
87
88BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
89{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 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;
Victor Stinner63941882011-09-29 00:42:28 +0200106 for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 modName >= possibleModules;
108 *modName--)
109 CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
110 break;
111 }
112 }
113 return ret;
Guido van Rossum11af5a41998-03-20 17:34:26 +0000114}
115
116BOOL CallModuleDllMain(char *modName, DWORD dwReason)
117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
Guido van Rossum11af5a41998-03-20 17:34:26 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 char funcName[255];
Martin v. Löwis50590f12012-01-14 17:54:09 +0100121 HMODULE hmod = GetModuleHandleW(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 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);
Guido van Rossum11af5a41998-03-20 17:34:26 +0000133}
134