CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > CodeGuru Technical FAQs > CodeGuru Individual FAQs
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    CodeGuru Individual FAQs The indivdual FAQs for CodeGuru. See the specific Topic FAQ forums for index pages and links to these Frequently Asked/Answered Questions.

    Reply
     
    Thread Tools Search this Thread Display Modes
      #1    
    Old February 14th, 2003, 03:55 AM
    Gabriel Fleseriu's Avatar
    Gabriel Fleseriu Gabriel Fleseriu is offline
    Moderator/Microsoft MVP
     
    Join Date: Jun 2001
    Location: Switzerland
    Posts: 4,443
    Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)Gabriel Fleseriu is a glorious beacon of light (400+)
    MFC String: How to convert between a 'CString' and a 'BSTR'?

    Q: How to convert between a 'CString' and a 'BSTR'?

    A:

    'CString' to 'BSTR':

    Use the AllocSysString member function of the CString:

    Code:
    CString cs("Hello");
    BSTR bstr = cs.AllocSysString();
    If you pass the 'BSTR' to some OLE function, this will normally free the 'BSTR' memory when done with it.

    If you use the 'BSTR' by yourself, dont forget to call '::SysFreeString()' when you're done with it.

    Code:
    ::SysFreeString(bstr);
    'BSTR' to 'CString':

    You will mostly need this when you have some OLE function that returns a 'BSTR'. Such an OLE Function will basically do something like this:

    Code:
    HRESULT SomeOLEFunction(BSTR& bstr)
    {
      bstr = ::SysAllocString(L"Hello");
      return S_OK;
    }
    Use a temporary variable of the type '_bstr_t' to wrap the 'BSTR'. This way you handle both the 208 and make sure that you have no memory leak:
    Code:
    BSTR bstr;
    SomeOLEFunction(bstr);
    _bstr_t tmp(bstr, FALSE);   //wrap the BSTR
    CString cs(static_cast<const char*>(tmp));  //convert it
    AfxMessageBox(cs, MB_OK, 0);
    // when tmp goes out of scope it will free the BSTRs memory
    Note, that this won't work in a UNICODE build.



    Last edited by Andreas Masur; July 24th, 2005 at 07:48 AM.
    Reply With Quote
      #2    
    Old February 18th, 2005, 10:11 AM
    rhaggard rhaggard is offline
    Junior Member
     
    Join Date: Jan 2005
    Posts: 1
    rhaggard is an unknown quantity at this point (<10)
    Re: MFC String: How to convert between a 'CString' and a 'BSTR'?

    This statement is incorrect: "If you pass the BSTR to some OLE function, this will normally free the BSTRs memory when done with it."

    The general rule to COM resource is, if you allocate it then you release it. The only exception is when a value is passed over a COM interface as an OUT param. In that case, the receiver of the value is responsible for releasing the resource.

    This sort of mistake is particularly onerous because the memory leaked is not associated with the process that leaked it. The only way that this leak is recovered is by reboot of the system.



    Last edited by Andreas Masur; July 24th, 2005 at 07:49 AM.
    Reply With Quote
      #3    
    Old March 20th, 2005, 12:50 AM
    Sahir's Avatar
    Sahir Sahir is offline
    Senior Member
     
    Join Date: Aug 2002
    Location: Dubai
    Posts: 1,181
    Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)Sahir is a glorious beacon of light (400+)
    Re: MFC String: How to convert between a 'CString' and a 'BSTR'?

    Code:
    #include <comutil.h>
    
    BSTR bstr;
    SomeOLEFunction(bstr);
    CString cs(com_util::ConvertBSTRToString(bstr)); 
    AfxMessageBox(cs, MB_OK, 0);


    Last edited by Andreas Masur; July 24th, 2005 at 07:49 AM.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > CodeGuru Technical FAQs > CodeGuru Individual FAQs


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is On
    Forum Jump


    All times are GMT -5. The time now is 09:22 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009