473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

with ctypes, how to parse a multi-string


Hi,

I am currently dealing with ctypes, interfacing with winscard libbrary
(for smart card access).

Several APIs (e.g. SCardListReaderGroupsW ) take a pointer to an
unicode string as a parameter , which points at function return to a
"sequence" of unicode strings, NULL terminated. The last string is
double NULL terminated. (of course buffer length is also returned as
another parameter).

e.g. it could return something like
'group1\x00group2\x00group3\x00\x00'

What should I use as argtypes to my function prototype in order to
gain access to the full list? using c_wchar_p works, but it resolves
the string until it reaches the first \x00, resulting in having access
to the first entry of the list only.

as reference, my current ctypes mapping for this API is:

# extern WINSCARDAPI LONG WINAPI
# SCardListReaderGroupsW(
# IN SCARDCONTEXT hContext,
# OUT LPWSTR mszGroups,
# IN OUT LPDWORD pcchGroups);

_ListReaderGroups = scardlib.SCardListReaderGroupsW
_ListReaderGroups.argtypes = [ c_ulong, c_void_p, c_void_p ]
_ListReaderGroups.restype = c_ulong

Calling the API looks like:

pcchreadergrp = c_long(SCARD_AUTOALLOCATE)
groups = c_wchar_p()

_ListReaderGroups( ctx, byref(groups), byref(pcchreadergrp))
Should I be using some array/ctypes.cast() combination to gain access
to all returned characters?

May 30 '07 #1
2 2710
Eric schrieb:
Hi,

I am currently dealing with ctypes, interfacing with winscard libbrary
(for smart card access).

Several APIs (e.g. SCardListReaderGroupsW ) take a pointer to an
unicode string as a parameter , which points at function return to a
"sequence" of unicode strings, NULL terminated. The last string is
double NULL terminated. (of course buffer length is also returned as
another parameter).

e.g. it could return something like
'group1\x00group2\x00group3\x00\x00'

What should I use as argtypes to my function prototype in order to
gain access to the full list? using c_wchar_p works, but it resolves
the string until it reaches the first \x00, resulting in having access
to the first entry of the list only.
A c_wchar_p instance represent a (one!) zero-terminated string, as you
already know. A POINTER(c_wchar) instance is more flexible, you should
use that instead. It can be indexed/sliced with arbitrary indexes.

Here is a simple script to get you started:

"""
from ctypes import *

# Normally, the function call will fill the buffer:
buf = create_unicode_buffer("first\0second\0third\0")

# The pointer you will pass to the function call
ptr = cast(buf, POINTER(c_wchar))

# function call omitted

# Print the raw result
print ptr[:len(buf)]

# Print a list of strings
print ptr[:len(buf)].split("\0")
"""

Thomas

May 31 '07 #2
On May 31, 2:52 pm, Thomas Heller <thel...@ctypes.orgwrote:
Eric schrieb:
Hi,
I am currently dealing with ctypes, interfacing with winscard libbrary
(for smart card access).
Several APIs (e.g. SCardListReaderGroupsW ) take a pointer to an
unicode string as a parameter , which points at function return to a
"sequence" of unicode strings, NULL terminated. The last string is
double NULL terminated. (of course buffer length is also returned as
another parameter).
e.g. it could return something like
'group1\x00group2\x00group3\x00\x00'
What should I use as argtypes to my function prototype in order to
gain access to the full list? using c_wchar_p works, but it resolves
the string until it reaches the first \x00, resulting in having access
to the first entry of the list only.

A c_wchar_p instance represent a (one!) zero-terminated string, as you
already know. A POINTER(c_wchar) instance is more flexible, you should
use that instead. It can be indexed/sliced with arbitrary indexes.

Here is a simple script to get you started:

"""
from ctypes import *

# Normally, the function call will fill the buffer:
buf = create_unicode_buffer("first\0second\0third\0")

# The pointer you will pass to the function call
ptr = cast(buf, POINTER(c_wchar))

# function call omitted

# Print the raw result
print ptr[:len(buf)]

# Print a list of strings
print ptr[:len(buf)].split("\0")
"""

Thomas
Thanks Thomas, it works as expected!

Regards,

Eric

May 31 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anton Vredegoor | last post by:
Recently there has been a lot of progress concerning the integration of midi into python. A midipy.cpp for Python20 is found and various compiling efforts having been undertaken to compile it for...
6
by: Dave Kuhlman | last post by:
Suppose that I have content that looks like what I've included at the end of this message. Is there something in the standard Python library that will help me parse it, break into the parts...
3
by: chris | last post by:
What is the proper way to use ctypes to access an exported Function in a dll file on windows? I must be missing something because I get memory leaks when I use it: import ctypes import gc ...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
2
by: mclaugb | last post by:
I have created a simple windows small_dll.dll exporting a function that does a computation using C and C++ classes which i wish to call from Python. I have read lots of ctypes documentation but I...
0
by: malen | last post by:
Hi all! I'm building mouse movement filter program for Windows and Mac OS X. In Windows I use ctypes.windll.user32.getCursorPos(pointer) and ctypes.windll.user32.setCursorPos(x,y) to get and set...
5
by: salmankamkoriwala | last post by:
Default Working with Sessions in Multi Tab Browsers - PHP Hi, I am working on the php project and been stuck on Managing Session in Multiple tabs. My Problem is --- When i open a page in...
1
by: moreati | last post by:
Recently I discovered the re module doesn't support POSIX character classes: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for...
2
by: Philluminati | last post by:
I'm a bit of a python newbie and I need to wrap a C library. I can initialise the library using CDLL('mcclient.so') and I can call functions correctly inside the library but I need to invoke...
14
by: Gordon Allott | last post by:
Hello :) The result of various incompatibilities has left me needing to somehow extract the address that a null pointer is pointing to with the null pointer being exposed to python via...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.