pycharm Python 调用dll 遇到的错误集合



python LoadLibrary [WinError 126] 找不到指定的模块

解决办法:引用的文件目录不对,

path1 = os.path.split(os.path.realpath(__file__))[0]  # 表示当前所处的文件夹的绝对路径

path1FileMZ = '%s%s' % (path1, '/Licence_DLL/xxx.dll')  # dll的文件地址


[WinError 193] %1 不是有效的 Win32 应用程序。

解决办法:多少位的python就调用多少位dll


dll函数的例子:generate_license_file(char * onlycode, char* licenseName, char* companyname, char* email, char* phone, char* companyperson, char* effective, char *out)


C基本类型和ctypes中实现的类型映射表

ctypes数据类型 C数据类型
c_char char
c_short short
c_int int
c_long long
c_ulong unsign long
c_float float
c_double double
c_void_p void

char* 对应的是c_char_c

在python声明如下

例:OnlyCode_A = c_char_p(str(OnlyCode).encode(encoding="utf-8")) #唯一码

str()的作用是转成str格式,如格式为str可以直接.encode(encoding="utf-8")

其中,要和dll约定好编码格式


接收返回值 char *out

定义一个空的容器来存放值

str_A = c_char_p(("").encode(encoding="utf-8"))#接收返回的值

str_A.value 的类型为bytes所以要转换成str

str_A =bytes.decode( str_A.value) #把bytes 转换成str


编码转换方法
按utf-8的方式编码,转成bytes

website_bytes_utf8 = website.encode(encoding="utf-8")
  • 1

2.按gb2312的方式编码,转成bytes

website_bytes_gb2312 = website.encode(encoding="gb2312")
  • 1

3.解码成string,默认不填

website_string = website_bytes_utf8.decode()
  • 1

4.解码成string,使用gb2312的方式

website_string_gb2312 = website_bytes_gb2312.decode("gb2312")

注意:

调用过程中最好用try: except BaseException  as err: print("错误==",err) 来获取异常


猜你喜欢

转载自blog.csdn.net/qq_28218253/article/details/80669202
今日推荐