dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 47

Thread: how to use BSTR

  1. #31
    Damit Senanayake Guest

    Re: how to use BSTR

    Hi Mattias,

    : Yes, but StrPtr sould be similar to passing the string ByVal - it will
    : be a BSTR on the C++ side. VarPtr is similar to passing it ByRef,
    : giving a BSTR* on the C++ side. Both the *Ptr functions will prevent
    : the character conversion though, so it will be "true" Unicode BSTRs,
    : not ANSI,

    But his (Mike's) declaration is already ByVal, so I don't know that it makes
    a difference... As another question, does StrPtr pass a BSTR or a const BSTR
    ?

    : But in this case, no string pointers are returned (as the function
    : return value). The C++ code just modifies the string using the BSTR
    : pointer, and VB will never know about it.

    Hm. That must have been for the other case we were talking about, no? :-)

    : Right here :-)

    OK... OE has gone crazy in msnews, and for a minute I thought it was crazy
    over here too :-(


    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  2. #32
    Damit Senanayake Guest

    Re: how to use BSTR

    Hi Brian,

    Part of the confusion (I think) is that there are ANSI and Unicode BSTRs
    available. Both, however, have the same (C++) type BSTR.

    ANSI BSTRs are allocated by SysAllocStringByteLen.
    Unicode BSTRs are allocated by SysAllocString (does SysAllocStringLen create
    Unicode BSTRs?).

    : in the C++ dll, I am receiving the string argument as
    : BSTR* s. I can vary the string length without preassigning
    : the length in VB.

    When you declare the function in VB as taking a String, VB converts the
    passed String (which is a Unicode BSTR under the hood) into an ANSI BSTR.
    Thus, by the time the BSTR arrives in your C(++) function, it's an ANSI BSTR
    and there's no way of getting the original Unicode BSTR.

    However, if you declare the function as taking a Long, and then pass
    VarPtr/StrPtr(<string>), VB sends the Unicode BSTR without translation, and
    you receive it as a Unicode BSTR.

    : I haven't checked this yet, but eventually I will see if I can
    : attach the BSTR to a CSTRING, which presumably has similar
    : functions as VB. Nevertheless, it is possible to attach the BSTR
    : to a _bstr_t object, modify it, and pass it back to VB.

    Also, attaching the BSTR to a CString, _bstr_t, or CComBSTR won't convert an
    ANSI BSTR into a Unicode BSTR, although to be sure I'll have to look at the
    constructors and the Attach() methods... and passing back the BSTR (wrapped
    or otherwise) gives you back an ANSI BSTR if that's what you received.

    I don't know of any function that will, given a BSTR, tell you if it's
    Unicode or ANSI...
    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  3. #33
    Damit Senanayake Guest

    Re: how to use BSTR

    Hi Brian,

    Part of the confusion (I think) is that there are ANSI and Unicode BSTRs
    available. Both, however, have the same (C++) type BSTR.

    ANSI BSTRs are allocated by SysAllocStringByteLen.
    Unicode BSTRs are allocated by SysAllocString (does SysAllocStringLen create
    Unicode BSTRs?).

    : in the C++ dll, I am receiving the string argument as
    : BSTR* s. I can vary the string length without preassigning
    : the length in VB.

    When you declare the function in VB as taking a String, VB converts the
    passed String (which is a Unicode BSTR under the hood) into an ANSI BSTR.
    Thus, by the time the BSTR arrives in your C(++) function, it's an ANSI BSTR
    and there's no way of getting the original Unicode BSTR.

    However, if you declare the function as taking a Long, and then pass
    VarPtr/StrPtr(<string>), VB sends the Unicode BSTR without translation, and
    you receive it as a Unicode BSTR.

    : I haven't checked this yet, but eventually I will see if I can
    : attach the BSTR to a CSTRING, which presumably has similar
    : functions as VB. Nevertheless, it is possible to attach the BSTR
    : to a _bstr_t object, modify it, and pass it back to VB.

    Also, attaching the BSTR to a CString, _bstr_t, or CComBSTR won't convert an
    ANSI BSTR into a Unicode BSTR, although to be sure I'll have to look at the
    constructors and the Attach() methods... and passing back the BSTR (wrapped
    or otherwise) gives you back an ANSI BSTR if that's what you received.

    I don't know of any function that will, given a BSTR, tell you if it's
    Unicode or ANSI...
    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  4. #34
    Eduardo A. Morcillo Guest

    Re: how to use BSTR

    > Part of the confusion (I think) is that there are ANSI and Unicode BSTRs
    > available. Both, however, have the same (C++) type BSTR.


    Looking at the header files will make things clear:

    /* OLECHAR is Unicode if _WIN32 is defined but not OLE2ANSI */

    #if defined(_WIN32) && !defined(OLE2ANSI)

    typedef WCHAR OLECHAR;

    typedef /* [string] */ OLECHAR __RPC_FAR *LPOLESTR;

    typedef /* [string] */ const OLECHAR __RPC_FAR *LPCOLESTR;

    #define OLESTR(str) L##str

    #else

    typedef char OLECHAR;
    typedef LPSTR LPOLESTR;
    typedef LPCSTR LPCOLESTR;

    #define OLESTR(str) str

    #endif

    typedef /* [wire_marshal] */ OLECHAR __RPC_FAR *BSTR;

    > ANSI BSTRs are allocated by SysAllocStringByteLen.
    > Unicode BSTRs are allocated by SysAllocString (does SysAllocStringLen

    create
    > Unicode BSTRs?).


    No. Both uses OLECHAR *:

    WINOLEAUTAPI_(BSTR) SysAllocString(const OLECHAR *);
    WINOLEAUTAPI_(BSTR) SysAllocStringLen(const OLECHAR *, UINT);

    HTH
    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  5. #35
    Eduardo A. Morcillo Guest

    Re: how to use BSTR

    > Part of the confusion (I think) is that there are ANSI and Unicode BSTRs
    > available. Both, however, have the same (C++) type BSTR.


    Looking at the header files will make things clear:

    /* OLECHAR is Unicode if _WIN32 is defined but not OLE2ANSI */

    #if defined(_WIN32) && !defined(OLE2ANSI)

    typedef WCHAR OLECHAR;

    typedef /* [string] */ OLECHAR __RPC_FAR *LPOLESTR;

    typedef /* [string] */ const OLECHAR __RPC_FAR *LPCOLESTR;

    #define OLESTR(str) L##str

    #else

    typedef char OLECHAR;
    typedef LPSTR LPOLESTR;
    typedef LPCSTR LPCOLESTR;

    #define OLESTR(str) str

    #endif

    typedef /* [wire_marshal] */ OLECHAR __RPC_FAR *BSTR;

    > ANSI BSTRs are allocated by SysAllocStringByteLen.
    > Unicode BSTRs are allocated by SysAllocString (does SysAllocStringLen

    create
    > Unicode BSTRs?).


    No. Both uses OLECHAR *:

    WINOLEAUTAPI_(BSTR) SysAllocString(const OLECHAR *);
    WINOLEAUTAPI_(BSTR) SysAllocStringLen(const OLECHAR *, UINT);

    HTH
    --
    Eduardo A. Morcillo
    http://www.domaindlx.com/e_morcillo



  6. #36
    Damit Senanayake Guest

    Re: how to use BSTR

    Hi Eduardo,

    : Looking at the header files will make things clear:
    :
    : /* OLECHAR is Unicode if _WIN32 is defined but not OLE2ANSI */

    [etc.]

    Wasn't OLE2ANSI some kind of DLL that was supposed to translate behind the
    scenes from Unicode to ANSI? (Correct me if I'm wrong, I saw this in
    HCVB...) IOW, this was a thing that got junked during the move to 32-bit
    when they made the decision that OLE/COM were to be Unicode (which is why
    Win95 has *some* Unicode support -- they required it for OLE?).

    The only significant mention I could find was this (objbase.h):

    #ifdef _MAC
    #if !defined(OLE2ANSI)
    #define OLE2ANSI
    #endif
    #endif

    Also, the docs for SysAllocStringByteLen:

    >>

    Takes an ANSI string as input, and returns a BSTR that contains an ANSI
    string. Does not perform any ANSI-to-Unicode translation.
    <<

    So apparently I was partially correct -- there are BSTRs that contain
    Unicode strings and BSTRs that contain ANSI strings, not Unicode BSTRs and
    ANSI BSTRs.

    Incidentally, SysAllocStringLen doesn't convert from ANSI to Unicode either:

    >>

    Note: This function does not convert a char * string into a Unicode BSTR.
    <<

    Also, check out the docs for SysAllocString's sz parameter:

    >>

    sz
    A zero-terminated string to copy. The sz parameter must be a Unicode string
    in 32-bit applications, and an ANSI string in 16-bit applications. The
    argument sz may be NULL.
    <<

    So it appears that regardless of the value of OLE2ANSI under Win32, BSTRs
    are Unicode...

    : No. Both uses OLECHAR *:

    .... which translates into WCHAR i.e. Unicode.

    BTW Eduardo, thanks for this information...
    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  7. #37
    Damit Senanayake Guest

    Re: how to use BSTR

    Hi Eduardo,

    : Looking at the header files will make things clear:
    :
    : /* OLECHAR is Unicode if _WIN32 is defined but not OLE2ANSI */

    [etc.]

    Wasn't OLE2ANSI some kind of DLL that was supposed to translate behind the
    scenes from Unicode to ANSI? (Correct me if I'm wrong, I saw this in
    HCVB...) IOW, this was a thing that got junked during the move to 32-bit
    when they made the decision that OLE/COM were to be Unicode (which is why
    Win95 has *some* Unicode support -- they required it for OLE?).

    The only significant mention I could find was this (objbase.h):

    #ifdef _MAC
    #if !defined(OLE2ANSI)
    #define OLE2ANSI
    #endif
    #endif

    Also, the docs for SysAllocStringByteLen:

    >>

    Takes an ANSI string as input, and returns a BSTR that contains an ANSI
    string. Does not perform any ANSI-to-Unicode translation.
    <<

    So apparently I was partially correct -- there are BSTRs that contain
    Unicode strings and BSTRs that contain ANSI strings, not Unicode BSTRs and
    ANSI BSTRs.

    Incidentally, SysAllocStringLen doesn't convert from ANSI to Unicode either:

    >>

    Note: This function does not convert a char * string into a Unicode BSTR.
    <<

    Also, check out the docs for SysAllocString's sz parameter:

    >>

    sz
    A zero-terminated string to copy. The sz parameter must be a Unicode string
    in 32-bit applications, and an ANSI string in 16-bit applications. The
    argument sz may be NULL.
    <<

    So it appears that regardless of the value of OLE2ANSI under Win32, BSTRs
    are Unicode...

    : No. Both uses OLECHAR *:

    .... which translates into WCHAR i.e. Unicode.

    BTW Eduardo, thanks for this information...
    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  8. #38
    Damit Senanayake Guest

    Re: how to use BSTR

    : Wasn't OLE2ANSI some kind of DLL that was supposed to translate behind the
    : scenes from Unicode to ANSI? (Correct me if I'm wrong, I saw this in
    : HCVB...)

    I found the section; actually it's from Bruce M's cpp4vb series of articles
    (this is from Article 3: Strings the OLE Way):

    >>

    Note: Versions of Visual C++ before 4.0 had a DLL called OLE2ANSI that
    automatically translated OLE Unicode strings to ANSI strings behind the
    scenes. This optimistic DLL tried to do the impossible. It was indeed
    pleasant to have the bothersome details taken care of, but performance-wise,
    users were living in a fool's paradise. OLE2ANSI is history now, although
    conditional symbols for it still exist in the OLE include files. The OLECHAR
    type, rather than the WCHAR type, was used in OLE prototypes so that it
    could be transformed into the CHAR type by this DLL. Do not define the
    symbol OLE2ANSI in the hopes that OLE strings will magically transform
    themselves into ANSI strings. There is no Santa Claus.
    <<

    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  9. #39
    Damit Senanayake Guest

    Re: how to use BSTR

    : Wasn't OLE2ANSI some kind of DLL that was supposed to translate behind the
    : scenes from Unicode to ANSI? (Correct me if I'm wrong, I saw this in
    : HCVB...)

    I found the section; actually it's from Bruce M's cpp4vb series of articles
    (this is from Article 3: Strings the OLE Way):

    >>

    Note: Versions of Visual C++ before 4.0 had a DLL called OLE2ANSI that
    automatically translated OLE Unicode strings to ANSI strings behind the
    scenes. This optimistic DLL tried to do the impossible. It was indeed
    pleasant to have the bothersome details taken care of, but performance-wise,
    users were living in a fool's paradise. OLE2ANSI is history now, although
    conditional symbols for it still exist in the OLE include files. The OLECHAR
    type, rather than the WCHAR type, was used in OLE prototypes so that it
    could be transformed into the CHAR type by this DLL. Do not define the
    symbol OLE2ANSI in the hopes that OLE strings will magically transform
    themselves into ANSI strings. There is no Santa Claus.
    <<

    --
    Damit Senanayake | damit@mvps.org | http://members.xoom.com/damit
    Please reply to newsgroups, not by e-mail. | ICQ: 6930718



  10. #40
    Mattias Sjögren Guest

    Re: how to use BSTR

    Damit,

    >: Yes, but StrPtr sould be similar to passing the string ByVal - it will
    >: be a BSTR on the C++ side. VarPtr is similar to passing it ByRef,
    >: giving a BSTR* on the C++ side. Both the *Ptr functions will prevent
    >: the character conversion though, so it will be "true" Unicode BSTRs,
    >: not ANSI,
    >
    >But his (Mike's) declaration is already ByVal, so I don't know that it makes
    >a difference...


    What I meant was that StrPtr() gives you a pointer to the character
    array in memory (BSTR), while VarPtr() gives you a pointer to the
    string pointer (BSTR*). And this is what you get on the C++ side too,
    assuming Mike pass this value ByVal to the DLL. If he declares it as
    ByRef, the result would be BSTR* and BSTR** on the C++ side... uhh..
    I think I'll shut up now, this is getting more and more confusing :-)


    >As another question, does StrPtr pass a BSTR or a const BSTR


    What exactly does const do in C++? Isn't it just something that gets
    evaluated at compile time to check that you don't accidently modify
    memory you shouldn't. Or does the memory actually get temporarily
    write protected somehow (using VirtualProtect or something)?


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  11. #41
    Mattias Sjögren Guest

    Re: how to use BSTR

    Damit,

    >: Yes, but StrPtr sould be similar to passing the string ByVal - it will
    >: be a BSTR on the C++ side. VarPtr is similar to passing it ByRef,
    >: giving a BSTR* on the C++ side. Both the *Ptr functions will prevent
    >: the character conversion though, so it will be "true" Unicode BSTRs,
    >: not ANSI,
    >
    >But his (Mike's) declaration is already ByVal, so I don't know that it makes
    >a difference...


    What I meant was that StrPtr() gives you a pointer to the character
    array in memory (BSTR), while VarPtr() gives you a pointer to the
    string pointer (BSTR*). And this is what you get on the C++ side too,
    assuming Mike pass this value ByVal to the DLL. If he declares it as
    ByRef, the result would be BSTR* and BSTR** on the C++ side... uhh..
    I think I'll shut up now, this is getting more and more confusing :-)


    >As another question, does StrPtr pass a BSTR or a const BSTR


    What exactly does const do in C++? Isn't it just something that gets
    evaluated at compile time to check that you don't accidently modify
    memory you shouldn't. Or does the memory actually get temporarily
    write protected somehow (using VirtualProtect or something)?


    Mattias

    __________________________________________________
    Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    VB+ http://hem.spray.se/mattias.sjogren/
    Please send questions/replies to the newsgroups

  12. #42
    Mattias Sjögren Guest

    Re: how to use BSTR

    Hi Damit,

    >I don't know of any function that will, given a BSTR, tell you if it's Unicode or ANSI...


    I think you could use IsTextUnicode for this. But I've never used the
    function, so I don't know exactly what it does and how accurate it is.

    Testing...

    Private Declare Function IsTextUnicode Lib "advapi32" ( _
    lpBuffer As Any, _
    ByVal cb As Long, _
    lpi As Any) As Long

    --------------

    Dim s As String, n As Long

    s = "Mattias Sjögren"
    n = Len(s)
    Debug.Print CBool(IsTextUnicode(ByVal s, n, ByVal 0&))
    Debug.Print CBool(IsTextUnicode(ByVal StrPtr(s), n * 2, ByVal 0&))


    Seems to work ok...


    Mattias


  13. #43
    Mattias Sjögren Guest

    Re: how to use BSTR

    Hi Damit,

    >I don't know of any function that will, given a BSTR, tell you if it's Unicode or ANSI...


    I think you could use IsTextUnicode for this. But I've never used the
    function, so I don't know exactly what it does and how accurate it is.

    Testing...

    Private Declare Function IsTextUnicode Lib "advapi32" ( _
    lpBuffer As Any, _
    ByVal cb As Long, _
    lpi As Any) As Long

    --------------

    Dim s As String, n As Long

    s = "Mattias Sjögren"
    n = Len(s)
    Debug.Print CBool(IsTextUnicode(ByVal s, n, ByVal 0&))
    Debug.Print CBool(IsTextUnicode(ByVal StrPtr(s), n * 2, ByVal 0&))


    Seems to work ok...


    Mattias


  14. #44
    Brian Leung Guest

    Re: how to use BSTR


    Const gets evaluated at compile time. Apparently, it is considered good programming
    practice to use.
    Also, you can't pass a const argument to non-const parameter (I think). I
    don't know if this would
    have any effect for the VB environment though.

    Brian

    >
    >What exactly does const do in C++? Isn't it just something that gets
    >evaluated at compile time to check that you don't accidently modify
    >memory you shouldn't. Or does the memory actually get temporarily
    >write protected somehow (using VirtualProtect or something)?
    >
    >
    >Mattias
    >
    >__________________________________________________
    >Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups



  15. #45
    Brian Leung Guest

    Re: how to use BSTR


    Const gets evaluated at compile time. Apparently, it is considered good programming
    practice to use.
    Also, you can't pass a const argument to non-const parameter (I think). I
    don't know if this would
    have any effect for the VB environment though.

    Brian

    >
    >What exactly does const do in C++? Isn't it just something that gets
    >evaluated at compile time to check that you don't accidently modify
    >memory you shouldn't. Or does the memory actually get temporarily
    >write protected somehow (using VirtualProtect or something)?
    >
    >
    >Mattias
    >
    >__________________________________________________
    >Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center