win32com, BSTR, and null terminated strings

M

Matt Helm

I am using win32com to access a third party COM interface but am
having trouble using the string that is returned.

The vendor's docs show the following method:

HRESULT CookString(BSTR param_a, short buf_size, [out, retval] BSTR* result_b);

param_a is a string to be processed.
buf_size is the size of the returned string + 1.

I can't know what the exact length of the returned string will be but
it is safe to assume it will not exceed 80 chars.

I have been calling it as such:

import win32com.client

o = win32com.client.Dispatch("BFG9000.BFG9000")

r = o.Cook("ABCDEFG", 81)


r comes back as a Python string 81 chars in length with a null at the
end of the actual valid text. In other words, it is your normal buffer
containing a null terminated string.

Python treats the whole 81 chars as valid - even though most of the
string is garbage. This is the expected behavior and I understand
this.

However, what is the proper way to recover the actual string? I have
been using:

r.split("\0", 1)[0]

but it seems like a bit of a kludge.


Regards,
Matt
 
S

Steve Holden

Matt said:
I am using win32com to access a third party COM interface but am
having trouble using the string that is returned.

The vendor's docs show the following method:

HRESULT CookString(BSTR param_a, short buf_size, [out, retval] BSTR* result_b);

param_a is a string to be processed.
buf_size is the size of the returned string + 1.

I can't know what the exact length of the returned string will be but
it is safe to assume it will not exceed 80 chars.

I have been calling it as such:

import win32com.client

o = win32com.client.Dispatch("BFG9000.BFG9000")

r = o.Cook("ABCDEFG", 81)


r comes back as a Python string 81 chars in length with a null at the
end of the actual valid text. In other words, it is your normal buffer
containing a null terminated string.

Python treats the whole 81 chars as valid - even though most of the
string is garbage. This is the expected behavior and I understand
this.

However, what is the proper way to recover the actual string? I have
been using:

r.split("\0", 1)[0]

but it seems like a bit of a kludge.

It's perfectly good Python, though, so just forget about it or wrap it
in a "cstring(s)" function do you don't have to look at it!

regards
Steve
 
J

John Bauman

Matt Helm said:
However, what is the proper way to recover the actual string? I have
been using:

r.split("\0", 1)[0]
I'd prefer to use
r[:-1]
to strip off the last character of the string.
 
S

Steve Holden

John said:
However, what is the proper way to recover the actual string? I have
been using:

r.split("\0", 1)[0]

I'd prefer to use
r[:-1]
to strip off the last character of the string.
Unfortunately there's no guarantee that the string occupies the whole of
the returned buffer: the first zero byte terminates the string.

regards
Steve
 
M

Matt Helm

It's perfectly good Python, though, so just forget about it or wrap it
in a "cstring(s)" function do you don't have to look at it!

Thanks for the sanity check. I just wanted to make sure I was correct
as well as right.

Matt
 
M

Matt Helm

John Bauman said:
Matt Helm said:
However, what is the proper way to recover the actual string? I have
been using:

r.split("\0", 1)[0]
I'd prefer to use
r[:-1]
to strip off the last character of the string.

No good. The string could be "ABC" + \0 + 77 more chars of random memory.

Matt
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top