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

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 ?
   


marsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

marsAuthor Commented:
In the Case 3
  What IS the value pointed by the pointer ?
0
GlennDeanCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GlennDeanCommented:
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
GlennDeanCommented:
Oh, and how you treat the 0-length BSTR follows the same rules as for greater than 0 length BSTR.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.