Click to See Complete Forum and Search --> : Olestr
George2
April 2nd, 2008, 02:25 AM
Hello everyone,
When we use OLESTR to allocate a variable from string literal, like OLESTR("Hello World"), the memory space for the string literal is on stack, right?
I can not find official document for this from MSDN.
thanks in advance,
George
Arjay
April 2nd, 2008, 03:02 AM
It's the same as using L"my string";
See OLESTR (http://msdn2.microsoft.com/en-us/library/ms692615(VS.85).aspx)
Note: It's not the same as using SysAllocString (http://msdn2.microsoft.com/en-us/library/ms221069(VS.80).aspx)( ) to allocate a BSTR.
George2
April 2nd, 2008, 03:27 AM
Thanks Arjay,
I have read that. My question is, can we feel free to pass the OLESTR pointer, which points to an OLESTR allocated in one function to another function (I mean passing the address of OLESTR string literal)?
If it is allocated in stack, we can not. And I understand it is not allocated on heap, like string pointed by BSTR.
If it is allocated by global read-only memory, I think it is fine.
Any comments?
It's the same as using L"my string";
See OLESTR (http://msdn2.microsoft.com/en-us/library/ms692615(VS.85).aspx)
Note: It's not the same as using SysAllocString (http://msdn2.microsoft.com/en-us/library/ms221069(VS.80).aspx)( ) to allocate a BSTR.
regards,
George
Igor Vartanov
April 2nd, 2008, 05:27 AM
My question is, can we feel free to pass the OLESTR pointer,I would recommend never rely on pointers in COM. In case a method requires for string parameter, it must be a correctly allocated BSTR but never OLESTR.
George2
April 2nd, 2008, 06:12 AM
Thanks Igor,
You mean OLESTR is not ensured to be on global read-only area or stack area?
So, not 100% safe to pass pointer to OLESTR variable in one function to another?
I would recommend never rely on pointers in COM. In case a method requires for string parameter, it must be a correctly allocated BSTR but never OLESTR.
regards,
George
Igor Vartanov
April 2nd, 2008, 10:47 AM
You mean OLESTR is not ensured to be on global read-only area or stack area?OLESTR is just another way of declaring wide-char literal. Where it will be placed depends on compiler family, version and/or settings.
So, not 100% safe to pass pointer to OLESTR variable in one function to another?From compiler's point of view there's no difference between BSTR and OLESTR in COM method prototype, but those types are really different in implementation details.
Imagine you pass some OLESTR literal where BSTR is expected. BSTR would have special byte counter prepended to string body while the literal has not. Don't you feel the danger? ;)
Arjay
April 2nd, 2008, 12:24 PM
Thanks Igor,
You mean OLESTR is not ensured to be on global read-only area or stack area?
So, not 100% safe to pass pointer to OLESTR variable in one function to another?
regards,
GeorgeIt's as safe as passing a "my string" string literal from one NON-COM function to another. When you are talking about passing a string to a COM method, you are generally talking about passing a BSTR and a OLESTR is not a BSTR. BSTR's must be allocated with SysAllocString() and there are specific rules about who frees the allocated string.
If you are doing COM work, you generally don't need to ever use OLESTR because it's just around for 16-bit compatibility (see the OLESTR link I gave you earlier, it explains this).
George2
April 3rd, 2008, 04:31 AM
Thanks Igor,
OLESTR is just another way of declaring wide-char literal. Where it will be placed depends on compiler family, version and/or settings.
I remember in C++ standard, string literal should be of static life cycle, means not on stack. Right?
regards,
George
George2
April 3rd, 2008, 04:33 AM
Thanks for your advice, Arjay!
It's as safe as passing a "my string" string literal from one NON-COM function to another. When you are talking about passing a string to a COM method, you are generally talking about passing a BSTR and a OLESTR is not a BSTR. BSTR's must be allocated with SysAllocString() and there are specific rules about who frees the allocated string.
If you are doing COM work, you generally don't need to ever use OLESTR because it's just around for 16-bit compatibility (see the OLESTR link I gave you earlier, it explains this).
regards,
George
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.