dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 47

Thread: how to use BSTR

  1. #16
    Jonathan Wood Guest

    Re: how to use BSTR

    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



  2. #17
    Jonathan Wood Guest

    Re: how to use BSTR

    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



  3. #18
    Damit Senanayake Guest

    Re: how to use BSTR

    "Mike" <m_culley@one.net.au> wrote in message
    news:38f27ec8$1@news.devx.com...
    :
    : 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.


    StrPtr would be just as good...

    It would stop the conversion, but you need to copy the pointer returned (if
    it's Unicode) to a VB string. I believe Mattias did something about this in
    microsoft.public.vb.winapi (Mattias, where are you?)

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



  4. #19
    Damit Senanayake Guest

    Re: how to use BSTR

    "Mike" <m_culley@one.net.au> wrote in message
    news:38f27ec8$1@news.devx.com...
    :
    : 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.


    StrPtr would be just as good...

    It would stop the conversion, but you need to copy the pointer returned (if
    it's Unicode) to a VB string. I believe Mattias did something about this in
    microsoft.public.vb.winapi (Mattias, where are you?)

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



  5. #20
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:

    Cast the BSTR into an LPSTR in C++ (or attach to a
    _bstr_t object). or depending on what you want to do,
    convert in VB using strConv function.



    >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
    >
    >



  6. #21
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:

    Cast the BSTR into an LPSTR in C++ (or attach to a
    _bstr_t object). or depending on what you want to do,
    convert in VB using strConv function.



    >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
    >
    >



  7. #22
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    Hi Jonathan,

    I am passing a vbstring byRef using a declare statement.

    Private Declare Sub testString Lib "testDLL.dll" _
    (str As String)

    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.

    void __stdcall testString(BSTR* str)


    See Dan Appleman's guide to Win32 API. He discusses
    this briefly (I don't know the section off hand, it's in the
    data type section at the end.)
    Matthias also provided a page in this discussion. Finally,
    the help file on BSTR and _bstr_t provided some clues (although
    it was certainly not clear how best to make conversions).

    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.

    You are correct though that conversions are occurring at
    some point between Unicode and ANSI. (I've done it via LPSTR, as
    suggested by Matthias) but it is not the only way,
    and it depends on what you want to do.

    Cheers,

    Brian



    >Brian,
    >
    >> Cast the BSTR into an LPSTR in C++ (or attach to a
    >> _bstr_t object).

    >
    >This doesn't control what VB is passing. Again, have you tested that you

    did
    >this and you still had a Unicode BSTR?
    >
    >> or depending on what you want to do,
    >> convert in VB using strConv function.

    >
    >You should be able to use StrConv to convert to a Byte array and pass the
    >Byte array without conversion but then you are not passing the original
    >BSTR.
    >
    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >
    >
    >



  8. #23
    Brian Leung Guest

    Re: how to use BSTR


    "Jonathan Wood" <jwood@softcircuits.com> wrote:
    Hi Jonathan,

    I am passing a vbstring byRef using a declare statement.

    Private Declare Sub testString Lib "testDLL.dll" _
    (str As String)

    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.

    void __stdcall testString(BSTR* str)


    See Dan Appleman's guide to Win32 API. He discusses
    this briefly (I don't know the section off hand, it's in the
    data type section at the end.)
    Matthias also provided a page in this discussion. Finally,
    the help file on BSTR and _bstr_t provided some clues (although
    it was certainly not clear how best to make conversions).

    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.

    You are correct though that conversions are occurring at
    some point between Unicode and ANSI. (I've done it via LPSTR, as
    suggested by Matthias) but it is not the only way,
    and it depends on what you want to do.

    Cheers,

    Brian



    >Brian,
    >
    >> Cast the BSTR into an LPSTR in C++ (or attach to a
    >> _bstr_t object).

    >
    >This doesn't control what VB is passing. Again, have you tested that you

    did
    >this and you still had a Unicode BSTR?
    >
    >> or depending on what you want to do,
    >> convert in VB using strConv function.

    >
    >You should be able to use StrConv to convert to a Byte array and pass the
    >Byte array without conversion but then you are not passing the original
    >BSTR.
    >
    >--
    >Jonathan Wood
    >SoftCircuits Programming
    >http://www.softcircuits.com
    >
    >
    >



  9. #24
    Jonathan Wood Guest

    Re: how to use BSTR

    Mike,

    > 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.


    Yes, that would work.

    Brian was talking about uses a ByRef declaration though and I don't believe
    you can do this soley in the declaration (without a type library).

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  10. #25
    Jonathan Wood Guest

    Re: how to use BSTR

    Mike,

    > 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.


    Yes, that would work.

    Brian was talking about uses a ByRef declaration though and I don't believe
    you can do this soley in the declaration (without a type library).

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com



  11. #26
    Jonathan Wood Guest

    Re: how to use BSTR

    Brian,

    > Cast the BSTR into an LPSTR in C++ (or attach to a
    > _bstr_t object).


    This doesn't control what VB is passing. Again, have you tested that you did
    this and you still had a Unicode BSTR?

    > or depending on what you want to do,
    > convert in VB using strConv function.


    You should be able to use StrConv to convert to a Byte array and pass the
    Byte array without conversion but then you are not passing the original
    BSTR.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com




  12. #27
    Jonathan Wood Guest

    Re: how to use BSTR

    Brian,

    > Cast the BSTR into an LPSTR in C++ (or attach to a
    > _bstr_t object).


    This doesn't control what VB is passing. Again, have you tested that you did
    this and you still had a Unicode BSTR?

    > or depending on what you want to do,
    > convert in VB using strConv function.


    You should be able to use StrConv to convert to a Byte array and pass the
    Byte array without conversion but then you are not passing the original
    BSTR.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com




  13. #28
    Mattias Sjögren Guest

    Re: how to use BSTR

    Hi Damit,

    >StrPtr would be just as good...


    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,


    >It would stop the conversion, but you need to copy the pointer returned (if
    >it's Unicode) to a VB string.


    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.


    >(Mattias, where are you?)


    Right here :-)


    Mattias


  14. #29
    Mattias Sjögren Guest

    Re: how to use BSTR

    Hi Damit,

    >StrPtr would be just as good...


    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,


    >It would stop the conversion, but you need to copy the pointer returned (if
    >it's Unicode) to a VB string.


    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.


    >(Mattias, where are you?)


    Right here :-)


    Mattias


  15. #30
    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



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