Testing return values

You can store return values in an object that encapsulates a COM data type. Then you can use one of that type’s operations to test it. In this example, the return value is stored in the _bstr_t data type, which allows us to use the length operation to test whether the result is an empty string; and the copy operation to construct a copy of the encapsulated BSTR:

   // Let the user select a file using the browser
   l_pFileBrowser->ShowOpen();

   // Get the selected file (empty string if none was selected)
   _bstr_t l_bstrSelected( l_pFileBrowser->FilePathName );

   // If no file was selected
   if (!l_bstrSelected.length())
   {
       return S_FALSE;
   }

   // Copy the string to a new pointer
   *out_pSelectedFile = l_bstrSelected.copy();

This is the equivalent to the following VBScript snippet:

   ' Let the user select a file using the browser
   oFileBrowser.ShowOpen

   ' Get the selected file
   sPath = oFileBrowser.FilePathName
   If sPath = "" Then
       bFileOpened = False
   End If

   ' Copy the string to a new variable
   sPath = sNewPathName