python调用c++开发的动态库

此处列举一下python调用Windows端动态库。

# *- coding=utf-8 -*
import ctypes
from ctypes import *
import os



objdll = ctypes.windll.LoadLibrary("xxx.dll")

nRet = objdll.Init()
print("Init = " + str(nRet))

objdll.ResetImageData()  # 这个家伙返回值是void类型
# print("ResetImageData = "+str(nRet))


nRet = objdll.LoadImageToMemory("xxx.jpg", 0)
print("LoadImageToMemory = " + str(nRet))


nRet = objdll.Set(xxx, byref(c_int(xxx)), 1)
print("Set = " + str(nRet))

objdll.SetParameter(0, xxx)  # 函数SetParameter返回值类型为void
# print("SetParameter = "+str(nRet))

nRet = objdll.SetProcessType(x, x)
print("SetProcessType = " + str(nRet))

nRet = objdll.SetLanguage(0)
print("SetLanguage = " + str(nRet))

nRet = objdll.Recog()
print("Recog = " + str(nRet))

nIndex = 0
strResult = c_wchar_p('')

strFieldName = c_wchar_p('')
if nRet > 0:
    for nIndex in range(7):
        nFieldLenth = 1000
        nRet = objdll.GetName(nIndex, strFieldName, byref(c_int(nFieldLenth)))
        print("GetFieldName = ")
        print(strFieldName.value)
        if nRet == 3:
            break
        if nRet == 0:
            nResultLenth = 1000
            nRet = objdll.GeResult(nIndex, strResult, byref(c_int(nResultLenth)))
            print("GetRecogResult = " + str(nRet))
            print(nIndex)
            print(strResult.value)

nRet = objdll.SaveHeadImage("xxx.jpg");
print("SaveHeadImage = " + str(nRet))

os.system("pause")

重点需要说明的是:

1、支持中文需要:

1 #*- coding=utf-8 -*

2、python调用dll需要:

1 import ctypes
2 from ctypes import *

3、C++接口中参数为LPTSTR在python ctypes中对应:

1 strResult = c_wchar_p('')
2 strFieldName = c_wchar_p('')

4、C++接口中的引用,在python ctypes中对应:

1 byref(c_int(nFieldLenth))

以上代码仅供参考,这些都是很具体的例子,使用中转化成自己需要的。