how to get raw bytes for ctypes functions that return c_wchar_p

M

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 ï¬) 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?

Thanks!
 
T

Thomas Heller

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 ï¬) 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
 
M

Mark Summerfield

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 =


Python returns this as a str with the raw bytes already decoded.

Unfortunately, when the returned text contains some special
characters (e.g. © or ï¬) it is not encoded correctly. Thismay 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

That worked well. I also tried POINTER(c_wchar) which also worked (but in adifferent way). Now I can see the raw bytes and decode them how I like.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top