Mailing List Archive

[issue5119] wide character parameter handling in ctypes
Jason R. Coombs <jaraco@jaraco.com> added the comment:

I've confirmed that my original assumption was quite false, and that
even if the parameters are the correct width, WNetAddConnection2W
behaves differently in 64-bit windows versus 32-bit windows, so it made
a bad test case.

So I've changed the title of this issue, because I still would like to
know if it is proper for ctypes to accept a narrow string but treat it
as a wide string without converting it.

----------
title: inconsistent wide character parameter handling in 64-bit python -> wide character parameter handling in ctypes

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5119] wide character parameter handling in ctypes [ In reply to ]
Jason R. Coombs <jaraco@jaraco.com> added the comment:

I see this in the documentation, which basically answers the question:

"windll does not try to select [wide or narrow functions] by magic, you
must access the version you need by specifying GetModuleHandleA or
GetModuleHandleW explicitely, and then call it with normal strings or
unicode strings respectively."

This behavior is inconsistent with how structures are handled, where
members are up-converted to unicode. For example.

>>> class simple(Structure):
>>> _fields_ = [('value', c_wchar_p)]
>>>
>>> simple('foo').value
u'foo'

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5119] wide character parameter handling in ctypes [ In reply to ]
Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment:

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)

----------
nosy: +amaury.forgeotdarc
resolution: -> works for me
status: open -> pending

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5119] wide character parameter handling in ctypes [ In reply to ]
Jason R. Coombs <jaraco@jaraco.com> added the comment:

Thanks for the excellent suggestion.

Please close this issue.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5119] wide character parameter handling in ctypes [ In reply to ]
Changes by Amaury Forgeot d'Arc <amauryfa@gmail.com>:


----------
status: pending -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5119>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com