Python 3.10调用sharedvar.dll遇WinError 0xe0434352错误求助
我在Multicharts中使用SharedVar-a64.dll完全正常,但切换到Python 3.10调用时触发错误:
OSError: [WinError -532462766] Windows Error 0xe0434352
测试脚本
from ctypes import * # DLL , RETURN VALUE , FUCTION NAME, PARAMS #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", int, "svInit", string; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", int, "svGetInt", int,string; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", bool, "svSetValue", int,string,double; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", bool, "svGetBool", int,string; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", string, "svGetString", int,string; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", double, "svGetDouble", int,string; #DefineDLLFunc: "C:\Windows\SharedVar-a64.dll", bool, "svSetString", int,string,string; c_lib = "C:\\\\Windows\\\\SharedVar-a64.dll" Symbol = "15" lib = cdll.LoadLibrary(c_lib) print(lib) #load ok lib.svInit.argtype = [c_wchar_p] lib.svInit.restype = c_double linkID = lib.svInit("15") lib.svGetDouble.restype = c_int lib.svGetDouble.argtype = [c_int, c_wchar_p] ret = lib.svGetDouble(linkID, "PC") print(ret)
报错信息
C:\Users\Gilles\PycharmProjects\pythonProject\sockets-python-master\venv\Scripts\python.exe "C:/Users/Gilles/PycharmProjects/pythonProject/sockets-python-master/test shared2.py" <CDLL 'C:\Windows\SharedVar-a64.dll', handle 7ffa05b60000 at 0x1ac76f42e00> Traceback (most recent call last): File "C:\Users\Gilles\PycharmProjects\pythonProject\sockets-python-master\test shared2.py", line 26, in <module> linkID = lib.svInit("15") OSError: [WinError -532462766] Windows Error 0xe0434352 Process finished with exit code 1
修复步骤
修正函数类型定义错误
代码中多处函数签名与注释标注的DLL实际定义不符,这是引发错误的核心原因:svInit返回值应为int,你错误设置为c_double,导致栈内存混乱- 参数类型注意是
argtypes(复数形式)而非argtype,且多数Windows DLL默认使用ASCII字符串(c_char_p),而非宽字符(c_wchar_p) svGetDouble返回值应为c_double,你错误设置为c_int
修正后的关键代码:
# 修正svInit的类型定义 lib.svInit.argtypes = [c_char_p] lib.svInit.restype = c_int # 调用时传入字节串 linkID = lib.svInit(b"15") # 修正svGetDouble的类型定义 lib.svGetDouble.restype = c_double lib.svGetDouble.argtypes = [c_int, c_char_p] # 调用时传入字节串 ret = lib.svGetDouble(linkID, b"PC")匹配调用约定
当前用cdll.LoadLibrary对应C调用约定(cdecl),如果DLL实际是stdcall编译的,改用windll.LoadLibrary加载:lib = windll.LoadLibrary(c_lib)确认环境位数匹配
确保使用64位Python,因为SharedVar-a64.dll是64位DLL,32位Python无法加载64位动态库。
内容的提问来源于stack exchange,提问作者gillou
相关产品推荐
相关产品推荐

