/
dllmain.c
290 lines (268 loc) · 8.81 KB
/
dllmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Bootloader for a DLL COM server.
* Copyright (C) 2005, Giovanni Bajo
* Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* In addition to the permissions in the GNU General Public License, the
* authors give you unlimited permission to link or embed the compiled
* version of this file into combinations with other programs, and to
* distribute those combinations without any restriction coming from the
* use of this file. (The General Public License restrictions do apply in
* other respects; for example, they cover modification of the file, and
* distribution when not linked into a combine executable.)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
// TODO leave only necessary header includes.
#include <windows.h>
#include <olectl.h>
#include <memory.h>
#include <string.h>
#include "pyi_global.h"
#include "pyi_utils.h"
#include "pyi_python.h"
#include "pyi_pythonlib.h"
typedef int (__stdcall *__PROC__DllCanUnloadNow) (void);
__PROC__DllCanUnloadNow Pyc_DllCanUnloadNow = NULL;
typedef HRESULT (__stdcall *__PROC__DllGetClassObject) (REFCLSID, REFIID, LPVOID *);
__PROC__DllGetClassObject Pyc_DllGetClassObject = NULL;
typedef int (__cdecl *__PROC__DllRegisterServerEx) (const char *);
__PROC__DllRegisterServerEx Pyc_DllRegisterServerEx = NULL;
typedef int (__cdecl *__PROC__DllUnregisterServerEx) (const char *);
__PROC__DllUnregisterServerEx Pyc_DllUnregisterServerEx = NULL;
typedef void (__cdecl *__PROC__PyCom_CoUninitialize) (void);
__PROC__PyCom_CoUninitialize PyCom_CoUninitialize = NULL;
HINSTANCE gPythoncom = 0;
char here[PATH_MAX + 1];
int LoadPythonCom(ARCHIVE_STATUS *status);
void releasePythonCom(void);
HINSTANCE gInstance;
PyThreadState *thisthread = NULL;
int launch(ARCHIVE_STATUS *status, char const * archivePath, char const * archiveName)
{
PyObject *obHandle;
int loadedNew = 0;
char pathnm[PATH_MAX];
VS("START");
strcpy(pathnm, archivePath);
strcat(pathnm, archiveName);
/* Set up paths */
if (pyi_arch_set_paths(status, archivePath, archiveName))
return -1;
VS("Got Paths");
/* Open the archive */
if (pyi_arch_open(status))
return -1;
VS("Opened Archive");
/* Load Python DLL */
if (pyi_pylib_attach(status, &loadedNew))
return -1;
if (loadedNew) {
/* Start Python with silly command line */
PI_PyEval_InitThreads();
if (pyi_pylib_start_python(status, 1, (char**)&pathnm))
return -1;
VS("Started new Python");
thisthread = PI_PyThreadState_Swap(NULL);
PI_PyThreadState_Swap(thisthread);
}
else {
VS("Attached to existing Python");
/* start a mew interp */
thisthread = PI_PyThreadState_Swap(NULL);
PI_PyThreadState_Swap(thisthread);
if (thisthread == NULL) {
thisthread = PI_Py_NewInterpreter();
VS("created thisthread");
}
else
VS("grabbed thisthread");
PI_PyRun_SimpleString("import sys;sys.argv=[]");
}
/* a signal to scripts */
PI_PyRun_SimpleString("import sys;sys.frozen='dll'\n");
VS("set sys.frozen");
/* Create a 'frozendllhandle' as a counterpart to
sys.dllhandle (which is the Pythonxx.dll handle)
*/
obHandle = PI_Py_BuildValue("i", gInstance);
PI_PySys_SetObject("frozendllhandle", obHandle);
Py_XDECREF(obHandle);
/* Import modules from archive - this is to bootstrap */
if (pyi_pylib_import_modules(status))
return -1;
VS("Imported Modules");
/* Install zlibs - now import hooks are in place */
if (pyi_pylib_install_zlibs(status))
return -1;
VS("Installed Zlibs");
/* Run scripts */
if (pyi_pylib_run_scripts(status))
return -1;
VS("All scripts run");
if (PI_PyErr_Occurred()) {
// PI_PyErr_Print();
//PI_PyErr_Clear();
VS("Some error occurred");
}
VS("PGL released");
// Abandon our thread state.
PI_PyEval_ReleaseThread(thisthread);
VS("OK.");
return 0;
}
void startUp()
{
ARCHIVE_STATUS *status_list[20];
char thisfile[PATH_MAX + 1];
char *p;
int len;
memset(status_list, 0, 20 * sizeof(ARCHIVE_STATUS *));
if (!GetModuleFileNameA(gInstance, thisfile, PATH_MAX)) {
FATALERROR("System error - unable to load!");
return;
}
// fill in here (directory of thisfile)
//GetModuleFileName returns an absolute path
strcpy(here, thisfile);
for (p=here+strlen(here); *p != '\\' && p >= here+2; --p);
*++p = '\0';
len = p - here;
//VS(here);
//VS(&thisfile[len]);
launch(status_list[SELF], here, &thisfile[len]);
LoadPythonCom(status_list[SELF]);
// Now Python is up and running (any scripts have run)
}
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if ( dwReason == DLL_PROCESS_ATTACH) {
VS("Attach from thread %x", GetCurrentThreadId());
gInstance = hInstance;
}
else if ( dwReason == DLL_PROCESS_DETACH ) {
VS("Process Detach");
//if (gPythoncom)
// releasePythonCom();
//pyi_pylib_finalize();
}
return TRUE;
}
int LoadPythonCom(ARCHIVE_STATUS *status)
{
char dllpath[PATH_MAX+1];
VS("Loading Pythoncom");
// see if pythoncom is already loaded
sprintf(dllpath, "pythoncom%02d.dll", pyi_arch_get_pyversion(status));
gPythoncom = GetModuleHandleA(dllpath);
if (gPythoncom == NULL) {
sprintf(dllpath, "%spythoncom%02d.dll", here, pyi_arch_get_pyversion(status));
//VS(dllpath);
gPythoncom = LoadLibraryExA( dllpath, // points to name of executable module
NULL, // HANDLE hFile, // reserved, must be NULL
LOAD_WITH_ALTERED_SEARCH_PATH // DWORD dwFlags // entry-point execution flag
);
}
if (!gPythoncom) {
VS("Pythoncom failed to load");
return -1;
}
// debugging
GetModuleFileNameA(gPythoncom, dllpath, PATH_MAX);
VS(dllpath);
Pyc_DllCanUnloadNow = (__PROC__DllCanUnloadNow)GetProcAddress(gPythoncom, "DllCanUnloadNow");
Pyc_DllGetClassObject = (__PROC__DllGetClassObject)GetProcAddress(gPythoncom, "DllGetClassObject");
// DllRegisterServerEx etc are mainly used for "scripts", so that regsvr32.exe can be run on
// a .py file, for example. They aren't really relevant here.
Pyc_DllRegisterServerEx = (__PROC__DllRegisterServerEx)GetProcAddress(gPythoncom, "DllRegisterServerEx");
Pyc_DllUnregisterServerEx = (__PROC__DllUnregisterServerEx)GetProcAddress(gPythoncom, "DllUnregisterServerEx");
PyCom_CoUninitialize = (__PROC__PyCom_CoUninitialize)GetProcAddress(gPythoncom, "PyCom_CoUninitialize");
if (Pyc_DllGetClassObject == NULL) {
VS("Couldn't get DllGetClassObject from pythoncom!");
return -1;
}
if (PyCom_CoUninitialize == NULL) {
VS("Couldn't get PyCom_CoUninitialize from pythoncom!");
return -1;
}
return 0;
}
void releasePythonCom(void)
{
if (gPythoncom) {
PyCom_CoUninitialize();
FreeLibrary(gPythoncom);
gPythoncom = 0;
}
}
//__declspec(dllexport) int __stdcall DllCanUnloadNow(void)
//__declspec(dllexport)
//STDAPI
HRESULT __stdcall DllCanUnloadNow(void)
{
HRESULT rc;
VS("DllCanUnloadNow from thread %x", GetCurrentThreadId());
if (gPythoncom == 0)
startUp();
rc = Pyc_DllCanUnloadNow();
VS("DllCanUnloadNow returns %x", rc);
//if (rc == S_OK)
// PyCom_CoUninitialize();
return rc;
}
//__declspec(dllexport) int __stdcall DllGetClassObject(void *rclsid, void *riid, void *ppv)
HRESULT __stdcall DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
HRESULT rc;
VS("DllGetClassObject from thread %x", GetCurrentThreadId());
if (gPythoncom == 0)
startUp();
rc = Pyc_DllGetClassObject(rclsid, riid, ppv);
VS("DllGetClassObject set %x and returned %x", *ppv, rc);
return rc;
}
__declspec(dllexport) int DllRegisterServerEx(LPCSTR fileName)
{
VS("DllRegisterServerEx from thread %x", GetCurrentThreadId());
if (gPythoncom == 0)
startUp();
return Pyc_DllRegisterServerEx(fileName);
}
__declspec(dllexport) int DllUnregisterServerEx(LPCSTR fileName)
{
if (gPythoncom == 0)
startUp();
return Pyc_DllUnregisterServerEx(fileName);
}
STDAPI DllRegisterServer()
{
int rc, pyrc;
if (gPythoncom == 0)
startUp();
PI_PyEval_AcquireThread(thisthread);
rc = callSimpleEntryPoint("DllRegisterServer", &pyrc);
PI_PyEval_ReleaseThread(thisthread);
return rc==0 ? pyrc : SELFREG_E_CLASS;
}
STDAPI DllUnregisterServer()
{
int rc, pyrc;
if (gPythoncom == 0)
startUp();
PI_PyEval_AcquireThread(thisthread);
rc = callSimpleEntryPoint("DllUnregisterServer", &pyrc);
PI_PyEval_ReleaseThread(thisthread);
return rc==0 ? pyrc : SELFREG_E_CLASS;
}