tags:

views:

600

answers:

3

I can't seem to get this code to work, I was under the impression I was doing this correctly.

from ctypes import *


kernel32 = windll.kernel32

string1 = "test"
string2 = "test2"

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

** I tried to change it to MessageBoxA as suggested below ** ** Error I get :: **

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
A: 

The problem is that the function you're trying to call isn't actually named MessageBox(). There are two functions, named MessageBoxA() and MessageBoxW(): the former takes 8-bit ANSI strings, and the latter takes 16-bit Unicode (wide-character) strings. In C, the preprocessor symbol MessageBox is #defined to be either MessageBoxA or MessageBoxW, depending on whether or not Unicode is enabled (specifically, if the symbol _UNICODE is defined).

Secondly, according to the MessageBox() documentation, MessageBoxA/W are located in user32.dll, not kernel32.dll.

Try this (I can't verify it, since I'm not in front of a Windows box at the moment):

user32 = windll.user32
user32.MessageBoxA(None, string1, string2, MB_OK)
Adam Rosenfield
+3  A: 

MessageBox is defined in user32 not kernel32, you also haven't defined MB_OK so use this instead

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

Also i recommend using python win32 API isntead of it ,as it has all constant and named functions

edit: I mean use this

from ctypes import *


kernel32 = windll.kernel32

string1 = "test"
string2 = "test2"

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

same thing you can do using win32 api as

import win32gui
win32gui.MessageBox(0, "a", "b", 1)
Anurag Uniyal
What do you mean?
I meant you are using wrong dll, see i have put whole code again with changed line
Anurag Uniyal
Oh I was talking about the win32 api think, I didn't realize it was a link :pp
Oh and 0 would be mb_ok
yes 1 is ok/cancel
Anurag Uniyal
A: 

Oh and anytime you are confused about if a call needs kernel32 or user32 or something of the sorts. Don't be afraid to look for the call on MSDN. They have an Alphabetical List and also a list based on categories. Hope you find them helpful .

LogicKills