python 3.6 导入c++dll所遇到的坑

1

返回值在c++里面为const char*,python 接收实际上为int类型
原因:python默认返回值为int
解决方法:

import ctypes
import os
CUR_PATH = os.path.dirname(__file__)
dllPath = os.path.join(CUR_PATH, "vando.dll")
pDll = ctypes.WinDLL(dllPath)
pResutl = pDll.get_term(1)
print(pResult)

改为:

import ctypes
import os
CUR_PATH = os.path.dirname(__file__)
dllPath = os.path.join(CUR_PATH, "vando.dll")
pDll = ctypes.WinDLL(dllPath)
 pDll.get_term.restype = ctypes.c_uint64
 print(pResult)

2

python 传入和返回dll的byte(在c++是char*)类型的中文输出乱码
原因:要解码
解决方法:
传入

pResutl = pDll.get_price(bytes("你", "gb2312"))

返回

b = ctypes.string_at(pResutl)
print(b.decode("gb2312"))

3

vscode 控制台输出中文乱码,但是cmd正常
setting.json中添加

"code-runner.runInTerminal":true

记录 2020/3/26
n-N-n

原创文章 12 获赞 2 访问量 2290

猜你喜欢

转载自blog.csdn.net/qq_40832960/article/details/105125848