windows Python ctypes 和没有足够的参数(缺少 4 个字节)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1458813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python ctypes and not enough arguments (4 bytes missing)
提问by
The function i'm trying to call is:
我试图调用的函数是:
void FormatError (HRESULT hrError,PCHAR pszText);
from a custom dll using windll.
从使用windll的自定义dll。
c_p = c_char_p()
windll.thedll.FormatError(errcode, c_p)
Results in:
结果是:
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
Using cdll instead increases the bytes missing counter to 12. errcode above is the errercode returned from another function out of the same dll. How do I get the call right?
使用 cdll 将字节丢失计数器增加到 12。上面的 errcode 是从同一个 dll 的另一个函数返回的 errercode。我如何接听电话?
回答by Mark Rushakoff
At the very least, you'll get more descriptive errors if you properly set up the argtypes
and the restype
.
Try doing it this way:
尝试这样做:
windll.thedll.FormatError.argtypes = [ctypes.HRESULT, ctypes.c_char_p]
windll.thedll.FormatError.restype = None
There's also a very good chance you are using the wrong calling convention -- check out the Calling Functions sectionand the Loading Libraries sectionfor details on how to use a different calling convention.
回答by Nick Craig-Wood
Actually I think you want to use FormatError as provided by ctypes
其实我认为你想使用 ctypes 提供的 FormatError
http://docs.python.org/library/ctypes.html#ctypes.FormatError
http://docs.python.org/library/ctypes.html#ctypes.FormatError
ctypes.FormatError([code])
Windows only: Returns a textual description of the error code. If no error code is specified, the last error code is used by calling the Windows api function GetLastError.
ctypes.FormatError([代码])
仅限 Windows:返回错误代码的文本描述。如果未指定错误代码,则通过调用 Windows api 函数 GetLastError 使用最后一个错误代码。
回答by luc
Have you tried to use the ctypes.HRESULT?
您是否尝试过使用ctypes.HRESULT?