python-3.xdllctypeslpstr

Calling dll in python 3 with LPSTR


I have a .dll named my.dll, with 4 functions, called in python 3.5 using:
myDLL = ctypes.cdll.LoadLibrary(Path:\to\my.dll)

My problem is calling a function that has LPSTR:

#include "stdafx.h"
#include "newheader.h"    
double  _stdcall pain_function(double arg1, double arg2, LPSTR arg_string_with_spaces)

the other 3 calls to my.dll work fine.

Below is the code I tried for the pain_function:

import ctypes
from ctypes import wintypes

# Load dll
myDLL = ctypes.WinDLL(Path:\to\my.dll)

# Call function 
pain_function = myDLL.pain_function

# Typecast the arguments
pain_function.argtypes = [ctypes.c_double, ctypes.c_double, wintypes.LPSTR]

# Typecast the return
pain_function.restype = ctypes.c_double


pain_return = pain_function(arg1, arg2, some_string)

pain_return returns some nonsensical number. I tried some variations, mostly along the lines of:

some_string = ctypes.create_string_buffer(b' ' * 200)

I have looked here among other places:

  1. python-ctypes-prototype-with-lpcstr-out-parameter

  2. Using String Parameters in DLL Function Calls

What is this correct way to pass the string of 200 spaces to the dll?

Thank you in advance


Solution

  • You indicated the prototype was:

    double  _stdcall pain_function(double arg1, double arg2, LPSTR arg_string_with_spaces)
    

    but using:

    myDLL = ctypes.cdll.LoadLibrary(Path:\to\my.dll)
    

    It should be, for __stdcall calling convention:

    myDLL = ctypes.WinDLL('Path:\to\my.dll')