?
Solved

BSTR as an ole automation parameter (freeing, allocating) !!!

Posted on 2000-02-02
4
Medium Priority
?
481 Views
Last Modified: 2012-08-14
SDK: ASP(IIS4), BCB4

When a BSTR is passed to an ole automation function call.

Case 1 >> HRESULT _stdcall MyFunction([in] BSTR strng );

   After using strng, must i free the BSTR by SysFreeString() ?

Case 2  >> HRESULT _stdcall MyFunction([int, out] BSTR * strng );

   After using strng, i want to change strng value (as an out parameter),
   must i free the BSTR by SysFreeString() to recreate one with
   another value ?
   What are the steps to initialize the strng to another value ?

Case 3 >> HRESULT _stdcall MyFunction([out, retval] BSTR * strng );

   To pass an empty string, what can i do ? When the function
   receives a pointer to a BSTR, can i let the pointer untouched ?
   what does the value pointed by the pointer ?
   


0
Comment
Question by:mars
  • 3
4 Comments
 

Author Comment

by:mars
ID: 2481446
In the Case 3
  What IS the value pointed by the pointer ?
0
 
LVL 3

Accepted Solution

by:
GlennDean earned 400 total points
ID: 2483386
mars:
   The rules are as follows:
Case I) The client is responsible for allocating and calling SysFreeString (not the server)

Case II) The client is responsible for allocating and calling SysFreeString.  The server can call SysFreeString and then reallocate.  BUT, whatever the client gets back (potentially different from what it sent) must call SysFreeString on the return value.

Case III) The server is responsible for allocating and the client is responsible for calling SysFreeString

Incidentally, these rules also apply to memory allocated via CoTaskMemAlloc and CoTaskMemFree.

   Glenn
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2483515
mars:
   I forgot to answer the last part of your question.  You can definitely allocate 0-length BSTRs as in
BSTR bstr = ::SysAllocString(L"");

You get a "honest-to-goodness" BSTR, it's just that the 4-byte prefix to the pointer has the length of 0.  
   Glenn
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 2483527
Oh, and how you treat the 0-length BSTR follows the same rules as for greater than 0 length BSTR.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question