[r723]: trunk / py2exe / source / Python-dynload.c Maximize Restore History

Download this file

Python-dynload.c    89 lines (80 with data), 2.1 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
/* **************** Python-dynload.c **************** */
#include "Python-dynload.h"
#include "MemoryModule.h"
#include "actctx.h"
#include <stdio.h>
struct IMPORT imports[] = {
#include "import-tab.c"
{ NULL, NULL }, /* sentinel */
};
void Py_XDECREF(PyObject *ob)
{
static PyObject *tup;
if (tup == NULL)
tup = PyTuple_New(1);
/* Let the tuple take the refcount */
PyTuple_SetItem(tup, 0, ob);
/* and overwrite it */
PyTuple_SetItem(tup, 0, PyInt_FromLong(0));
}
void Py_XINCREF(PyObject *ob)
{
if (ob)
Py_BuildValue("O", ob);
}
int _load_python_FromFile(char *dllname)
{
int i;
struct IMPORT *p = imports;
HMODULE hmod;
// In some cases (eg, ISAPI filters), Python may already be
// in our process. If so, we don't want it to try and
// load a new one! (Actually, we probably should not even attempt
// to load an 'embedded' Python should GetModuleHandle work - but
// that is less clear than this straight-forward case)
// Get the basename of the DLL.
char *dllbase = dllname + strlen(dllname);
while (dllbase != dllname && *dllbase != '\\')
dllbase--;
if (*dllbase=='\\')
++dllbase;
hmod = GetModuleHandle(dllbase);
if (hmod == NULL)
hmod = LoadLibrary(dllname);
if (hmod == NULL) {
return 0;
}
for (i = 0; p->name; ++i, ++p) {
p->proc = (void (*)())GetProcAddress(hmod, p->name);
if (p->proc == NULL) {
OutputDebugString("undef symbol");
fprintf(stderr, "undefined symbol %s -> exit(-1)\n", p->name);
return 0;
}
}
return 1;
}
int _load_python(char *dllname, char *bytes)
{
int i;
struct IMPORT *p = imports;
HMODULE hmod;
ULONG_PTR cookie = 0;
if (!bytes)
return _load_python_FromFile(dllname);
cookie = _My_ActivateActCtx();//try some windows manifest magic...
hmod = MemoryLoadLibrary(dllname, bytes);
_My_DeactivateActCtx(cookie);
if (hmod == NULL) {
return 0;
}
for (i = 0; p->name; ++i, ++p) {
p->proc = (void (*)())MemoryGetProcAddress(hmod, p->name);
if (p->proc == NULL) {
OutputDebugString("undef symbol");
fprintf(stderr, "undefined symbol %s -> exit(-1)\n", p->name);
return 0;
}
}
return 1;
}