return string from COM method
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: return string from COM method

  1. #1
    Join Date
    May 2013
    Posts
    10

    return string from COM method

    i try to make dll and install it under "Component Services".
    i tried to make method that return string but when i test it i don't get any result


    Code:
    STDMETHODIMP CSimpleChat::CellMe(BSTR name, BSTR** helloMessage)
    {
    
    	CString temp = _T("Hi ");
        temp += name;
        temp += ", welcome to the simple chat server!";
        BSTR str = temp.AllocSysString();
        *helloMessage = &str;
    	return S_OK;
    }
    thanks for the help i am new to this.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,612

    Re: return string from COM method

    Quote Originally Posted by wanttolearn1 View Post
    i try to make dll and install it under "Component Services".
    i tried to make method that return string but when i test it i don't get any result


    Code:
    STDMETHODIMP CSimpleChat::CellMe(BSTR name, BSTR** helloMessage)
    {
    	...
    	return S_OK;
    }
    Are you sure you want BSTR** type for the _out_ parameter, not a BSTR* one?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2013
    Posts
    10

    Re: return string from COM method

    i also tried
    Code:
    STDMETHODIMP CSimplec1::CellOut1(BSTR name1, BSTR* ret)
    {
    	// TODO: Add your implementation code here
    	CString temp = _T("Hi ");
        temp += name1;
        temp += ", welcome to the simple chat server!";
        BSTR str = temp.AllocSysString();
        *ret = str;
    	::SysFreeString(str);
    	return S_OK;
    }
    same problem

  4. #4
    Join Date
    May 2013
    Posts
    10

    Re: return string from COM method

    i found the problem, its because of
    ::SysFreeString(str);
    what is the correct way to do it?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,612

    Re: return string from COM method

    Quote Originally Posted by wanttolearn1 View Post
    i also tried
    Code:
    STDMETHODIMP CSimplec1::CellOut1(BSTR name1, BSTR* ret)
    {
    	// TODO: Add your implementation code here
    	CString temp = _T("Hi ");
        temp += name1;
        temp += ", welcome to the simple chat server!";
        BSTR str = temp.AllocSysString();
        *ret = str;
    	::SysFreeString(str);
    	return S_OK;
    }
    same problem
    First replace
    Code:
        *ret = str;
    with
    Code:
        ret = &str;
    Then remove the ::SysFreeString(str); call because after it is done there is no any data in str.
    The ::SysFreeString must be called by the caller of your CellOut1 method.
    Victor Nijegorodov

  6. #6
    Join Date
    May 2013
    Posts
    10

    Re: return string from COM method

    Quote Originally Posted by VictorN View Post
    The ::SysFreeString must be called by the caller of your CellOut1 method.
    how do i do this?
    if for example i call the method from vbscript?
    i need to call to another method to clear "str" variable?
    isn't there a better way?

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,072

    Re: return string from COM method

    Quote Originally Posted by wanttolearn1 View Post
    how do i do this?
    if for example i call the method from vbscript?
    i need to call to another method to clear "str" variable?
    isn't there a better way?
    It's not you to do this. Out value ownership is on client side, so client is responsible for freeing the string. In your case this is vbscript.

    PS. You definitely need to get some very basic reading on COM model.
    Best regards,
    Igor

  8. #8
    Join Date
    May 2013
    Posts
    10

    Re: return string from COM method

    Quote Originally Posted by Igor Vartanov View Post
    It's not you to do this. Out value ownership is on client side, so client is responsible for freeing the string. In your case this is vbscript.

    PS. You definitely need to get some very basic reading on COM model.
    any recommended articles?

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,072

    Re: return string from COM method

    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center