ctypes包装“MessageBoxA”示例在python33中不起作用

时间:2013-08-10 18:20:48

标签: python ctypes

这个例子在python 3.3.2 doc:

http://docs.python.org/3/library/ctypes.html?highlight=ctypes#ctypes

但是:当我在翻译中尝试时,我收到错误。

我使用的是windows7 32 python 3.3.2。

请帮忙。

from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
MessageBox()
MessageBox(text="Spam, spam, spam")
MessageBox(flags=2, text="foo bar")

error message:
Traceback (most recent call last):
File "<string>", line 250, in run_nodebug
File "<g1>", line 7, in <module>
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

1 个答案:

答案 0 :(得分:6)

猜猜这是文档中的错误。 ; - )

在Python 3中,默认情况下所有字符串都是Unicode,但示例是调用ANSI MessageBoxA函数,而不是MessageBoxW Unicode版本。请参阅ctypes文档中的16.17.1.2. Accessing functions from loaded dlls

因此,对于示例中的MessageBoxA,您可以通过调用locale.getpreferredencoding()将函数的输入字符串参数编码为所需内容来使其工作:

from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
import locale
preferred_encoding = locale.getpreferredencoding(False)
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = ((1, "hwnd", 0), (1, "text", "Hi".encode(preferred_encoding)),
                (1, "caption", None), (1, "flags", 0))
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

MessageBox()
MessageBox(text="Spam, spam, spam".encode(preferred_encoding))
MessageBox(flags=2, text="foo bar".encode(preferred_encoding))

使用支持“宽”Unicode字符串参数(MessageBoxW而不是LPCWSTR)的LPCSTR Windows函数,使得它们的显式编码几乎不那么尴尬每次通话都不必要此外,我将使用命名常量替换示例中的大多数“幻数”:

from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCWSTR, UINT
import win32con  # contains Win32 constants pulled from the C header files
INPUT_PARM, OUTPUT_PARAM, INPUT_PARM_DEFAULT_ZERO = 1, 2, 4
prototype = WINFUNCTYPE(c_int, HWND, LPCWSTR, LPCWSTR, UINT)
paramflags = ((INPUT_PARM, "hwnd", 0),
              (INPUT_PARM, "text", "Hi"),
              (INPUT_PARM, "caption", None),
              (INPUT_PARM, "flags", win32con.MB_HELP))
MessageBox = prototype(("MessageBoxW", windll.user32), paramflags)

MessageBox()
MessageBox(text="Spam, spam, spam")
MessageBox(flags=win32con.MB_ABORTRETRYIGNORE, text="foo bar")