Re: How to return string?
"Lee Tow" <fbjlt@pub3.fz.fj.cn> wrote in message
news:uLyM7GfmHHA.2452@TK2MSFTNGP04.phx.gbl
I want to know if I want to return string in the function of
interface,
look:HRESULT PrtStr([out,retval] wchar_t *str);
This is a poor choice for the interface. It suggests the caller must
allocate memory for the string and the function would then fill this
buffer with actual data, but a) there is no way for the function to know
how large a buffer the caller has allocated, and b) the caller doesn't
know how much it's supposed to allocate.
If your interface needs to be automation-compatible, make it
HRESULT PrtStr([out,retval] BSTR* str);
If it doesn't have to be, you may still choose to use BSTR, but there
are also other options. For example:
HRESULT PrtStr([out,retval,string] wchar_t** str);
Here the function must allocate memory for the string with
CoTaskMemAlloc, and the caller would free it with CoTaskMemFree when
done with it.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925