- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
此示例在 python 3.3.2 文档中:
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
最佳答案
猜测这是文档中的一个错误。 ;-)
在 Python 3 中,默认情况下所有字符串都是 Unicode,但示例调用的是 ANSIMessageBoxA
函数,而不是MessageBoxW
Unicode 版本。参见 16.17.1.2. Accessing functions from loaded dlls在ctypes
文档中。
因此,对于示例中的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
Windows 函数(LPCWSTR
而不是LPCSTR
) 使得几乎每次调用都不需要对它们进行显式编码。此外,我会用命名常量替换示例中的大部分“魔数(Magic Number)”:
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")
关于python - ctypes 包装 "MessageBoxA"示例在 python33 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164994/
我正在尝试使用 gcc inline 在程序集中直接调用 MessageBoxA()。但是我需要通过两种方式来做到这一点:首先是使用动态寻址,使用 LoadLibrary() 和 GetProcAdd
我正在尝试将 MessageBoxA 函数与 MS Detours 3.0 Hook ,但是当我尝试它时,我的程序崩溃了。我不确定是什么导致程序崩溃。当我运行测试程序并按下 shift 时,会出现消息
此示例在 python 3.3.2 文档中: http://docs.python.org/3/library/ctypes.html?highlight=ctypes#ctypes 但是:当我在解释
我正在用 C# 编写一个报警程序,它在指定的时间显示一个带有用户指定消息的系统模式对话框。但是,我似乎找不到 C# 等价物 MessageBoxA(HWND_DESKTOP, msg, "Alarm"
这个问题在这里已经有了答案: How to convert std::string to LPCSTR? (9 个回答) 关闭 9 年前。 我在 C++ 中试过这个: std::string tes
我是一名优秀的程序员,十分优秀!