This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author amaury.forgeotdarc
Recipients amaury.forgeotdarc, jaraco, theller
Date 2009-02-03.01:39:20
SpamBayes Score 2.4466108e-06
Marked as misclassified No
Message-id <1233625165.11.0.941737782752.issue5119@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2009-02-03 01:39:25amaury.forgeotdarcsetrecipients: + amaury.forgeotdarc, theller, jaraco
2009-02-03 01:39:25amaury.forgeotdarcsetmessageid: <1233625165.11.0.941737782752.issue5119@psf.upfronthosting.co.za>
2009-02-03 01:39:23amaury.forgeotdarclinkissue5119 messages
2009-02-03 01:39:21amaury.forgeotdarccreate