Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

COM: BSTR in structure. How pass it?

Posted on 1998-11-11
1
Medium Priority
?
665 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 400 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

715 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