[r737]: trunk / py2exe / source / run_ctypes_dll.c Maximize Restore History

Download this file

run_ctypes_dll.c    251 lines (225 with data), 7.4 kB

  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
/*
* Copyright (c) 2000, 2001 Thomas Heller
* Copyright (c) 2003 Mark Hammond
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>
#include <stdio.h>
#include <olectl.h>
#include <assert.h>
#include "Python-dynload.h"
#include "MyLoadLibrary.h"
#include "actctx.h"
// Function pointers we load from _ctypes.pyd
typedef int (__stdcall *__PROC__DllCanUnloadNow) (void);
typedef HRESULT (__stdcall *__PROC__DllGetClassObject) (REFCLSID, REFIID, LPVOID *);
CRITICAL_SECTION csInit; // protecting our init code
__PROC__DllCanUnloadNow Pyc_DllCanUnloadNow = NULL;
__PROC__DllGetClassObject Pyc_DllGetClassObject = NULL;
extern BOOL _LoadPythonDLL(HMODULE hmod);
extern BOOL _LocateScript(HMODULE hmod);
extern void init_memimporter(void);
void SystemError(int error, char *msg)
{
char Buffer[1024];
int n;
if (error) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,
NULL
);
strncpy(Buffer, lpMsgBuf, sizeof(Buffer));
LocalFree(lpMsgBuf);
} else
Buffer[0] = '\0';
n = lstrlen(Buffer);
_snprintf(Buffer+n, sizeof(Buffer)-n, msg);
MessageBox(GetFocus(), Buffer, NULL, MB_OK | MB_ICONSTOP);
}
BOOL have_init = FALSE;
HANDLE g_ctypes = 0;
HMODULE gInstance = 0;
extern int init_with_instance(HMODULE, char *);
extern void fini();
extern int run_script(void);
int load_ctypes(void)
{
char dll_path[_MAX_PATH+_MAX_FNAME+1];
// shouldn't do this twice
assert(g_ctypes == NULL);
#ifdef _DEBUG
strcpy(dll_path, "_ctypes_d.pyd");
#else
strcpy(dll_path, "_ctypes.pyd");
#endif
g_ctypes = MyGetModuleHandle(dll_path);
/*
if (g_ctypes == NULL) {
// not already loaded - try and load from the current dir
char *temp;
GetModuleFileNameA(gInstance, dll_path, sizeof(dll_path));
temp = dll_path + strlen(dll_path);
while (temp>dll_path && *temp != '\\')
temp--;
// and printf directly in the buffer.
// temp points to '\\filename.ext'!
#ifdef _DEBUG
g_ctypes = MyGetModuleHandle("_ctypes_d.pyd");
#else
g_ctypes = MyGetModuleHandle("_ctypes.pyd");
#endif
g_ctypes = LoadLibraryEx(dll_path, // 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 (g_ctypes == NULL) {
OutputDebugString("GetModuleHandle _ctypes?.pyd failed");
// give up in disgust
return -1;
}
Pyc_DllCanUnloadNow = (__PROC__DllCanUnloadNow)MyGetProcAddress(g_ctypes, "DllCanUnloadNow");
Pyc_DllGetClassObject = (__PROC__DllGetClassObject)MyGetProcAddress(g_ctypes, "DllGetClassObject");
assert(Pyc_DllGetClassObject);
return 0;
}
int check_init()
{
if (!have_init) {
EnterCriticalSection(&csInit);
// Check the flag again - another thread may have beat us to it!
if (!have_init) {
PyObject *frozen;
/* If Python had previously been initialized, we must use
PyGILState_Ensure normally. If Python has not been initialized,
we must leave the thread state unlocked, so other threads can
call in.
*/
PyGILState_STATE restore_state = PyGILState_UNLOCKED;
if (!Py_IsInitialized) {
// Python function pointers are yet to be loaded
// - force that now. See above for why we *must*
// know about the initialized state of Python so
// we can setup the thread-state correctly
// before calling init_with_instance(); This is
// wasteful, but fixing it would involve a
// restructure of init_with_instance()
_LocateScript(gInstance);
_LoadPythonDLL(gInstance);
}
if (Py_IsInitialized && Py_IsInitialized()) {
restore_state = PyGILState_Ensure();
}
// a little DLL magic. Set sys.frozen='dll'
init_with_instance(gInstance, "dll");
init_memimporter();
frozen = PyLong_FromVoidPtr(gInstance);
if (frozen) {
PySys_SetObject("frozendllhandle", frozen);
Py_DECREF(frozen);
}
// Now run the generic script - this always returns in a DLL.
run_script();
have_init = TRUE;
if (g_ctypes == NULL)
load_ctypes();
// Reset the thread-state, so any thread can call in
PyGILState_Release(restore_state);
}
LeaveCriticalSection(&csInit);
}
return g_ctypes != NULL;
}
// *****************************************************************
// All the public entry points needed for COM, Windows, and anyone
// else who wants their piece of the action.
// *****************************************************************
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if ( dwReason == DLL_PROCESS_ATTACH) {
gInstance = hInstance;
InitializeCriticalSection(&csInit);
_MyLoadActCtxPointers();
if (pfnGetCurrentActCtx && pfnAddRefActCtx)
if ((*pfnGetCurrentActCtx)(&PyWin_DLLhActivationContext))
if (!(*pfnAddRefActCtx)(PyWin_DLLhActivationContext))
OutputDebugString("Python failed to load the default activation context\n");
}
else if ( dwReason == DLL_PROCESS_DETACH ) {
gInstance = 0;
DeleteCriticalSection(&csInit);
if (pfnReleaseActCtx)
(*pfnReleaseActCtx)(PyWin_DLLhActivationContext);
// not much else safe to do here
}
return TRUE;
}
HRESULT __stdcall DllCanUnloadNow(void)
{
HRESULT rc;
check_init();
assert(Pyc_DllCanUnloadNow);
if (!Pyc_DllCanUnloadNow) return E_UNEXPECTED;
rc = Pyc_DllCanUnloadNow();
return rc;
}
//__declspec(dllexport) int __stdcall DllGetClassObject(void *rclsid, void *riid, void *ppv)
HRESULT __stdcall DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
HRESULT rc;
check_init();
assert(Pyc_DllGetClassObject);
if (!Pyc_DllGetClassObject) return E_UNEXPECTED;
rc = Pyc_DllGetClassObject(rclsid, riid, ppv);
return rc;
}
STDAPI DllRegisterServer()
{
int rc=0;
PyGILState_STATE state;
check_init();
state = PyGILState_Ensure();
rc = PyRun_SimpleString("DllRegisterServer()\n");
if (rc != 0)
PyErr_Print();
PyGILState_Release(state);
return rc==0 ? 0 : SELFREG_E_CLASS;
}
STDAPI DllUnregisterServer()
{
int rc=0;
PyGILState_STATE state;
check_init();
state = PyGILState_Ensure();
rc = PyRun_SimpleString("DllUnregisterServer()\n");
if (rc != 0)
PyErr_Print();
PyGILState_Release(state);
return rc==0 ? 0 : SELFREG_E_CLASS;
}