Question

I have a [dual] interface implementing IDispatch, something like this:

[dual, ...]
interface IMyInterface : IDispatch
{
    [id(1), propget] HRESULT StringValue([out, string, retval] OLECHAR const ** str);
};

My backing object for IMyInterface has a member variable, d_str:

class CBackingObject : public IMyInterface
{
    std::basic_string<OLECHAR> d_str;
    ...
};

What's the COM convention for returning StringValue property? Should I return d_str.data(), or a copy of it? Do clients automatically take on the responsibility of freeing the string returned by a string-valued property?

Was it helpful?

Solution

Hard to say, it is not an Automation compatible signature. An IDispatch interface requires passing strings as a BSTR, a string that's allocated on the COM heap. The contract for an [out,retval] is that the interface method allocates the string with SysAllocString() and the caller releases it with SysFreeString().

Your code as written is likely to misbehave. You must at least use SysAllocString, a copy in other words. You might get away with the OLECHAR** declaration in your IDL but it is quite unhealthy, this goes wrong when the call is early-bound and marshaled across an apartment boundary. You'd better fix it, use BSTR* instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top