2

I know the code in C is this:

WCHAR string[] = L"string\0";

So I tried (in Python):

string = ctypes.create_unicode_buffer("String\0")

But this strips the Null character of or something. If I use create_string_buffer it does work.

string = ctypes.create_string_buffer("String\0")

Now the the string has 2 Null characters at the end of it:

>>> string.raw
'String\x00\x00'

So why does this not work for unicode?

Edit:

This works (source: Python Ctypes Null Terminated String Block):

string = ctypes.wstring_at(ctypes.create_unicode_buffer(u'string\0'), 7)
2

1 Answer 1

1

This works nicely. Note the single-parameter init of create_unicode_buffer nul-terminates:

>>> r=ctypes.create_unicode_buffer('String\0Other')
>>> r.value              # value display stops at first nul.
u'String'
>>> ''.join(r)           # gives answer similar to create_string_buffer's raw method.
u'String\x00Other\x00'
Sign up to request clarification or add additional context in comments.

1 Comment

I already found a hackish way to do it, but this seems cleaner. Thnx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.