1

我似乎无法让这段代码工作,我的印象是我做对了。

from ctypes import *


kernel32 = windll.kernel32

string1 = "test"
string2 = "test2"

kernel32.MessageBox(None,
                       string1,
                       string2,
                       MB_OK)

** 我尝试按照下面的建议将其更改为 MessageBoxA ** ** 我得到错误 :: **

Traceback (most recent call last):
  File "C:\<string>", line 6, in <module>
  File "C:\Python26\Lib\ctypes\__init__.py", line 366, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python26\Lib\ctypes\__init__.py", line 371, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'MessageBoxA' not found
4

3 回答 3

4

MessageBox 是在 user32 而不是 kernel32 中定义的,你也没有定义 MB_OK 所以改用它

windll.user32.MessageBoxA(None, string1, string2, 1)

另外我建议使用python win32 API而不是它,因为它具有所有常量和命名函数

编辑:我的意思是用这个

from ctypes import *

kernel32 = windll.kernel32

string1 = "test"
string2 = "test2"

#kernel32.MessageBox(None, string1, string2, MB_OK)
windll.user32.MessageBoxA(None, string1, string2, 1)

你可以使用 win32 api 做同样的事情

import win32gui
win32gui.MessageBox(0, "a", "b", 1)
于 2009-06-17T04:32:48.813 回答
0

哦,任何时候你对调用是否需要 kernel32 或 user32 或类似的东西感到困惑。不要害怕在 MSDN 上寻找电话。他们有一个按字母顺序排列的列表和一个基于类别的列表。希望您发现它们对您有所帮助。

于 2009-06-17T06:49:59.490 回答
0

问题是您尝试调用的函数实际上并未命名MessageBox()。有两个函数,namedMessageBoxA()MessageBoxW():前者采用 8 位 ANSI 字符串,后者采用 16 位 Unicode(宽字符)字符串。在 C 中,预处理器符号MessageBox#definedMessageBoxAMessageBoxW,这取决于是否启用了 Unicode(特别是,如果_UNICODE定义了符号)。

其次,根据MessageBox()文档MessageBoxA/W位于user32.dll,而不是kernel32.dll.

试试这个(我无法验证它,因为我现在不在 Windows 框前):

user32 = windll.user32
user32.MessageBoxA(None, string1, string2, MB_OK)
于 2009-06-17T04:31:21.983 回答