1. any special benefits if we use SysAllocString compared with using a simple wide character string, like L"Hello"?
2. If we use SysAllocString to allocate constant string, then does SysAllocString allocate the constant string on heap, stack or on the constant string pool?
This function allocates a new string and copies the passed string into it. This function returns null if there is insufficient memory or if a null pointer is passed in
[in] Null-terminated string to copy. The string must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications.
Second thing Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying
BSTR b = L"A String";
This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is
This function allocates a new string and copies the passed string into it. This function returns null if there is insufficient memory or if a null pointer is passed in
[in] Null-terminated string to copy. The string must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications.
Second thing Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying
BSTR b = L"A String";
This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is
BSTR b = :: SysAllocString(L"A String");
Thanx
I do not quite understand what means,
BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation.
I think when we use SysAllocString to allocate some memory, then we have to *concern* to free it by SysFreeString, why in MSDN, it is mentioned that there is no concern for memory management? Please feel free to correct me if I am wrong.
Regarding this line "BSTRs are allocated using COM memory allocation functions"
Few point as i mention in my above post:
Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying
BSTR b = L"A String";
This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is
A BSTR is not a COM object, it is simply a pointer to a specially laid out wide string that is strictly allocated via SysAllocString. The meaning of "BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation" is not that the string doesn't need to be cleaned up (you must eventually free the string via SysFreeString or it will leak); it means that it is allocated on a COM heap instead of your CRT's heap, so you may return it out of your COM object to another process, and that other process may clean it up (this would not be possible if it were allocated via your process' CRT).
In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.
I can fully understand your reply. But is your reply answers to my previous question (below)?
BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation.
My question is why we do not need to concern about memory allocation? We still need to keep in mind when to free (SysFreeString) it. What does the above statement mean?
Pintu Shukla wrote:
Regarding this line "BSTRs are allocated using COM memory allocation functions"
Few point as i mention in my above post:
Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying
BSTR b = L"A String";
This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is
A BSTR is not a COM object, it is simply a pointer to a specially laid out wide string that is strictly allocated via SysAllocString. The meaning of "BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation" is not that the string doesn't need to be cleaned up (you must eventually free the string via SysFreeString or it will leak); it means that it is allocated on a COM heap instead of your CRT's heap, so you may return it out of your COM object to another process, and that other process may clean it up (this would not be possible if it were allocated via your process' CRT).
In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.