Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Thomas Heller Newsgroups: comp.lang.python Subject: Re: how to get raw bytes for ctypes functions that return c_wchar_p Date: Tue, 19 Nov 2013 18:22:36 +0100 Lines: 34 Message-ID: References: <1be2895d-8b81-4f12-8fc5-6d116ffaba32@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net v3MgVGsVtAMF8wDfg62sYwSutR2wlKmsv80Eb0CuawKNMCDLU= Cancel-Lock: sha1:6B1ANPBAOOcXhxhZwDfeaoCmP+E= User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <1be2895d-8b81-4f12-8fc5-6d116ffaba32@googlegroups.com> Xref: csiph.com comp.lang.python:60012 Am 19.11.2013 17:58, schrieb Mark Summerfield: > Hi, > > I am using ctypes to access a function in a DLL using Python 3.3 > 32-bit on Windows 7 64-bit: > > dplGetPageText = dpl.DPLGetPageText dplGetPageText.argtypes = > (ctypes.c_int, ctypes.c_int) dplGetPageText.restype = > ctypes.c_wchar_p > > Python returns this as a str with the raw bytes already decoded. > > Unfortunately, when the returned text contains some special > characters (e.g. © or fi) it is not encoded correctly. This may be a > problem with Windows or with ctypes or with the library I'm using; or > of course, it could be my own mistake. > > To find out, I'd like to change the restype to give me the raw bytes > so that I can view them and if necessary decode them myself. > > Can anyone tell me how to change the restype to get the bytes? ctypes on Python 2.7 has the set_conversion_mode(coding, errors) which could be used to change the way c_wchar_p is converted from/to Python strings. Unfortunately it seems to be gone in Python 3.3. However, you can set restype to POINTER(c_char) and then index the result: result = dplGetPageText(...) print(result[0], result[1], result[2]) Thomas