Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r732]: trunk / py2exe / source / MyLoadLibrary.c Maximize Restore History

Download this file

MyLoadLibrary.c    233 lines (204 with data), 5.7 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
#ifdef STANDALONE
# include <Python.h>
# include "Python-version.h"
#else
# include "Python-dynload.h"
# include <stdio.h>
#endif
#include <windows.h>
#include "MemoryModule.h"
#include "MyLoadLibrary.h"
/* #define VERBOSE /* enable to print debug output */
/*
Windows API:
============
HMODULE LoadLibraryA(LPCSTR)
HMODULE GetModuleHandleA(LPCSTR)
BOOL FreeLibrary(HMODULE)
FARPROC GetProcAddress(HMODULE, LPCSTR)
MemoryModule API:
=================
HMEMORYMODULE MemoryLoadLibrary(void *)
void MemoryFreeLibrary(HMEMORYMODULE)
FARPROC MemoryGetProcAddress(HMEMORYMODULE, LPCSTR)
HMEMORYMODULE MemoryLoadLibrayEx(void *,
load_func, getproc_func, free_func, userdata)
(there are also some resource functions which are not used here...)
General API in this file:
=========================
HMODULE MyLoadLibrary(LPCSTR, void *, userdata)
HMODULE MyGetModuleHandle(LPCSTR)
BOOL MyFreeLibrary(HMODULE)
FARPROC MyGetProcAddress(HMODULE, LPCSTR)
*/
/****************************************************************
* A linked list of loaded MemoryModules.
*/
typedef struct tagLIST {
HCUSTOMMODULE module;
LPCSTR name;
struct tagLIST *next;
struct tagLIST *prev;
int refcount;
} LIST;
static LIST *libraries;
int level;
static int dprintf(char *fmt, ...)
{
#ifdef VERBOSE
va_list marker;
int i;
va_start(marker, fmt);
for (i = 0; i < level; ++i) {
putchar(' ');
putchar(' ');
}
return vprintf(fmt, marker) + 2*level;
#else
return 0;
#endif
}
#define PUSH() level++
#define POP() level--
/****************************************************************
* Search for a loaded MemoryModule in the linked list, either by name
* or by module handle.
*/
static LIST *_FindMemoryModule(LPCSTR name, HMODULE module)
{
LIST *lib = libraries;
while (lib) {
if (name && 0 == _stricmp(name, lib->name)) {
dprintf("_FindMemoryModule(%s, %p) -> %s[%d]\n", name, module, lib->name, lib->refcount);
return lib;
} else if (module == lib->module) {
dprintf("_FindMemoryModule(%s, %p) -> %s[%d]\n", name, module, lib->name, lib->refcount);
return lib;
} else {
lib = lib->next;
}
}
return NULL;
}
/****************************************************************
* Insert a MemoryModule into the linked list of loaded modules
*/
static LIST *_AddMemoryModule(LPCSTR name, HCUSTOMMODULE module)
{
LIST *entry = (LIST *)malloc(sizeof(LIST));
entry->name = _strdup(name);
entry->module = module;
entry->next = libraries;
entry->prev = NULL;
entry->refcount = 1;
libraries = entry;
dprintf("_AddMemoryModule(%s, %p) -> %p[%d]\n",
name, module, entry, entry->refcount);
return entry;
}
/****************************************************************
* Helper functions for MemoryLoadLibraryEx
*/
static FARPROC _GetProcAddress(HCUSTOMMODULE module, LPCSTR name, void *userdata)
{
return MyGetProcAddress(module, name);
}
static void _FreeLibrary(HCUSTOMMODULE module, void *userdata)
{
MyFreeLibrary(module);
}
static HCUSTOMMODULE _LoadLibrary(LPCSTR filename, void *userdata)
{
HCUSTOMMODULE result;
LIST *lib;
dprintf("_LoadLibrary(%s, %p)\n", filename, userdata);
PUSH();
lib = _FindMemoryModule(filename, NULL);
if (lib) {
lib->refcount += 1;
POP();
dprintf("_LoadLibrary(%s, %p) -> %s[%d]\n\n",
filename, userdata, lib->name, lib->refcount);
return lib->module;
}
if (userdata) {
PyObject *findproc = (PyObject *)userdata;
PyObject *res = PyObject_CallFunction(findproc, "s", filename);
if (res && PyString_AsString(res)) {
result = MemoryLoadLibraryEx(PyString_AsString(res),
_LoadLibrary, _GetProcAddress, _FreeLibrary,
userdata);
Py_DECREF(res);
if (result) {
lib = _AddMemoryModule(filename, result);
POP();
dprintf("_LoadLibrary(%s, %p) -> %s[%d]\n\n",
filename, userdata, lib->name, lib->refcount);
return lib->module;
} else {
dprintf("_LoadLibrary(%s, %p) failed with error %d\n",
filename, userdata, GetLastError());
}
} else {
PyErr_Clear();
}
}
result = (HCUSTOMMODULE)LoadLibraryA(filename);
POP();
dprintf("LoadLibraryA(%s) -> %p\n\n", filename, result);
return result;
}
/****************************************************************
* Public functions
*/
HMODULE MyGetModuleHandle(LPCSTR name)
{
LIST *lib;
lib = _FindMemoryModule(name, NULL);
if (lib)
return lib->module;
return GetModuleHandle(name);
}
HMODULE MyLoadLibrary(LPCSTR name, void *bytes, void *userdata)
{
if (userdata) {
HCUSTOMMODULE mod = _LoadLibrary(name, userdata);
if (mod)
return mod;
} else if (bytes) {
HCUSTOMMODULE mod = MemoryLoadLibraryEx(bytes,
_LoadLibrary,
_GetProcAddress,
_FreeLibrary,
userdata);
if (mod) {
LIST *lib = _AddMemoryModule(name, mod);
return lib->module;
}
}
return LoadLibrary(name);
}
BOOL MyFreeLibrary(HMODULE module)
{
LIST *lib = _FindMemoryModule(NULL, module);
if (lib) {
if (--lib->refcount == 0)
MemoryFreeLibrary(module);
return TRUE;
} else
return FreeLibrary(module);
}
FARPROC MyGetProcAddress(HMODULE module, LPCSTR procname)
{
FARPROC proc;
LIST *lib = _FindMemoryModule(NULL, module);
if (lib) {
// dprintf("MyGetProcAddress(%p, %s)\n", module, procname);
// PUSH();
proc = MemoryGetProcAddress(lib->module, procname);
// POP();
// dprintf("MyGetProcAddress(%p, %s) -> %p\n", module, procname, proc);
return proc;
} else
return GetProcAddress(module, procname);
}