-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomserver.c
More file actions
186 lines (182 loc) · 5.36 KB
/
comserver.c
File metadata and controls
186 lines (182 loc) · 5.36 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
#include <stdio.h>
#include <Windows.h>
#include <Objbase.h>
#include <Python.h>
#define DLLEXPORT __declspec(dllexport)
PyObject *py_wmod = NULL;
INT py_ini = 1;
HRESULT DLLEXPORT WINAPI DllGetClassObject(const REFCLSID rclsid, const REFIID riid, LPVOID *ppv) {
LPOLESTR pclsid;
if (! py_wmod || StringFromCLSID(rclsid, &pclsid)) {return E_FAIL;}
if (wcslen(pclsid) != 38) {
CoTaskMemFree(pclsid);
return E_FAIL;
}
WCHAR rpath[60] = L"CLSID\\ \\InprocServer32";
wcsncpy(rpath + 6, pclsid, 38);
CoTaskMemFree(pclsid);
WCHAR *mpath = NULL;
DWORD msize = 0;
if (! RegGetValueW(HKEY_CLASSES_ROOT, rpath, L"PyModule", RRF_RT_REG_SZ, NULL, NULL, &msize) && msize) {
if (mpath = (WCHAR*) malloc(msize)) {
if (RegGetValueW(HKEY_CLASSES_ROOT, rpath, L"PyModule", RRF_RT_REG_SZ, NULL, mpath, &msize)) {
free(mpath);
return E_FAIL;
}
} else {return E_FAIL;}
}
PyGILState_STATE state = PyGILState_Ensure();
PyObject *py_mod = NULL;
if (mpath) {
WCHAR *mname = wcsrchr(mpath, L'\\');
if (mname) {
*(mname++) = 0;
} else {
mname = mpath;
}
WCHAR *mext = wcsrchr(mname, L'.');
if (mext && ! _wcsicmp(mext, L".py")) {
*mext = 0;
}
PyObject *py_mname = PyUnicode_FromWideChar(mname, -1);
if (py_mname && ! (py_mod = PyImport_GetModule(py_mname)) && ! PyErr_Occurred()) {
if (mname != mpath) {
PyObject *py_path = PySys_GetObject("path");
PyObject *py_mpath = PyUnicode_FromWideChar(mpath, -1);
if (py_path && py_mpath) {
if (! PySequence_Contains(py_path, py_mpath)) {
PyList_Append(py_path, py_mpath);
}
py_mod = PyImport_Import(py_mname);
}
Py_XDECREF(py_mpath);
} else {
py_mod = PyImport_Import(py_mname);
}
}
free(mpath);
if (! py_mod) {
PyErr_Clear();
PyGILState_Release(state);
return E_FAIL;
}
}
PyObject *py_func = PyObject_GetAttrString(py_mod ? py_mod : py_wmod, "DllGetClassObject");
Py_XDECREF(py_mod);
if (! (py_func )) {
PyErr_Clear();
PyGILState_Release(state);
return E_FAIL;
}
long res = E_FAIL;
PyObject *py_rclsid = PyLong_FromVoidPtr((void*) rclsid);
PyObject *py_riid = PyLong_FromVoidPtr((void*) riid);
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
if (py_rclsid && py_riid && py_ppv) {
PyObject *py_res = PyObject_CallFunctionObjArgs(py_func, py_rclsid, py_riid, py_ppv, NULL);
if (py_res) {
res = PyLong_AsLong(py_res);
if (PyErr_Occurred()) {
PyErr_Clear();
res = E_FAIL;
}
Py_DECREF(py_res);
}
} else {
PyErr_Clear();
}
Py_DECREF(py_func);
Py_XDECREF(py_rclsid);
Py_XDECREF(py_riid);
Py_XDECREF(py_ppv);
PyGILState_Release(state);
return res;
}
HRESULT DLLEXPORT WINAPI DllCanUnloadNow(void) {
if (! py_wmod) {return E_FAIL;}
PyGILState_STATE state = PyGILState_Ensure();
PyObject *py_func = PyObject_GetAttrString(py_wmod, "DllCanUnloadNow");
if (! py_func) {
PyErr_Clear();
PyGILState_Release(state);
return E_FAIL;
}
long res = E_FAIL;
PyObject *py_res = PyObject_CallNoArgs(py_func);
if (py_res) {
res = PyLong_AsLong(py_res);
if (PyErr_Occurred()) {
PyErr_Clear();
res = E_FAIL;
}
Py_DECREF(py_res);
}
Py_DECREF(py_func);
PyGILState_Release(state);
return res;
}
BOOL WINAPI DllMain(const HINSTANCE hinstDLL, const DWORD fdwReason, const LPVOID lpvReserved ) {
switch(fdwReason) {
case DLL_PROCESS_ATTACH:
py_ini = Py_IsInitialized();
if (! py_ini) {
Py_InitializeEx(0);
if (PyErr_Occurred()) {
PyErr_Clear();
return FALSE;
}
}
WCHAR *dllpath = NULL;
DWORD alen = MAX_PATH;
DWORD rlen;
do {
if (! (dllpath = (WCHAR*) malloc(sizeof(WCHAR) * alen))) {return FALSE;}
if (! (rlen = GetModuleFileNameW(hinstDLL, dllpath, alen))) {
free(dllpath);
return FALSE;
}
if (rlen < alen) {break;}
free(dllpath);
dllpath = NULL;
alen *= 2;
} while (alen <= 66560);
if (! dllpath) {return FALSE;}
WCHAR *e = wcsrchr(dllpath, L'\\');
if (e) {*e = 0;}
PyGILState_STATE state = PyGILState_Ensure();
PyObject *py_path = PySys_GetObject("path");
PyObject *py_mpath = PyUnicode_FromWideChar(dllpath, -1);
free(dllpath);
if (! py_path || ! py_mpath) {
Py_XDECREF(py_mpath);
PyErr_Clear();
PyGILState_Release(state);
return FALSE;
}
PyList_Append(py_path, py_mpath);
Py_XDECREF(py_mpath);
if (! (py_wmod = PyImport_ImportModule("wic"))) {
PyErr_Clear();
PyGILState_Release(state);
return FALSE;
}
PyGILState_Release(state);
break;
case DLL_PROCESS_DETACH:
if (! lpvReserved) {
PyGILState_STATE state = PyGILState_Ensure();
Py_XDECREF(py_wmod);
py_wmod = NULL;
PyGILState_Release(state);
if (! py_ini) {
py_ini = 1;
Py_FinalizeEx();
if (PyErr_Occurred()) {
PyErr_Clear();
}
}
}
break;
}
return TRUE;
}