using NULL pointers in COM
I have a COM object with a method that is declared like this:
[id(1), helpstring("method GetString")] HRESULT GetString([out,retval]
BSTR * string);
In my implementation, it is possible for me to not modify that "BSTR
*string" if I exit with an error code:
STDMETHODIMP CMyInterface::GetString(BSTR * string)
{
if (BadThings())
return -1;
char str[100];
//...
CComBSTR bstrString(100, str);
return bstrString.CopyTo(ssid);
}
But, if BadThings() returns true, then I get an exception whenever the
GetString() function in my COM object is called.
RaiseException: Thread=86c607ac Proc=8041d8c0 'COMTestApp.exe'
AKY=00004001 PC=03fa2ea8(coredll.dll+0x00042ea8)
RA=801078b8(NK.EXE+0x000078b8) BVA=00000000 FSR=00000000
First-chance exception at 0x03fa2ea8 in COMTestApp.exe: Microsoft C++
exception: _com_error at memory location 0x1e11f870..
I call it like this:
MyLib::IMyInterfacePtr spObj= NULL;
HRESULT hr = spObj.CreateInstance(__uuidof(MyLib::MyInterface));
_bstr_t string = spObj->GetString(); //memory exception here...
Am I doing something wrong, or do I just need to make sure I always
return a non-null ptr from GetString()?
Thanks,
PaulH