Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2.6帮助中的ctypes_Python_Ctypes - Fatal编程技术网

Python2.6帮助中的ctypes

Python2.6帮助中的ctypes,python,ctypes,Python,Ctypes,我似乎无法让这段代码正常工作,我的印象是我做得很正确 from ctypes import * kernel32 = windll.kernel32 string1 = "test" string2 = "test2" kernel32.MessageBox(None, string1, string2, MB_OK) **我尝试将其更改为Messa

我似乎无法让这段代码正常工作,我的印象是我做得很正确

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
回溯(最近一次呼叫最后一次):
文件“C:\”,第6行,在
文件“C:\Python26\Lib\ctypes\\uuuu init\uuuuu.py”,第366行,在\uuu getattr中__
func=self.\uuuu getitem\uuuuu(名称)
文件“C:\Python26\Lib\ctypes\\uuuu init\uuuuu.py”,第371行,在\uuu getitem中__
func=self.\u FuncPtr((名称或顺序,self))
AttributeError:未找到函数“MessageBoxA”

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

其次,根据
MessageBoxA/W
位于
user32.dll
,而不是
kernel32.dll

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


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

其次,根据
MessageBoxA/W
位于
user32.dll
,而不是
kernel32.dll

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


MessageBox是在user32中定义的,而不是在kernel32中定义的,您还没有定义MB_OK 所以用这个代替

windll.user32.MessageBoxA(None, string1, string2, 1)
此外,我还建议使用isntead,因为它具有所有常量和命名函数

编辑:我的意思是用这个

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)

MessageBox是在user32中定义的,而不是在kernel32中定义的,您还没有定义MB_OK 所以用这个代替

windll.user32.MessageBoxA(None, string1, string2, 1)
此外,我还建议使用isntead,因为它具有所有常量和命名函数

编辑:我的意思是用这个

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)

哦,任何时候你都会对一个调用是否需要kernel32或user32之类的东西感到困惑。不要害怕在MSDN上寻找呼叫。他们有一个列表,也有一个基于的列表。
希望您能发现它们对您有所帮助。

哦,如果您对呼叫是否需要kernel32、user32或诸如此类的东西感到困惑的话。不要害怕在MSDN上寻找呼叫。他们有一个列表,也有一个基于的列表。
希望您能从中受益。

我的意思是您使用了错误的dll,请看我用更改的行重新放置了整个代码哦,我是说win32 api,我想,我没有意识到这是一个链接:ppI的意思是您使用了错误的dll,请看我用更改的行重新放置了整个代码哦,我是说win32 api,请看,我不知道这是一个链接:pp