|
|
|
date: Tue, 15 Nov 2005 12:50:32 +0100,
group: microsoft.public.platformsdk.com_ole
back
Re: Passing string between com object and back
Hello Arsalan,
> CComPtr<ICTestATL> ptr;
>
> HRESULT hr = ptr.CoCreateInstance(__uuidof(CTestATL));
>
> BSTR inStr = SysAllocStringByteLen( "This is a test string",
> strlen("This is a test string"));
>
> BSTR outStr;
>
> ptr->GetString(inStr, &outStr)
No need to allocate outStr, it's the server's responsibility. And it's your
responsibility to deallocate it once you have received it and used it.
Your code can be reduced to:
CComPtr<ICTestATL> ptr;
HRESULT hr = ptr.CoCreateInstance(__uuidof(CTestATL));
CComBSTR outStr;
ptr->GetString(CComBSTR("This is a test string"), &outStr);
CComBSTR automatically deallocates the contained BSTR when it leaves the
current scope.
> Also right now I am getting following linking errors:
>
> 1>CTestATL.obj : error LNK2019: unresolved external symbol "wchar_t *
> __stdcall _com_util::ConvertStringToBSTR(char const *)"
> (?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z) referenced in function
> "public: __thiscall _bstr_t::Data_t::Data_t(char const *)"
> (??0Data_t@_bstr_t@@QAE@PBD@Z)
I think you need to link with comsupp.lib.
--
Best regards,
Kim Grsman
> Hi,
>
> I am implementing a COM object which has an interface function that
> takes one string as an input an returns another string as out
> variable.
>
> Please tell me for the out string do I need to allocate it first
> before passing its address to the interface function as shown below or
> not.
>
> CComPtr<ICTestATL> ptr;
>
> HRESULT hr = ptr.CoCreateInstance(__uuidof(CTestATL));
>
> BSTR inStr = SysAllocStringByteLen( "This is a test string",
> strlen("This is a test string"));
>
> BSTR outStr;
>
> ptr->GetString(inStr, &outStr)
>
> The implementation of GetString() is as follows:
>
> STDMETHODIMP CCTestATL::GetBSTRString(BSTR inStr, BSTR* outStr)
>
> {
>
> // TODO: Add your implementation code here
>
> char* lpszText2 = _com_util::ConvertBSTRToString(inStr);
>
> char outStr1[1000];
>
> g_obj.GetString(lpszText2, strlen(lpszText2), outStr1, 1000); // here
> I am getting some data in outStr2 which i need to return to client
>
> *outStr = _com_util::ConvertStringToBSTR(lpszText);
>
> delete[] lpszText2;
>
> return S_OK;
>
> }
>
> My question is do I need to allocate output bstr string in my client
> application using SysAllocString()? If yes then I think I need to
> delete the string in my COM interface function using SysFreeString().
> I like to mention that my client and servers are NOT within same
> process.
>
> Also right now I am getting following linking errors:
>
> 1>CTestATL.obj : error LNK2019: unresolved external symbol "wchar_t *
> __stdcall _com_util::ConvertStringToBSTR(char const *)"
> (?ConvertStringToBSTR@_com_util@@YGPA_WPBD@Z) referenced in function
> "public: __thiscall _bstr_t::Data_t::Data_t(char const *)"
> (??0Data_t@_bstr_t@@QAE@PBD@Z)
>
> 1>CTestATL.obj : error LNK2019: unresolved external symbol "char *
> __stdcall _com_util::ConvertBSTRToString(wchar_t *)"
> (?ConvertBSTRToString@_com_util@@YGPADPA_W@Z) referenced in function
> "public: char const * __thiscall _bstr_t::Data_t::GetString(void)const
> " (?GetString@Data_t@_bstr_t@@QBEPBDXZ)
>
> Thanks,
>
> Arsalan
>
date: Tue, 15 Nov 2005 04:47:23 -0800
author: Kim Gräsman
|
|