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. ATL / WTL / STL
  4. Need help to return string value from COM

Need help to return string value from COM

Scheduled Pinned Locked Moved ATL / WTL / STL
c++helpcsharpdotnetcom
6 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.
  • A Offline
    A Offline
    agarunk
    wrote on last edited by
    #1

    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).. I tried a lot but always faced problem of returing string back to calling function in C#. I did the same with C++ class library successfully but when it comes to ATL its not the same. As I'm using other SDK API's in creation of .dll and I can't go with Class Library its having problem with "CLR and MTd". Which can't go together. Finally I decided to stick to ATL. Now please guide me to create the methods which I can use in C# to get the string value. Arun

    R 1 Reply Last reply
    0
    • A agarunk

      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).. I tried a lot but always faced problem of returing string back to calling function in C#. I did the same with C++ class library successfully but when it comes to ATL its not the same. As I'm using other SDK API's in creation of .dll and I can't go with Class Library its having problem with "CLR and MTd". Which can't go together. Finally I decided to stick to ATL. Now please guide me to create the methods which I can use in C# to get the string value. Arun

      R Offline
      R Offline
      Roozbeh69
      wrote on last edited by
      #2

      I assume that you don't use attributes in your project, and you want to have a property named 'Title' of type BSTR (COM string type) in your IDL file: interface IYourInterface: IDispatch{ [propget, id(1), helpstring("property Title")] HRESULT Title([out, retval] BSTR *pVal); [propput, id(1), helpstring("property Title")] HRESULT Title([in] BSTR newVal); }; in the header file of your object: public: STDMETHOD(get_Title)(/*[out, retval]*/ BSTR *pVal); STDMETHOD(put_Title)(/*[in]*/ BSTR newVal); private: _bstr_t m_bstrTitle; // variable holding the property value and in the implementation file of your object: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle.copy(); return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = _bstr_t(newVal); return S_OK; } -- modified at 15:53 Monday 19th June, 2006

      A 1 Reply Last reply
      0
      • R Roozbeh69

        I assume that you don't use attributes in your project, and you want to have a property named 'Title' of type BSTR (COM string type) in your IDL file: interface IYourInterface: IDispatch{ [propget, id(1), helpstring("property Title")] HRESULT Title([out, retval] BSTR *pVal); [propput, id(1), helpstring("property Title")] HRESULT Title([in] BSTR newVal); }; in the header file of your object: public: STDMETHOD(get_Title)(/*[out, retval]*/ BSTR *pVal); STDMETHOD(put_Title)(/*[in]*/ BSTR newVal); private: _bstr_t m_bstrTitle; // variable holding the property value and in the implementation file of your object: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle.copy(); return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = _bstr_t(newVal); return S_OK; } -- modified at 15:53 Monday 19th June, 2006

        A Offline
        A Offline
        agarunk
        wrote on last edited by
        #3

        hello @ Roozbeh69 thanks for that solution but I have faced few problems which are listed below : I have created property using wizard but when it comes to private: _bstr_t m_bstrTitle; // variable holding the property value and in the implementation file of your object: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle.copy(); //troubling here return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = _bstr_t(newVal); //troubling here return S_OK; } and As per me the syntax which u have given varies a lot from auto generated one. I'm using VS 2005. Once again I would like to clear what exactly I'm trying after ... I want to create a .dll using ATL. After I want to create a method inside this, which I can call from C#. This method should take Filepath(string) as input while invoking from C# and should return some string back to C#.(not pointer) Please guide me if possible with a proper working code. Thanking you, Arun

        M 1 Reply Last reply
        0
        • A agarunk

          hello @ Roozbeh69 thanks for that solution but I have faced few problems which are listed below : I have created property using wizard but when it comes to private: _bstr_t m_bstrTitle; // variable holding the property value and in the implementation file of your object: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle.copy(); //troubling here return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = _bstr_t(newVal); //troubling here return S_OK; } and As per me the syntax which u have given varies a lot from auto generated one. I'm using VS 2005. Once again I would like to clear what exactly I'm trying after ... I want to create a .dll using ATL. After I want to create a method inside this, which I can call from C#. This method should take Filepath(string) as input while invoking from C# and should return some string back to C#.(not pointer) Please guide me if possible with a proper working code. Thanking you, Arun

          M Offline
          M Offline
          Milton Karimbekallil
          wrote on last edited by
          #4

          No need to call the .copy() or to create a new _bstr_t out of the BSTR u r gettign into the property put. corrected code below: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle; return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = newVal; return S_OK; } Regarding the method u need to create, the IDL signature will be: [id(1), helpstring("your atl method signature")] HRESULT YourATLMethod([in] BSTR Filepath, [out,retval] BSTR* ReturnString); and the C++ implimentation signature will be: STDMETHODIMP CYourClass::YourATLMethod(BSTR Filepath, BSTR* ReturnString) { _bstr_t bstrFilepath = Filepath; _bstr_t bstrReturnstring = _T("some string here"); *ReturnString = bstrReturnstring ; return S_OK; } cheers ...milton kb

          A 1 Reply Last reply
          0
          • M Milton Karimbekallil

            No need to call the .copy() or to create a new _bstr_t out of the BSTR u r gettign into the property put. corrected code below: STDMETHODIMP CYourClass::get_Title(BSTR *pVal) { *pVal = m_bstrTitle; return S_OK; } STDMETHODIMP CYourClass::put_Title(BSTR newVal) { m_bstrTitle = newVal; return S_OK; } Regarding the method u need to create, the IDL signature will be: [id(1), helpstring("your atl method signature")] HRESULT YourATLMethod([in] BSTR Filepath, [out,retval] BSTR* ReturnString); and the C++ implimentation signature will be: STDMETHODIMP CYourClass::YourATLMethod(BSTR Filepath, BSTR* ReturnString) { _bstr_t bstrFilepath = Filepath; _bstr_t bstrReturnstring = _T("some string here"); *ReturnString = bstrReturnstring ; return S_OK; } cheers ...milton kb

            A Offline
            A Offline
            agarunk
            wrote on last edited by
            #5

            Hi Milton, Yes, it solved my problem. Really thank u very much man. Thanking U, Arun

            P 1 Reply Last reply
            0
            • A agarunk

              Hi Milton, Yes, it solved my problem. Really thank u very much man. Thanking U, Arun

              P Offline
              P Offline
              Pharago
              wrote on last edited by
              #6

              STDMETHODIMP CYourClass::YourATLMethod(BSTR Filepath, BSTR* ReturnString) { use the BSTR FilePath to get the BSTR to return, you can also use a LPCOLESTR CComBSTR TheReturnString = L"the string"; *ReturnString = SysAllocString(TheReturString.m_str); //or any BSTR or LPCOLESTR to be allocated by the system using the pointer given by the com client return S_OK; }

              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