Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Newbie COM Question.

Posted on 2004-11-30
8
Medium Priority
?
253 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:tcss
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 12704784

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
 
LVL 1

Author Comment

by:tcss
ID: 12705384
That Fixed it.

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

tcss
0
 
LVL 16

Expert Comment

by:nonubik
ID: 12705552
>*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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
makerp earned 1000 total points
ID: 13114351
>>Can anyone tell me why it fixed it to share double the points with makerp
0
 
LVL 16

Expert Comment

by:nonubik
ID: 13115790
Seems I was not the "anyone" from above claim...
0
 
LVL 1

Author Comment

by:tcss
ID: 13118560
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
 
LVL 16

Expert Comment

by:nonubik
ID: 13119008
No problem, then. Thanks.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question