0

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 because pModule is nullptr.

Why is it failing to retrieve the module?

4
  • But you add the current directory to sys.path, not the python subdirectory? Commented Jul 26, 2024 at 8:10
  • good point, I don't know why I didnt think of it, let me try like that Commented Jul 26, 2024 at 8:13
  • BTW no it didnt work, anyone has any other suggestion? Commented Jul 30, 2024 at 15:27
  • "it didn't work" is a terrible, inexcusable diagnosis. Edit your question with the code you have now. Did you correct for the missing quotes around the %s ? Commented Jul 30, 2024 at 19:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.