I have an install structure like this:
install_folder
- app.exe
- lib.dll
- python
- kitsu.py
And I am trying to execute some functions in my application from the file kitsu.py.
My C++ code is like this:
( main.cpp )
TCHAR curr_path[MAX_PATH];
DWORD length = GetModuleFileName(nullptr, curr_path, MAX_PATH);
PathRemoveFileSpec(curr_path, MAX_PATH);
PyObject* pName, * pModule, * pDict, * pFunc, * pValue, * presult, * sys, * python_folder, * fromlist, * syspathres;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyUnicode_FromString((char*)"kitsu");
python_folder = PyUnicode_FromString((char*)".python");
PyObject* py_curr_path = PyUnicode_FromString((char*)curr_path);
char command[MAX_PATH];
sprintf(command, "sys.path.append(%s)", curr_path);
PyRun_SimpleString("import sys");
PyRun_SimpleString(command);
fromlist = PyTuple_Pack(1, python_folder);
// Load the module object
pModule = PyImport_ImportModuleEx((char*)"kitsu", nullptr, nullptr, fromlist);
PyObject* init_kitsu = PyObject_GetAttrString(pModule, (char*)"init_kitsu");
presult = PyObject_CallObject(init_kitsu, nullptr);
(Please excuse any formal inconsitencies as this is early stage code.)
The application crashes becuase pModule is nullptr.
Why is it failing to retrieve the module?
Thanks in advance,