Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60008 > unrolled thread

how to get raw bytes for ctypes functions that return c_wchar_p

Started byMark Summerfield <list@qtrac.plus.com>
First post2013-11-19 08:58 -0800
Last post2013-11-19 10:58 -0800
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  how to get raw bytes for ctypes functions that return c_wchar_p Mark Summerfield <list@qtrac.plus.com> - 2013-11-19 08:58 -0800
    Re: how to get raw bytes for ctypes functions that return c_wchar_p Thomas Heller <theller@ctypes.org> - 2013-11-19 18:22 +0100
      Re: how to get raw bytes for ctypes functions that return c_wchar_p Mark Summerfield <list@qtrac.plus.com> - 2013-11-19 10:58 -0800

#60008 — how to get raw bytes for ctypes functions that return c_wchar_p

FromMark Summerfield <list@qtrac.plus.com>
Date2013-11-19 08:58 -0800
Subjecthow to get raw bytes for ctypes functions that return c_wchar_p
Message-ID<1be2895d-8b81-4f12-8fc5-6d116ffaba32@googlegroups.com>
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?

Thanks!

[toc] | [next] | [standalone]


#60012

FromThomas Heller <theller@ctypes.org>
Date2013-11-19 18:22 +0100
Message-ID<bf1kucFjcppU1@mid.individual.net>
In reply to#60008
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

[toc] | [prev] | [next] | [standalone]


#60021

FromMark Summerfield <list@qtrac.plus.com>
Date2013-11-19 10:58 -0800
Message-ID<8de1a35e-ef2e-4ff8-92c6-a8350d019b8d@googlegroups.com>
In reply to#60012
On Tuesday, November 19, 2013 5:22:36 PM UTC, Thomas Heller wrote:
> 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

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

Thanks!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web