Link to home
Start Free TrialLog in
Avatar of Member_2_207537
Member_2_207537Flag for South Africa

asked on

Newbie COM Question.

Hi All

Please excuse me if this should be asked on the VB fourum. I suspect the problem is in the C code.

I have the following code in a COM object
STDMETHODIMP CHello::get_FullName(BSTR *pVal)
{
      memset(m_FullName, 0 , sizeof(m_FullName));
      strcat(m_FullName, m_FirstName);
      strcat(m_FullName, ", ");
      strcat(m_FullName, m_LastName);
      CComBSTR Tmp(m_FullName);
      *pVal = BSTR(Tmp);
      return S_OK;
}
Which is a simple function to cat two strings togeather and return the result. This is simple but I am new to this COM game.

When I return the result to a VB client and put the text into a text box like such,
                 Text1.Text = objTestATL.FullName
The text box only displays the first charachter of the string.
If I display it in a message box  like such
                 MsgBox (objTestATL.FullName)
I get the complete string.

If I run it from a C++ client
                        pICG->get_FullName(&FullName);
                        WideCharToMultiByte(CP_ACP,
                                0,
                                FullName,
                                lstrlenW(FullName),    
                                m_FirstName,
                                lstrlenW(FullName),    
                                NULL,
                                NULL);

I get the full string.

What am I doing wrong that this data is misrepresented in VB.

All help appretiated

tcss
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image


memset(m_FullName, 0 , sizeof(m_FullName));
strcat(m_FullName, m_FirstName);
strcat(m_FullName, ", ");
strcat(m_FullName, m_LastName);

/* i usually do this to return a string */
CComBSTR t_retval = m_FullName;      
*retval = t_retval.Detach();
Avatar of Member_2_207537

ASKER

That Fixed it.

Can anyone tell me why it fixed it to share double the points with makerp

tcss
Avatar of nonubik
nonubik

>*pVal = BSTR(Tmp);

Only casts the CComBstr variable to a BSTR one. More exactly, the BSTR cast operator returns the m_str member of the CComBstr class (which is a BSTR).

BSTR* operator&() throw()
{
      return &m_str;
}

When the method is left, the CCOmBstr variable is destroyed and the m_str is freed

~CComBSTR() throw()
{
      ::SysFreeString(m_str);
}

Thus, the data from the m_str address should not be reliable (I wonder how you didn't get an access violation ;)


>*retval = t_retval.Detach();

The Detach methods returns the m_str member (without freeing) and is the caller duty to free it.

BSTR Detach() throw()
{
      BSTR s = m_str;
      m_str = NULL;
      return s;
}
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems I was not the "anyone" from above claim...
You were supposed to be. I stuffed up with the allocation and need to figuare out how to undo it. It appears the best way will be to post a question and give you the points. That will equate to half double the points.

My appologies on the error(s).

https://www.experts-exchange.com/questions/21285587/nonubik-Allocation-error-correction.html

tcss
No problem, then. Thanks.