Member_2_207537
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(&FullNa me);
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
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(&FullNa
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
ASKER
That Fixed it.
Can anyone tell me why it fixed it to share double the points with makerp
tcss
Can anyone tell me why it fixed it to share double the points with makerp
tcss
>*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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Seems I was not the "anyone" from above claim...
ASKER
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
My appologies on the error(s).
https://www.experts-exchange.com/questions/21285587/nonubik-Allocation-error-correction.html
tcss
No problem, then. Thanks.
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();