Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
platform
active.directory
adsi
adsi.iis-admin
base
com_ole
complus_mts
component_svcs
database
directx
gdi
graphics_mm
internet.client
internet.server
internet.server.isapi-dev
localization
mapi
messaging
msi
mslayerforunicode
multimedia
networking
networking.ipv6
sdk_install
security
shell
telephony.tapi_2
telephony.tapi_3
telephony.tsp
telephony.wte
tools
ui
ui_shell
win_base_svcs
win16
  
 
date: Tue, 15 Nov 2005 12:50:32 +0100,    group: microsoft.public.platformsdk.com_ole        back       


Passing string between com object and back   
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 12:50:32 +0100   author:   Arsalan Ahmad

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

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us