Need help to return string value from COM
-
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
-
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
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
-
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
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 -
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, ArunNo 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 -
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 -
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; }