2

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.

1 Answer 1

3

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() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.