CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: BSTR Blues

  1. #1

    BSTR Blues

    Hi,

    This may be a beginner question, but I am having problems. I am creating my first, so simple, ATL COM object with MFC support in VC 5.0. I have methods and properties which as in/out parameters utilize BSTR FAR* as strings (because I cannot use CString for VB to work etc.

    1. First problem is converting BSTR FAR* to a CString (it just become garbage)
    2. Setting a BSTR FAR* value as an out parameter for properties from a CString. I used AllocSysString casted to BSTR * but it filled the BSTR with garbage.
    3. Similarly, all my long * parameters are suffering the same fate (garbage).

    Can anyone tell me how to do parameter passing where the values are not garbled ? Should I be using VARIANT and VARIANTARG ? If so how ?

    Thanks for any help in advance,

    Simon




  2. #2
    Join Date
    May 1999
    Posts
    6

    Re: BSTR Blues

    Hi,

    Try this:
    1. CString can be constructed (with the constructor!) getting a BSTR parameter. Write something like this:

    CString str;
    BSTR bstr;
    str = CString(bstr);
    ...

    2. You should make a copy of what AllocSysString returns (allocate space and copy).

    Hope it works!


  3. #3
    Join Date
    May 1999
    Posts
    3

    Re: BSTR Blues

    Watch out for [out] IDL notation.

    You'll need to pass addres of placeholder types.

    BSTR bname = NULL;

    // from CString

    bname = cstringname.AllocSysString();

    psecuredptr->Login( bname );


    NOTE: for a return value

    BSTR bname = NULL;

    psomeCOMptr->GetBstrval( &bname );

    CString name = bname;


    Like wise of long's
    long lval;
    psomeBinCOMCall->GetLong( &lval );


    This '&' (address of operator) means a location
    in memory to place the result.

    Try it.




  4. #4
    Join Date
    May 1999
    Posts
    3

    Re: BSTR Blues

    OOPPS:

    When using CSTRING::AllocSysString()
    after using the BSTR, SysFreeString( bstr );

    Can use a CComVariant more easily for [in] params

    SomeClientApp::Login( CString& name )
    {
    CComVariant var;
    var = name;
    psecurity->Login( var.bstrVal );
    // No need to clean up the bstr, the CComVariant does that automatically

    }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured