Solved

COM: BSTR in structure. How pass it?

Posted on 1998-11-11
1
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
xyu earned 100 total points
ID: 1177529
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 110
I could not build boost code, 10 116
C++ mouse_event mouse look 7 131
One named event, multiple event handlers 2 58
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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