DCOM with BSTR data types

Started by
0 comments, last by Ra 16 years, 4 months ago
Let's get enterprisey, old-school style. Picture the following scenario: A DCOM server as an exe file, and a client app - both written in C++. The server may be either on the same machine as the client, or some other computer on the network. I am not sure if this scenario would make the answers differ from normal COM usage. Now - the questions. 1) When using a BSTR return type on a COM interface method (declared [out, retval] in IDL file), how should it be handled on the client? Should SysFreeString be used on the returned value? Would it be better to use a _bstr_t than a raw BSTR - and does it need to be freed in that case too, or would the _bstr_t take care of it? (Three questions for the price of one, yay!) 2) For the same BSTR return type from the server, it should be allocated but not freed on the server, right? 2) If a BSTR is declared in the IDL-file as [out] only, I would guess it needs to be allocated manually on the client, and freed after the COM call. On the server it's neither allocated nor freed manually. Is this correct? 3) I am guessing the same as 2) generally applies to parameters that are declared [in, out]. Correct? Getting this wrong seems to cause subtle bugs that are hard to detect. I would be very grateful if someone experienced with COM could advice.
Advertisement
If a method accepts a BSTR the caller allocates it, passes it in, and frees it upon returning. As far as I know the callee is okay to free and re-allocate this string in order to change the value passed back to the caller, since you're not supposed to write to them in-place (they live in memory controlled by RPC).

The COM marshaler takes care of all the BSTR memory management stuff behind the scenes, so from my understanding you just have to make sure that whoever allocates the resource frees it on the same side. That is if the server allocates it then it needs to free it after the call and vice versa.

I'm not sure what the difference between BSTR and _bstr_t is, as I haven't used IDL much at all.
Ra

This topic is closed to new replies.

Advertisement