2013-06-24 36 views
2

我真的有一個很難使用ctypes的的Python的ctypes調用簡單的C++ DLL

以下調用從蟒蛇一個簡單的C++ DLL是我的C++代碼:

#ifdef __cplusplus 
extern "C"{ 
#endif 
    __declspec(dllexport) char const* greet() 
{ 
    return "hello, world"; 
} 
#ifdef __cplusplus 
} 
#endif 

...

我的Python代碼:

import ctypes 
testlib = ctypes.CDLL("CpLib.dll"); 
print testlib.greet(); 

當我運行我的py腳本,我得到這個奇怪的返回值-97902232

請協助。

回答

3

你沒有告訴ctypes返回值是什麼類型,所以它假定它是一個整數。但它實際上是一個指針。設置restype屬性讓ctypes知道如何解釋返回值。

import ctypes 
testlib = ctypes.CDLL("CpLib.dll") 
testlib.greet.restype = ctypes.c_char_p 
print testlib.greet() 
+0

哎呀...謝謝你:) –