Recently I've been looking into BSTRs, and have found how 'touchy' they can be. I saw this in a COM class in the class destructor regarding a m_strBStr member...

if(m_strBStr)
   SysFreeString(m_strBStr);

When I saw it I thought, "How would the BSTR have a zero assigned to it?", because I didn't see anywhere in the COM class where that had been done. In fact, I thought it was an illegal operation on a BSTR. However, I wrote a test app to check it, and I'm not getting any crashes...

#include <windows.h>
#include <oaidl.h>
#include <stdio.h>

int main(void)
{
 wchar_t* pWideCh=0;
 BSTR strName=0;

 wprintf(L"pWideCh = %u\n",(unsigned)pWideCh);
 wprintf(L"strName = %u\n",(unsigned)strName);
 strName=SysAllocString(L"Fred");
 wprintf(L"strName = %s\n",strName);
 SysFreeString(strName);
 strName=0;
 wprintf(L"strName = %u\n",(unsigned)strName);
 getchar();

 return 0;
}

/*
pWideCh = 0
strName = 0
strName = Fred
strName = 0
*/

Since a BSTR is just a typedef of a wchar_t*, I guess its OK. Could someone let me know for sure?

I'm not familiar with the Windows API, but this should say it all:

a BSTR is just a typedef of a wchar_t*

It is a pointer. Pointers can be NULL. NULL is equivalent to 0, which is interpreted as false by a conditional statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.