zoukankan      html  css  js  c++  java
  • 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))

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

  • 相关阅读:
    SQL 数据库备份
    压力测试工具WAS
    petshop4.0 详解之八(PetShop表示层设计
    类库生成的dll 添加 注释
    硬盘格式化后 数据全部找回
    petshop4.0 详解之七(PetShop表示层设计)
    JS 显示动态更新时间
    petshop4.0 详解之六(PetShop表示层设计)
    在linux环境下搭建嵌入式开发平台
    收录 Uboot 详解
  • 原文地址:https://www.cnblogs.com/juluwangshier/p/11733416.html
Copyright © 2011-2022 走看看