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


Newest CodeGuru.com Articles:

  • Binding Data to Silverlight 4.0 Controls Using ASP.NET MVC Framework 2.0
  • ADO.NET Data Services in the .NET Framework
  • Visual C++ Programming: What's new for MFC library in VC++ 2010?
  • Microsoft Visual Studio LightSwitch and What It Can Do For You

  • 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 November 12th, 2005, 09:52 PM
    kirants kirants is offline
    Elite Member
    Power Poster
     
    Join Date: Feb 2000
    Location: San Diego, CA
    Posts: 10,354
    kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)kirants has a reputation beyond repute (3000+)
    Windows SDK String: What are the rules for BSTR allocation and deallocation?

    Q: What are the rules for BSTR allocation and deallocation ?

    A:
    • Case 1: BSTR passed by value

      ODL/IDL method signature:
      Code:
      [id(1), helpstring("method SomeMethodRule1")] HRESULT SomeMethodRule1([in] BSTR bstrParam);
      Client side rules:
      Client allocates and deallocates BSTRs

      Client side example code:
      Code:
      {
        BSTR bstrParam = SysAllocString(L"Test");
        if(SUCCEEDED(pTestBstr->SomeMethodRule1(bstrParam)))
        {
          //use bstrParam
        }
        //done.. Now free..
        SysFreeString(bstrParam);
      }
      Server side rules:
      None

      Server side example code:
      Code:
      STDMETHODIMP CBSTRTest::SomeMethodRule1(BSTR bstrParam)
      {
        // TODO: Add your implementation code here
        MessageBox(NULL,bstrParam,_T("MethodRule1"),MB_OK);
      
        return S_OK;
      }
    • Case 2: BSTR passed by reference

      ODL/IDL method signature:
      Code:
      [id(2), helpstring("method SomeMethodRule2")] HRESULT SomeMethodRule2([in,out] BSTR* bstrParam);
      Client side rules:
      Client allocates and deallocates BSTRs

      Client side example code:
      Code:
      {
        BSTR bstrParam = SysAllocString(L"Test");
        if(SUCCEEDED(pTestBstr->SomeMethodRule2(&bstrParam)))
        {
          //use bstrParam
          MessageBox(NULL,bstrParam,_T("New value"),MB_OK);
        }
        //done.. Now free..
        SysFreeString(bstrParam);
      }
      Server side rules:
      Server can replace the BSTR with a new one, hence returning the new one to the client

      Server side example code:
      Code:
      STDMETHODIMP CBSTRTest::SomeMethodRule2(BSTR *bstrParam)
      {
        // TODO: Add your implementation code here
        MessageBox(NULL,*bstrParam,_T("MethodRule2"),MB_OK);
      
        //now reallocate if required
        SysReAllocString(bstrParam,L"newValueRule2");
      
        //or modify existing one if required
        return S_OK;
      }
    • Case 3: BSTR returned from server

      ODL/IDL method signature:
      Code:
      [id(3), helpstring("method SomeMethodRule3")] HRESULT SomeMethodRule3([out] BSTR* bstrParam);
      Client side rules:
      Client does not allocate. Client deallocates.

      Client side example code:
      Code:
      {
        BSTR bstrParam;
        if(SUCCEEDED(pTestBstr->SomeMethodRule3(&bstrParam)))
        {
          //use bstrParam
          MessageBox(NULL,bstrParam,_T("New value"),MB_OK);
      
          //Done.. Now free..
          SysFreeString(bstrParam);
        }
      }
      Server side rules:
      Server allocates BSTR.

      Server side example code:
      Code:
      STDMETHODIMP CBSTRTest::SomeMethodRule3(BSTR *bstrParam)
      {
        // TODO: Add your implementation code here
        *bstrParam = SysAllocString(L"newValueRule3");
          
        return S_OK;
      }

    Attached Files
    File Type: zip testBSTRRules.zip (13.5 KB, 615 views)

    Last edited by Andreas Masur; November 13th, 2005 at 07:25 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 08:33 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.