Repository navigation
Expand file tree
/
Copy pathcomserver.c
More file actions
153 lines (141 loc) · 4.08 KB
/
Copy pathcomserver.c
File metadata and controls
153 lines (141 loc) · 4.08 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
#include <stdio.h>
#include <Windows.h>
#include <Objbase.h>
#include <Python.h>
#define DLLEXPORT __declspec(dllexport)
PyObject *py_wmod = NULL;
INT py_ini = 1;
#define FUNC1(name) \
if (! py_wmod) {return E_FAIL;} \
PyGILState_STATE state = PyGILState_Ensure(); \
PyObject *py_func = PyObject_GetAttrString(py_wmod, name); \
if (! (py_func )) { \
PyErr_Clear(); \
PyGILState_Release(state); \
return E_FAIL; \
} \
long res = E_FAIL;
#define FUNC_ \
if (py_res) { \
res = PyLong_AsLong(py_res); \
if (PyErr_Occurred()) { \
PyErr_Clear(); \
res = E_FAIL; \
} \
Py_DECREF(py_res); \
}
#define FUNC2 \
FUNC_ \
} else { \
PyErr_Clear(); \
} \
Py_DECREF(py_func); \
#define FUNC3 \
PyGILState_Release(state); \
return res;
#define FUNC4(name) \
FUNC1(name) \
PyObject *py_res = PyObject_CallNoArgs(py_func); \
FUNC_ \
Py_DECREF(py_func); \
FUNC3
HRESULT DLLEXPORT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) {
FUNC1("DllGetClassObject")
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);
FUNC2
Py_XDECREF(py_rclsid);
Py_XDECREF(py_riid);
Py_XDECREF(py_ppv);
FUNC3
}
HRESULT DLLEXPORT WINAPI DllCanUnloadNow(void) {
FUNC4("DllCanUnloadNow")
}
HRESULT DLLEXPORT WINAPI DllRegisterServer() {
FUNC4("DllRegisterServer")
}
HRESULT DLLEXPORT WINAPI DllUnregisterServer() {
FUNC4("DllUnregisterServer")
}
HRESULT DLLEXPORT WINAPI DllInstall(BOOL bInstall, PCWSTR pszCmdLine) {
FUNC1("DllInstall")
PyObject *py_bInstall = PyLong_FromInt32(bInstall);
PyObject *py_pszCmdLine = PyLong_FromVoidPtr((void*) pszCmdLine);
if (py_bInstall && py_pszCmdLine) {
PyObject *py_res = PyObject_CallFunctionObjArgs(py_func, py_bInstall, py_pszCmdLine, NULL);
FUNC2
Py_XDECREF(py_bInstall);
Py_XDECREF(py_pszCmdLine);
FUNC3
}
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);
if (! py_ini) {
PyThreadState_Clear(PyThreadState_Get());
PyThreadState_DeleteCurrent();
}
break;
case DLL_PROCESS_DETACH:
if (! lpvReserved) {
PyGILState_STATE state = PyGILState_Ensure();
Py_XDECREF(py_wmod);
py_wmod = NULL;
if (py_ini) {
PyGILState_Release(state);
} else {
py_ini = 1;
Py_FinalizeEx();
}
}
break;
}
return TRUE;
}