Reputation: 3739
I am really having a hard time to call a simple c++ dll from python using ctypes
Below is my C++ code :
#ifdef __cplusplus
extern "C"{
#endif
__declspec(dllexport) char const* greet()
{
return "hello, world";
}
#ifdef __cplusplus
}
#endif
...
My Python code :
import ctypes
testlib = ctypes.CDLL("CpLib.dll");
print testlib.greet();
When i run my py script, I get this strange return value of -97902232
Kindly assist.
Upvotes: 2
Views: 3862
Reputation: 612794
You didn't tell ctypes what type the return value is, and so it assumes that it is an integer. But it is in fact a pointer. Set the restype attribute to let ctypes know how to interpret the return value.
import ctypes
testlib = ctypes.CDLL("CpLib.dll")
testlib.greet.restype = ctypes.c_char_p
print testlib.greet()
Upvotes: 3