Trying to load pyd with LoadLibraryEx and got failed

326 views Asked by At

I have a pyd pytest.pyd, where declared two functions: say_hello and add_numbers. So I want to load this pyd dynamically in C++ with LoadLibraryEx. But, when I try to call initpytest func, it fails.

const char* funcname = "initpytest";

HINSTANCE hDLL = LoadLibraryEx(L"D:\\msp\\myproj\\Test\\pytest.pyd", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);

FARPROC p = GetProcAddress(hDLL, funcname);

(*p)(); // fail

In output error: Fatal Python error: PyThreadState_Get: no current thread Unhandled exception at 0x00007FFCD0CC286E (ucrtbase.dll) in Test.exe: Fatal program exit requested.

Here code of the extension before generating to pyd:

#include "Python.h"

static PyObject* say_hello(PyObject* self, PyObject* args)
{
    const char* msg;

    if(!PyArg_ParseTuple(args, "s", &msg))
    {
        return NULL;
    }
    printf("This is C world\nYour message is %s\n", msg);
    return Py_BuildValue("i", 25);
}

static PyObject* add_numbers(PyObject* self, PyObject* args)
{
    double a, b;

    if (!PyArg_ParseTuple(args, "dd", &a, &b))
    {
        return NULL;
    }
    double res = a + b;
    return Py_BuildValue("d", res);
}

static PyMethodDef pytest_methods[] = {
    {"say_hello", say_hello, METH_VARARGS, "Say Hello!"},
    {"add_numbers", add_numbers, METH_VARARGS, "Adding two numbers"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initpytest(void)
{
    Py_InitModule("pytest", pytest_methods);
}
1

There are 1 answers

0
DavidW On BEST ANSWER

In the absence of a proper minimal, reproducible example it's impossible to be certain. However, it's probably because you haven't initialized the interpreter: (see this question for example). You need to call Py_Initialize before using any Python functions.

Can I suggest that you use the normal Python C-API tools for running modules (rather than doing it yourself with LoadLibraryEx!) until you fully understand what how embedding Python works. You might consider PyImport_AppendInittab (before initializing) to set up your function directly and avoid the Python search path.