SysAllocString() allocation
-
I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution
-
I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution
1. wild guess - on the heap of the library that exported the
SysAllocString
? 2. I think yes, as long as you will be the one who releases the memory back 3. when expectsBSTR
, it expects the size on the start, when parameter isLPWSTR
, it checks for zero termination. -
I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution
I believe it's allocated on the COM heap. M.Shoaib Khan wrote: LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? When COM allocates a BSTR, it does something like this:
struct _BSTR {
int length;
OLECHAR str[1];
};BSTR SysAllocString(LPCOLESTR src)
{
int bstr_len = wcslen(src) + 1; // 1 for NUL character
_BSTR* bstr = CoTaskMemAlloc(bstr_len * sizeof(OLECHAR) + sizeof(int));
bstr->length = bstr_len;
wcscpy(bstr->str, src, bstr_len - 1);
return bstr->str;
}As you can see, a BSTR really isn't a LPWSTR. It just appears to be. :) -- My name in Katakana is ヨルゲン. My name in German is Jörgen. My name in Mandarin/Kanji is 乔尔根 西格瓦德森. I blog too now[^]