Can a method of COM-server return a string pointer
-
Hi, I'm a novice in COM so my question is very easy. I want to crete the COM server (INPROC_SERVER) with method like: virtual /* [helpstring][id] */ HRESULT STDMETHODCALLTYPE GetStr( /* [out][in] */ LPSTR lpcszStr) = 0; STDMETHODIMP CAbc::GetStr(LPSTR lpcszStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here char *mystr="string from COM"; lpcszStr=mystr; return S_OK; } So, is it possible to return the string pointer from COM server? Yours sincerely, Alex Bash
-
Hi, I'm a novice in COM so my question is very easy. I want to crete the COM server (INPROC_SERVER) with method like: virtual /* [helpstring][id] */ HRESULT STDMETHODCALLTYPE GetStr( /* [out][in] */ LPSTR lpcszStr) = 0; STDMETHODIMP CAbc::GetStr(LPSTR lpcszStr) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here char *mystr="string from COM"; lpcszStr=mystr; return S_OK; } So, is it possible to return the string pointer from COM server? Yours sincerely, Alex Bash
Hi, I'm a novice too. But I have had the same problem. So maybe I can give you a hint. As far as I know the best solution is to use the BSTR data type. This is not very complicated. There are only some special functions that you have to use: SysAllocString, SysFreeString, SysStringLen and some other ones. You could do it like this: IDL: GetStr([out] BSTR* str) Method implementation: STDMETHODIMP CAbc::GetStr(BSTR* str) { //Allocate memory for the BSTR str = SysAllocString("string for COM"); //Check wether allocation was successful if(str) return S_OK; else return E_FAIL; } And the caller: ... BSTR myBStr; HRESULT hr = Abc->GetStr(myBStr); if(FAILED(hr)) damned(); else { //Now you've got the string //Do with it what ever you want (but don't get rude...) //You can use it like a LPSTR as far as I know printf("My String: %s", myBStr); //example SetDlgItemText(IDC_EDIT, myBStr); //example //But now it is your responsability to free the memory! SysFreeString(myBStr); //That's it } ... I hope this was all correct. Try it. It should work. But maybe someone else can tell you more or correct my posting.
-
Hi, I'm a novice too. But I have had the same problem. So maybe I can give you a hint. As far as I know the best solution is to use the BSTR data type. This is not very complicated. There are only some special functions that you have to use: SysAllocString, SysFreeString, SysStringLen and some other ones. You could do it like this: IDL: GetStr([out] BSTR* str) Method implementation: STDMETHODIMP CAbc::GetStr(BSTR* str) { //Allocate memory for the BSTR str = SysAllocString("string for COM"); //Check wether allocation was successful if(str) return S_OK; else return E_FAIL; } And the caller: ... BSTR myBStr; HRESULT hr = Abc->GetStr(myBStr); if(FAILED(hr)) damned(); else { //Now you've got the string //Do with it what ever you want (but don't get rude...) //You can use it like a LPSTR as far as I know printf("My String: %s", myBStr); //example SetDlgItemText(IDC_EDIT, myBStr); //example //But now it is your responsability to free the memory! SysFreeString(myBStr); //That's it } ... I hope this was all correct. Try it. It should work. But maybe someone else can tell you more or correct my posting.
-
Hi, I'm a novice too. But I have had the same problem. So maybe I can give you a hint. As far as I know the best solution is to use the BSTR data type. This is not very complicated. There are only some special functions that you have to use: SysAllocString, SysFreeString, SysStringLen and some other ones. You could do it like this: IDL: GetStr([out] BSTR* str) Method implementation: STDMETHODIMP CAbc::GetStr(BSTR* str) { //Allocate memory for the BSTR str = SysAllocString("string for COM"); //Check wether allocation was successful if(str) return S_OK; else return E_FAIL; } And the caller: ... BSTR myBStr; HRESULT hr = Abc->GetStr(myBStr); if(FAILED(hr)) damned(); else { //Now you've got the string //Do with it what ever you want (but don't get rude...) //You can use it like a LPSTR as far as I know printf("My String: %s", myBStr); //example SetDlgItemText(IDC_EDIT, myBStr); //example //But now it is your responsability to free the memory! SysFreeString(myBStr); //That's it } ... I hope this was all correct. Try it. It should work. But maybe someone else can tell you more or correct my posting.
what he says and what u answer..?? oooooooops..
[ It is possible to represent everything in this universe by using 0 and 1 ]