How can I convert a string to BSTR (binary string)?
Thanks in advance.
convert string to BSTR
Godd samples when asking ie UNI2ANSI on this forum :
viewtopic.php?t=10053&start=0&postdays= ... t=uni2ansi
Rgrds
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.
Problems solved.
for information :
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
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
Just some info..
Timo
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

Just some info..
Timo
quidquid Latine dictum sit altum videtur
-
- Addict
- Posts: 1073
- Joined: Fri Apr 25, 2003 11:13 pm
- Location: Netherlands
- Contact: