Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/PC/frozen_dllmain.c
12 views
1
/* FreezeDLLMain.cpp
2
3
This is a DLLMain suitable for frozen applications/DLLs on
4
a Windows platform.
5
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.
9
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)
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
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.
26
27
Note that this scheme does not handle dependencies in the order
28
of DllMain calls - except it does call pywintypes first :-)
29
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.
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
49
static char *possibleModules[] = {
50
"pywintypes",
51
"pythoncom",
52
"win32ui",
53
NULL,
54
};
55
56
BOOL 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
*/
63
void PyWinFreeze_ExeInit(void)
64
{
65
char **modName;
66
for (modName = possibleModules;*modName;*modName++) {
67
/* printf("Initialising '%s'\n", *modName); */
68
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
*/
76
void PyWinFreeze_ExeTerm(void)
77
{
78
// Must go backwards
79
char **modName;
80
for (modName = possibleModules+Py_ARRAY_LENGTH(possibleModules)-2;
81
modName >= possibleModules;
82
*modName--) {
83
/* printf("Terminating '%s'\n", *modName);*/
84
CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
85
}
86
}
87
88
BOOL 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+Py_ARRAY_LENGTH(possibleModules)-2;
107
modName >= possibleModules;
108
*modName--)
109
CallModuleDllMain(*modName, DLL_PROCESS_DETACH);
110
break;
111
}
112
}
113
return ret;
114
}
115
116
BOOL CallModuleDllMain(char *modName, DWORD dwReason)
117
{
118
BOOL (WINAPI * pfndllmain)(HINSTANCE, DWORD, LPVOID);
119
120
char funcName[255];
121
HMODULE hmod = GetModuleHandleW(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
135
136