dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 4 123 ... LastLast
Results 1 to 15 of 47

Thread: how to use BSTR

  1. #1
    Brian Leung Guest

    how to use BSTR


    Hi,

    I'm not sure if this is the right newsgroup to ask, but
    does any one know how to use BSTRs?
    I want to pass a visual basic string by reference to a DLL written in C++.
    The visual basic string passes OK, but becomes garbage in C++.
    I can convert it to char *, or to _bstr_t to get the proper characters.
    But this doesn't help when I want to return the modified string.


    Thanks,

    Brian

  2. #2
    Mattias Sjögren Guest

    Re: how to use BSTR

    >I'm not sure if this is the right newsgroup to ask, but
    >does any one know how to use BSTRs?
    >I want to pass a visual basic string by reference to a DLL written in C++.
    >The visual basic string passes OK, but becomes garbage in C++.
    >I can convert it to char *, or to _bstr_t to get the proper characters.
    >But this doesn't help when I want to return the modified string.


    Brian,

    Remember that VB converts the string to ANSI before calling the
    function, and then back to Unicode after the call. So you should treat
    it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    expects you to return.


    Mattias

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

  3. #3
    Mattias Sjögren Guest

    Re: how to use BSTR

    >I'm not sure if this is the right newsgroup to ask, but
    >does any one know how to use BSTRs?
    >I want to pass a visual basic string by reference to a DLL written in C++.
    >The visual basic string passes OK, but becomes garbage in C++.
    >I can convert it to char *, or to _bstr_t to get the proper characters.
    >But this doesn't help when I want to return the modified string.


    Brian,

    Remember that VB converts the string to ANSI before calling the
    function, and then back to Unicode after the call. So you should treat
    it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    expects you to return.


    Mattias

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

  4. #4
    Brian Leung Guest

    Re: how to use BSTR


    mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >>I'm not sure if this is the right newsgroup to ask, but
    >>does any one know how to use BSTRs?
    >>I want to pass a visual basic string by reference to a DLL written in C++.
    >>The visual basic string passes OK, but becomes garbage in C++.
    >>I can convert it to char *, or to _bstr_t to get the proper characters.
    >>But this doesn't help when I want to return the modified string.

    >
    >Brian,
    >
    >Remember that VB converts the string to ANSI before calling the
    >function, and then back to Unicode after the call. So you should treat
    >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    >expects you to return.
    >
    >

    Hi Mattias,

    LPSTR works, but I wanted to pass it as a BSTR.
    Problem with LPSTR is that it is fixed length, and I wanted
    to keep the dynamic properties of VB strings, and potentially
    pass the BSTR to other functions within VB.






  5. #5
    Brian Leung Guest

    Re: how to use BSTR


    mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >>I'm not sure if this is the right newsgroup to ask, but
    >>does any one know how to use BSTRs?
    >>I want to pass a visual basic string by reference to a DLL written in C++.
    >>The visual basic string passes OK, but becomes garbage in C++.
    >>I can convert it to char *, or to _bstr_t to get the proper characters.
    >>But this doesn't help when I want to return the modified string.

    >
    >Brian,
    >
    >Remember that VB converts the string to ANSI before calling the
    >function, and then back to Unicode after the call. So you should treat
    >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    >expects you to return.
    >
    >

    Hi Mattias,

    LPSTR works, but I wanted to pass it as a BSTR.
    Problem with LPSTR is that it is fixed length, and I wanted
    to keep the dynamic properties of VB strings, and potentially
    pass the BSTR to other functions within VB.






  6. #6
    Mattias Sjögren Guest

    Re: how to use BSTR

    Brian,

    >LPSTR works, but I wanted to pass it as a BSTR.


    It is a BSTR too, since it stores the string length before the
    character data, but it's probably better to cast it as a LPSTR since
    it contains ANSI chars and not Unicode as "real" BSTRs do.


    >Problem with LPSTR is that it is fixed length, and I wanted
    >to keep the dynamic properties of VB strings, and potentially
    >pass the BSTR to other functions within VB.


    You can still modify the string using the Sys*** APIs, like
    SysReAllocString.


    If you have any VB5 CDs available, you should take a look at the
    Vb5dll.doc file there. It has detailed descriptions of all this. The
    VB4 version of the file can be downloaded from

    http://support.microsoft.com/support.../Q150/7/05.asp


    Mattias

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

  7. #7
    Mattias Sjögren Guest

    Re: how to use BSTR

    Brian,

    >LPSTR works, but I wanted to pass it as a BSTR.


    It is a BSTR too, since it stores the string length before the
    character data, but it's probably better to cast it as a LPSTR since
    it contains ANSI chars and not Unicode as "real" BSTRs do.


    >Problem with LPSTR is that it is fixed length, and I wanted
    >to keep the dynamic properties of VB strings, and potentially
    >pass the BSTR to other functions within VB.


    You can still modify the string using the Sys*** APIs, like
    SysReAllocString.


    If you have any VB5 CDs available, you should take a look at the
    Vb5dll.doc file there. It has detailed descriptions of all this. The
    VB4 version of the file can be downloaded from

    http://support.microsoft.com/support.../Q150/7/05.asp


    Mattias

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

  8. #8
    Brian Leung Guest

    Re: how to use BSTR


    Thanks Mattias,

    it works well.


    Cheers,

    Brian




    mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >Brian,
    >
    >>LPSTR works, but I wanted to pass it as a BSTR.

    >
    >It is a BSTR too, since it stores the string length before the
    >character data, but it's probably better to cast it as a LPSTR since
    >it contains ANSI chars and not Unicode as "real" BSTRs do.
    >
    >
    >>Problem with LPSTR is that it is fixed length, and I wanted
    >>to keep the dynamic properties of VB strings, and potentially
    >>pass the BSTR to other functions within VB.

    >
    >You can still modify the string using the Sys*** APIs, like
    >SysReAllocString.
    >
    >
    >If you have any VB5 CDs available, you should take a look at the
    >Vb5dll.doc file there. It has detailed descriptions of all this. The
    >VB4 version of the file can be downloaded from
    >
    >http://support.microsoft.com/support.../Q150/7/05.asp
    >
    >
    >Mattias
    >
    >__________________________________________________
    >Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups



  9. #9
    Brian Leung Guest

    Re: how to use BSTR


    Thanks Mattias,

    it works well.


    Cheers,

    Brian




    mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >Brian,
    >
    >>LPSTR works, but I wanted to pass it as a BSTR.

    >
    >It is a BSTR too, since it stores the string length before the
    >character data, but it's probably better to cast it as a LPSTR since
    >it contains ANSI chars and not Unicode as "real" BSTRs do.
    >
    >
    >>Problem with LPSTR is that it is fixed length, and I wanted
    >>to keep the dynamic properties of VB strings, and potentially
    >>pass the BSTR to other functions within VB.

    >
    >You can still modify the string using the Sys*** APIs, like
    >SysReAllocString.
    >
    >
    >If you have any VB5 CDs available, you should take a look at the
    >Vb5dll.doc file there. It has detailed descriptions of all this. The
    >VB4 version of the file can be downloaded from
    >
    >http://support.microsoft.com/support.../Q150/7/05.asp
    >
    >
    >Mattias
    >
    >__________________________________________________
    >Mattias Sjögren (MCP) - mattiass @ hem.passagen.se
    > VB+ http://hem.spray.se/mattias.sjogren/
    > Please send questions/replies to the newsgroups



  10. #10
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    >The only way to have VB pass the original BSTR is to use a type library

    and
    >specify that's what your function accepts.
    >


    True enough in that winAPI doesn't usually use BSTR.
    However, you can write your own DLLs that use BSTR, and then pass
    visual basic type strings byRef to the function.

    It works, but I still have to play around with conversions
    between Unicode and ANSI. I'd like to localize conversions
    within the DLL itself if possible, so that I don't have to
    change VB code.

    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
    >news:38f1eec2$1@news.devx.com...
    >>
    >> mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >> >>I'm not sure if this is the right newsgroup to ask, but
    >> >>does any one know how to use BSTRs?
    >> >>I want to pass a visual basic string by reference to a DLL written in

    >C++.
    >> >>The visual basic string passes OK, but becomes garbage in C++.
    >> >>I can convert it to char *, or to _bstr_t to get the proper characters.
    >> >>But this doesn't help when I want to return the modified string.
    >> >
    >> >Brian,
    >> >
    >> >Remember that VB converts the string to ANSI before calling the
    >> >function, and then back to Unicode after the call. So you should treat
    >> >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    >> >expects you to return.
    >> >
    >> >

    >> Hi Mattias,
    >>
    >> LPSTR works, but I wanted to pass it as a BSTR.
    >> Problem with LPSTR is that it is fixed length, and I wanted
    >> to keep the dynamic properties of VB strings, and potentially
    >> pass the BSTR to other functions within VB.
    >>
    >>
    >>
    >>
    >>

    >
    >



  11. #11
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    >The only way to have VB pass the original BSTR is to use a type library

    and
    >specify that's what your function accepts.
    >


    True enough in that winAPI doesn't usually use BSTR.
    However, you can write your own DLLs that use BSTR, and then pass
    visual basic type strings byRef to the function.

    It works, but I still have to play around with conversions
    between Unicode and ANSI. I'd like to localize conversions
    within the DLL itself if possible, so that I don't have to
    change VB code.

    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
    >news:38f1eec2$1@news.devx.com...
    >>
    >> mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    >> >>I'm not sure if this is the right newsgroup to ask, but
    >> >>does any one know how to use BSTRs?
    >> >>I want to pass a visual basic string by reference to a DLL written in

    >C++.
    >> >>The visual basic string passes OK, but becomes garbage in C++.
    >> >>I can convert it to char *, or to _bstr_t to get the proper characters.
    >> >>But this doesn't help when I want to return the modified string.
    >> >
    >> >Brian,
    >> >
    >> >Remember that VB converts the string to ANSI before calling the
    >> >function, and then back to Unicode after the call. So you should treat
    >> >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    >> >expects you to return.
    >> >
    >> >

    >> Hi Mattias,
    >>
    >> LPSTR works, but I wanted to pass it as a BSTR.
    >> Problem with LPSTR is that it is fixed length, and I wanted
    >> to keep the dynamic properties of VB strings, and potentially
    >> pass the BSTR to other functions within VB.
    >>
    >>
    >>
    >>
    >>

    >
    >



  12. #12
    Jonathan Wood Guest

    Re: how to use BSTR

    The only way to have VB pass the original BSTR is to use a type library and
    specify that's what your function accepts.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
    news:38f1eec2$1@news.devx.com...
    >
    > mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    > >>I'm not sure if this is the right newsgroup to ask, but
    > >>does any one know how to use BSTRs?
    > >>I want to pass a visual basic string by reference to a DLL written in

    C++.
    > >>The visual basic string passes OK, but becomes garbage in C++.
    > >>I can convert it to char *, or to _bstr_t to get the proper characters.
    > >>But this doesn't help when I want to return the modified string.

    > >
    > >Brian,
    > >
    > >Remember that VB converts the string to ANSI before calling the
    > >function, and then back to Unicode after the call. So you should treat
    > >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    > >expects you to return.
    > >
    > >

    > Hi Mattias,
    >
    > LPSTR works, but I wanted to pass it as a BSTR.
    > Problem with LPSTR is that it is fixed length, and I wanted
    > to keep the dynamic properties of VB strings, and potentially
    > pass the BSTR to other functions within VB.
    >
    >
    >
    >
    >




  13. #13
    Jonathan Wood Guest

    Re: how to use BSTR

    The only way to have VB pass the original BSTR is to use a type library and
    specify that's what your function accepts.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    Brian Leung <bleung@zoo.cam.ac.uk> wrote in message
    news:38f1eec2$1@news.devx.com...
    >
    > mattiass.dont.want.spam@hem.passagen.se (Mattias Sjögren) wrote:
    > >>I'm not sure if this is the right newsgroup to ask, but
    > >>does any one know how to use BSTRs?
    > >>I want to pass a visual basic string by reference to a DLL written in

    C++.
    > >>The visual basic string passes OK, but becomes garbage in C++.
    > >>I can convert it to char *, or to _bstr_t to get the proper characters.
    > >>But this doesn't help when I want to return the modified string.

    > >
    > >Brian,
    > >
    > >Remember that VB converts the string to ANSI before calling the
    > >function, and then back to Unicode after the call. So you should treat
    > >it as an ANSI BSTR (or LPSTR) on the C++ side, and that's also what VB
    > >expects you to return.
    > >
    > >

    > Hi Mattias,
    >
    > LPSTR works, but I wanted to pass it as a BSTR.
    > Problem with LPSTR is that it is fixed length, and I wanted
    > to keep the dynamic properties of VB strings, and potentially
    > pass the BSTR to other functions within VB.
    >
    >
    >
    >
    >




  14. #14
    Mike Guest

    Re: how to use BSTR


    I have not tried this, but would it be possible to pass the pointer to the
    string into the C function:

    Declare MyDLLFunc lib "XYZ.dll" (ByVal strABC as long)

    MyDLLFunc VarPtr(strX)

    Maybe this would stop the conversion.

    Mike


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    >Brian,
    >
    >> True enough in that winAPI doesn't usually use BSTR.
    >> However, you can write your own DLLs that use BSTR, and then pass
    >> visual basic type strings byRef to the function.

    >
    >BSTRs are Unicode. I think you are mistaken that you can have VB pass a
    >Unicode BSTR to a DLL without a type library. Is this what you are claiming?
    >
    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >
    >



  15. #15
    Mike Guest

    Re: how to use BSTR


    I have not tried this, but would it be possible to pass the pointer to the
    string into the C function:

    Declare MyDLLFunc lib "XYZ.dll" (ByVal strABC as long)

    MyDLLFunc VarPtr(strX)

    Maybe this would stop the conversion.

    Mike


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    >Brian,
    >
    >> True enough in that winAPI doesn't usually use BSTR.
    >> However, you can write your own DLLs that use BSTR, and then pass
    >> visual basic type strings byRef to the function.

    >
    >BSTRs are Unicode. I think you are mistaken that you can have VB pass a
    >Unicode BSTR to a DLL without a type library. Is this what you are claiming?
    >
    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >
    >



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