Re: Need help to return string value from COM
Wow, that much code when a simple API call would suffice...
STDMETHODIMP CMyObj::Echo(BSTR val, BSTR* ret) {
*ret = SysAllocString(val);
return (val == NULL) || (*ret != NULL) ? S_OK : E_OUTOFMEMORY;
}
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:%23hFfoy6kGHA.1260@TK2MSFTNGP05.phx.gbl...
agarunk@gmail.com wrote:
I'm trying to create a .dll using ATL, which I want use in C#.
Can anybody please guide me to create a simple method / function which
takes string in input parameters and returns the same(string)..
// in IDL
HRESULT Echo([in] BSTR val, [out, retval] BSTR* ret);
// in C++
STDMETHODIMP CMyObj::Echo(BSTR val, BSTR* ret) {
CComBSTR bstr;
bstr.Attach(val);
HRESULT hr = bstr.CopyTo(ret);
bstr.Detach();
return hr;
}
--
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