473,320 Members | 1,825 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,320 software developers and data experts.

win32com, BSTR, and null terminated strings



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

Feb 5 '06 #1
5 3280
Matt Helm wrote:

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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 6 '06 #2

"Matt Helm" <er**@qwhd746oihd49.net> wrote in message
news:mz**********@qwhd746oihd49.net...
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.
Feb 6 '06 #3
John Bauman wrote:
"Matt Helm" <er**@qwhd746oihd49.net> wrote in message
news:mz**********@qwhd746oihd49.net...
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 6 '06 #4
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

Feb 6 '06 #5

"John Bauman" <jo**@baumanfamily.com> writes:
"Matt Helm" <er**@qwhd746oihd49.net> wrote in message
news:mz**********@qwhd746oihd49.net...
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

Feb 6 '06 #6

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

Similar topics

1
by: Chris | last post by:
I am not sure if this is the right newsgroup. But does anyone know what is the difference between a BSTR and a LPOLESTR? The only thing I could find out is that the advantage of taking BSTR...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
3
by: Michel Racicot | last post by:
I need to read a null terminated ASCII string from a FileStream. How can I do that? I know that with FileStream.ReadByte, I can read until I get a byte == 0 but how can I convert a single byte to...
7
by: Gilad Walden | last post by:
I use C# in .NET framework. I have an ActiveX implemented in C++ that has a COM interface method that gets as it’s out parameter a BSTR* . The interop translates this BSTR* into C# string. From...
2
by: Ithaqua | last post by:
How do you create them? -- Cheers Ithaqua
3
by: Carlo Stonebanks | last post by:
I need to read a binary file which has mixed data types embedded in it. Fixed length strings, 16-bit integers and zero (null) terminated ASCII strings. (akak ASCIIZ) The first two types are no...
37
by: Egbert Nierop \(MVP for IIS\) | last post by:
In win32 mode, a BSTR was UINT length prefixed and terminated exactly by a zero char. So if you allocated "Hello World" that would allocate 28 bytes. In x64 and (IA64 as well) it would become...
0
by: Fozzie | last post by:
Hi, I have a problem which is quite circular, and hopefully either someone has encountered something similar or has a reason why this will not work. We have a COM library providing...
12
by: semut | last post by:
Given that the string is of null terminated type. What could be the possible causes (by experience) the string to have no null terminated and cause buffer overflow later. I know it is quite broad,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.