Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Can a method of COM-server return a string pointer

Can a method of COM-server return a string pointer

Scheduled Pinned Locked Moved COM
questioncomsysadminlinux
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bash
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • B 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

      J Offline
      J Offline
      justanotherprogrammer
      wrote on last edited by
      #2

      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.

      V R 2 Replies Last reply
      0
      • J justanotherprogrammer

        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.

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        Remember to pass in the COM interface function an address of BSTR and to dereference the pointer to a BSTR before assignment. *str = SysAllocString("string for COM"); HRESULT hr = Abc->GetStr(&myBStr); Kuphryn

        1 Reply Last reply
        0
        • J justanotherprogrammer

          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.

          R Offline
          R Offline
          Renjith Ramachandran
          wrote on last edited by
          #4

          what he says and what u answer..?? oooooooops..


          [ It is possible to represent everything in this universe by using 0 and 1 ]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups