Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

USES_CONVERSION

39 views
Skip to first unread message

Remo Brigger

unread,
Feb 25, 1999, 3:00:00 AM2/25/99
to
Use ATL Macros:

void yourfuntion()
{
USES_CONVERSION;

BSTR bstr=SysAllocString(OLESTR("Hello")); // contains an ole string
2Byte
char* pArr = new char[20];
pArr=OLE2A(bstr); // ole to ansi conversion
delete [] pArr; // contains the ansi string 1 Byte
}


Alexander Nickolov

unread,
Feb 25, 1999, 3:00:00 AM2/25/99
to
What is the quiestion?
There is one bug though - the delete operator will crash. You don't
have to delete the result from the ATL conversion macros except
for X2BSTR which should be followed by SysFreeString. The macros
allocate space on the stack (except for the aforementioned one).

--
===============================
Alexander Nickolov
STL Panasonic
email: agnic...@geocities.com
===============================

Remo Brigger wrote in message <7b2tfp$hvk$1...@pollux.ip-plus.net>...

Remo Brigger

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to

This was a repla to another question, in the wrong place.
Sorry
But how can I then delete the array created with new??
I've tried it, and it works!

Remo

Alexander Nickolov wrote in message ...

Reginald Blue

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
Alexander's comment about new was as follows:

char* pArr = new char[20];

// assigns a pointer allocated from the heap to pArr


pArr=OLE2A(bstr); // ole to ansi conversion

// assigns a pointer allocated from the stack using _alloca()


delete [] pArr; // contains the ansi string 1 Byte

// deletes the memory pointed to by pArr which points to the stack
// this will corrupt something...probably the stack frame.

deleting a pointer is not a problem IN GENERAL, but what you've done here
will definitely cause problems.

Note that had you done this:


char* pArr = new char[20];

delete [] pArr;
pArr=OLE2A(bstr);

It would work fine.

HTH

--
Reginald Blue | Opinions expressed here do not
Natural Language Understanding | necessarily represent those of
Unisys Corporation | my employer.
|-------------------------------
| My email address is wrong, you
r...@NOSPAM.trsvr.tr.unisys.com | need to remove the obvious.

Remo Brigger wrote in message <7b5dqd$f0r$1...@pollux.ip-plus.net>...

Alexander Nickolov

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
Thanks, Reginald.

Remo, I simply didn't see the new operator :) Else I would have
scored TWO errors :) - the former - memory leak, the latter - GPF.

--
===============================
Alexander Nickolov
STL Panasonic
email: agnic...@geocities.com
===============================

Reginald Blue wrote in message ...

0 new messages