Message81022
ctypes cannot guess the function signature, and does not know if the function
expects strings or unicodes.
In your examples,
ctypes.windll.user32.MessageBoxW(handle, text, caption, type)
will accept everything you pass, and create C values depending on the types of the
actual values of the parameters: when you pass a unicode, ctypes uses a wchar_t*
buffer; when you pass a narrow string, ctypes uses a char* buffer (and is wrong in
this case).
In your Structure example, you do declare a kind of signature for the Structure.
ctypes is now able to convert the value you give into the declared type.
You can do the same thing with functions, if you provide the signature:
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p,
ctypes.c_wchar_p, ctypes.c_int]
(or better, since this matches the documentation on msdn:)
from ctypes.wintypes import *
MessageBox.argtypes = [HWND, LPCWSTR, LPCWSTR, UINT]
And then you may indifferently pass strings or unicodes:
MessageBox(None, u"café", "drink", 0) |
|
Date |
User |
Action |
Args |
2009-02-03 01:39:25 | amaury.forgeotdarc | set | recipients:
+ amaury.forgeotdarc, theller, jaraco |
2009-02-03 01:39:25 | amaury.forgeotdarc | set | messageid: <1233625165.11.0.941737782752.issue5119@psf.upfronthosting.co.za> |
2009-02-03 01:39:23 | amaury.forgeotdarc | link | issue5119 messages |
2009-02-03 01:39:21 | amaury.forgeotdarc | create | |
|