convert string to BSTR

Just starting out? Need help? Post your questions and find answers here.
tonnelier
User
User
Posts: 45
Joined: Mon May 03, 2004 10:34 pm
Location: France

convert string to BSTR

Post by tonnelier »

How can I convert a string to BSTR (binary string)?

Thanks in advance.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Godd samples when asking ie UNI2ANSI on this forum :

viewtopic.php?t=10053&start=0&postdays= ... t=uni2ansi

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Of the top of my head, isn't a BSTR just it's length (WORD) followed by the string? If so, create a membank with size equal to length of str+2 (maybe an extra byte to be safe) then pokew(membank_address, str_length) and then mem copy the str to membank_address+2.
tonnelier
User
User
Posts: 45
Joined: Mon May 03, 2004 10:34 pm
Location: France

Post by tonnelier »

that's what I thought, but i'm not sure.
Sure : there is a long first that contains the length of the string.
But is the string after is an usual string, or unicode, or...?
tonnelier
User
User
Posts: 45
Joined: Mon May 03, 2004 10:34 pm
Location: France

Post by tonnelier »

I've not seen : a word, not a long? That's maybe the source of my problem.
tonnelier
User
User
Posts: 45
Joined: Mon May 03, 2004 10:34 pm
Location: France

Post by tonnelier »

Finally, it seems that one cans convert a string to BSTR by converting it first to unicode, with MultiByteToWideChar, and then using SysAllocString.
Will try.
tonnelier
User
User
Posts: 45
Joined: Mon May 03, 2004 10:34 pm
Location: France

Post by tonnelier »

Problems solved.

for information :

Code: Select all

ProcedureDLL.l ANSI2BSTR(ansi.s)
  size.l=MultiByteToWideChar_(#CP_ACP,0,ansi,-1,0,0)
  Dim unicode.w(size)
  MultiByteToWideChar_(#CP_ACP, 0, ansi, Len(ansi), unicode(), size)
  ProcedureReturn SysAllocString_(@unicode()) 
EndProcedure
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

A bstr is a unicode zero terminated string with a long in front that contains it's length.
The pointer to a bstr however points to the actual string, so the length is
stored at the *pointer-4 memory location.

The important thing is though that it is allocated with SysAllocString_() and
freed with SysFreeString_(). This ensures that when you pass a bstr to a
function, the function is able to free it. So don't just create a memorybank
and put the string in it, that would be wrong.

So the function posted by tonnelier is the right way to go.

A tip though:
> size.l=MultiByteToWideChar_(#CP_ACP,0,ansi,-1,0,0)

This returnes the needed space in bytes for the new string (not including the
termination 0, which are 2 of them for a unicode string)
So if you do "Dim unicode.w(size)", you actually get double the size
than you actually need, because each word is 2 bytes, but "size.l" contains
the full unicode string length in bytes allready.

So "Dim unicode.b(size+2)" is actually enough to store the new string.
Of course the function works well as it is, because a little more memory
never hurts :wink:

Just some info..

Timo
quidquid Latine dictum sit altum videtur
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

>A bstr is a unicode
NOT always!


This is also a BSTR but for ansi:
SysAllocStringByteLen()
Post Reply