1. 程式人生 > 程式設計 >python ctypes庫2_指定引數型別和返回型別詳解

python ctypes庫2_指定引數型別和返回型別詳解

python函式的引數型別和返回型別預設為int。

如果需要傳遞一個float值給dll,那麼需要指定引數的型別。

如果需要返回一個flaot值到python中,那麼需要指定返回資料的型別。

資料型別參考python文件:

https://docs.python.org/3.6/library/ctypes.html#fundamental-data-types

import ctypes
path = r'E:\01_Lab\VisualStudioLab\cpp_dll\cpp_dll\Debug\cpp_dll.dll'
dll = ctypes.WinDLL(path)
 
dll.add_float.argtypes = [ctypes.c_float,ctypes.c_float]
dll.add_float.restype = ctypes.c_float
 
data_float = dll.add_float(7,10) # (ctypes.c_float(7.0),ctypes.c_float(10.0))

c++中函式如下:

DLLEXPORT float __stdcall add_float(float a,float b)
{
 float sum = a + b;
 return sum;
}

以上這篇python ctypes庫2_指定引數型別和返回型別詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。