[Python] ctypes.ArgumentError passing a const char * from Python 3 to C function in Windows DLL

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function in a Windows DLL:

    extern "C" {
    bool __declspec(dllexport) PublicOpenFile(const char * pFilePath)
    {
    return SomeOtherFunc(pFilePath);
    }
    }


    which I am trying to invoke from a Python 3.12.9 script as follows:

    import ctypes
    lib = ctypes.cdll.LoadLibrary('The.dll')
    funcOpenFile = lib.PublicOpenFile
    funcOpenFile.argtypes = [ctypes.c_char_p]
    funcOpenFile.restype = ctypes.c_bool
    fileName = "TheOutput.txt"
    result = funcOpenFile(fileName)
    print(result)


    and am getting:

    Traceback (most recent call last):
    File "C:\Python_Cpp.py", line 9, in <module>
    result = funcOpenFile(fileName)
    ^^^^^^^^^^^^^^^^^^^^^^
    ctypes.ArgumentError: argument 1: TypeError: wrong type


    What am I doing wrong?

    Many thanks

    Continue reading...

Compartilhe esta Página