Solved

COM: BSTR in structure. How pass it?

Posted on 1998-11-11
1
649 Views
Last Modified: 2009-10-08
Hi,
I have this problem:
I want to pass some structure with two BSTR strings from server to client.

SERVER SIDE:
============
part of my IDL:
---------------
typedef struct
{
      BSTR      text1;
      BSTR      text2;
} NWVaTestStruct;

....

interface INWVaTest : IUnknown
{
      [helpstring("method GetTestData")] HRESULT GetTestData([out] NWVaTestStruct** ppData);
      ....
};

....

part of H file of CNWVaTest class:
----------------------------------
STDMETHOD(GetTestData)(/*[out]*/ NWVaTestStruct** ppData);

part of CPP file of CNWVaTest class:
------------------------------------
STDMETHODIMP CNWVaTest::GetTestData(/*[out]*/ NWVaTestStruct** ppData)
{
      *ppData = new NWVaTestStruct;

      (*ppData)->text1 = (BSTR)CComBSTR("aaa");
      (*ppData)->text2 = (BSTR)CComBSTR("bbb");

      return S_OK;
}

CLIENT SIDE:
============
NWVaTestStruct* testData;

m_pVaTest->GetTestData(&testData);

CString sTemp1,sTemp2;
BSTR bstr;

sTemp1 = CString(testData->text1);
sTemp2 = CString(testData->text2);
----------------------------------------

Both strings, sTemp1 and sTemp2, contains "bbb". What's wrong?
How can I pass structure with BSTRs?

Thank you.
0
Comment
Question by:zemanek
1 Comment
 
LVL 3

Accepted Solution

by:
xyu earned 100 total points
Comment Utility
Your problem is in the code:

  (*ppData)->text1 = (BSTR)CComBSTR("aaa");
  (*ppData)->text2 = (BSTR)CComBSTR("bbb");

Here You creating 2 temp. objects that become destructed after the operator= call...

You have to change it to:
:

  (*ppData)->text1 = SysAllocString(L"aaa");
  (*ppData)->text2 = SysAllocString(L"bbb");

in the client and to:

  sTemp1 = CString(testData->text1);
  SysFreeString(testData->text1);
  sTemp2 = CString(testData->text2);
  SysFreeString(testData->text2);

in the server...

Good luck



0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now