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))

以上代碼僅供參考,這些都是很具體的例子,使用中轉化成自己需要的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM