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
LVL 1
tcssAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

makerpCommented:

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();
0
tcssAuthor Commented:
That Fixed it.

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

tcss
0
nonubikCommented:
>*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;
}
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

makerpCommented:
>>Can anyone tell me why it fixed it to share double the points with makerp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nonubikCommented:
Seems I was not the "anyone" from above claim...
0
tcssAuthor Commented:
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).

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21285587.html

tcss
0
nonubikCommented:
No problem, then. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.