Solved

COM: BSTR in structure. How pass it?

Posted on 1998-11-11
1
658 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 158
C++ get user from AD  (VS6) 7 86
Fast, free, private online code repository / editor 7 106
Writing a parser for java language 4 85
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.

830 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