我想從Python(我是Python新手)訪問C++ DLL。我克服了許多調用約定問題,並最終讓它在沒有任何編譯/鏈接錯誤的情況下運行。但是,當我打印從Python的C + + dll返回數組它顯示所有隨機初始化值。它看起來像值沒有正確返回。返回從Python調用的指針的C++ DLL
我的C++代碼如下所示。
double DLL_EXPORT __cdecl *function1(int arg1, int arg2, double arg3[],int arg4,double arg5,double arg6,double arg7, double arg8)
{
double *Areas = new double[7];
....Calculations
return Areas;
}
我的Python代碼如下:
import ctypes
CalcDll = ctypes.CDLL("CalcRoutines.dll")
arg3_py=(ctypes.c_double*15)(1.926,1.0383,0.00008,0.00102435,0.0101,0.0,0.002,0.0254,102,1,0.001046153,0.001046153,0.001046153,0.001046153,20)
dummy = ctypes.c_double(0.0)
CalcDll.function1.restype = ctypes.c_double*7
Areas = CalcDll.function1(ctypes.c_int(1),ctypes.c_int(6),arg3_py,ctypes.c_int(0),dummy,dummy,dummy,dummy)
for num in HxAreas:
print("\t", num)
print語句的輸出如下:
2.4768722583947873e-306
3.252195577561737e+202
2.559357001198207e-306
5e-324
2.560791130833573e-306
3e-323
2.5621383435212475e-306
任何建議什麼我做錯了,非常感謝。
注意,返回結構並不總是功能做了不同的編譯器同樣的方式正確打印(我瞭解到這個硬盤的方式) 。 C++編譯器返回這種結構的方式可能與您的Python版本期望的方式不兼容。 –
http://stackoverflow.com/a/6731737/95954 –
這可能更好的是返回已分配'malloc()'的雙精度數組,以便可以通過ctypes解除分配。 –