problem calling python function from c++ : pFunc = PyObject_GetAttrString(pModule, (char*)"main");

Viewed 21

So i'm trying to run a python function in my c++ program but I'm having trouble. This is the relevant code:

if (SendMessage(download, BM_GETSTATE, 0, 0)) {
            youtubeLink = SendMessage(ytLink, WM_GETTEXT, 0, 0); // gets the text from the ytlink hwnd
            Py_Initialize();
            PyObject* pName, * pModule, * pFunc, * pArgs, * pValue;
            pName = PyUnicode_FromString((char*)"Downloader");
            pModule = PyImport_Import(pName);
            pFunc = PyObject_GetAttrString(pModule, (char*)"download");
            pArgs = PyTuple_Pack(
                3,
                PyUnicode_FromString((char*)youtubeLink),
                PyUnicode_FromString((char*)outputPath),
                PyUnicode_FromString((char*)false)
            );
            pValue = PyObject_CallObject(pFunc, pArgs);
            auto result = _PyUnicode_AsString(pValue);
            Py_Finalize();

I suspect it has something to do with the youtubeLink and outputPath variables as they are of LRESULT and PWSTR types respectively and the python file takes in String variables as parameters but I'm not sure.

for some context, here is the python file:

from pytube import YouTube

def download(l, output, audioOnly):
    link = l
    yt = YouTube(link)
    if audioOnly:
        ys = yt.streams.get_audio_only()
        ys.download(output_path=output)
    else:
        ys = yt.streams.get_highest_resolution()
        ys.download(output_path=output)

has anyone encountered this type of problem before?

This is the full code if needed: https://github.com/elliot1234567/YTDownloader

Thanks!!

0 Answers
Related