HOWTO: Trapping Visual SourceSafe OLE errors

Last reviewed: October 28, 1997
Article ID: Q175758
The information in this article applies to:
  • Microsoft Visual SourceSafe, 32-bit, for Windows, version 5.0
  • Microsoft Visual Studio 97

SUMMARY

When writing an application that drives SourceSafe via OLE automation, you may want to trap the error codes defined in ssauterr.h. This file can be found at:

   http://www.microsoft.com/SSAFE/download/ssauterr.h

This article describes how to trap these error codes from Visual Basic and Visual C++.

NOTE: This cannot be done in C++ if you use the code generated by the Class Wizard to generate a wrapper for SSAPI.DLL.

MORE INFORMATION

Visual Basic Code

In Visual Basic, the error code is returned as the HelpContext property of the Err object.

An example of Visual Basic error handling code:

   Private Sub DriveVss()

   Set oSSafe = CreateObject("SourceSafe") 'assume this succeeds
   On Error GoTo errlabel

   inipath = "c:\vss\srcsafe.ini"
   user = "FredA"
   pwrd = "TopHat"


   oSSafe.Open SrcSafeIni:= inipath, Username:= user, Password:= pwrd
   Set oProject = oSSafe.VSSItem("$/myproj")
   Exit Sub

   errlabel:
   errmsg = "Source: " & Err.Source & vbCrLf & _
           "Error Number: " & Err.HelpContext & vbCrLf & _
           "Please inform your SourceSafe Admin"

   MsgBox (errmsg)
   End Sub


Visual C++ Code

In Visual C++, the error code is a member of the HRESULT structure that is returned from the call to the automation method. The DriveVSS function below is based upon the code in the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q169928
   TITLE     : HOWTO: Open a SourceSafe Database with OLE Automation in C++

where pVdb is a pointer to the IVSSDatabase interface.

You will need to add the following:

   #include <winerror.h> //for HRESULT_CODE
   #include <string.h>   //for strcpy, strcat
   #include <stdlib.h>   // for itoa

to the header files listed in Q169928.

   void DriveVSS()
   {
      void ErrHand(HRESULT hr);

      IVSSItem *pIVSSItem;
      HRESULT hr;
      BSTR bstrPath = SysAllocString(L"c:\\vss\\srcsafe.ini");
      BSTR bstrUName = SysAllocString(L"FredA");
      BSTR bstrUPass = SysAllocString(L"TopHat");
      BSTR bstrVSSSpec = SysAllocString(L"$/myproj");


      if(!SUCCEEDED(hr = pVdb->Open(bstrPath,bstrUName, bstrUPass)))
         ErrHand(hr);
    else   if(!SUCCEEDED(hr = pVdb->get_VSSItem(bstrVSSSpec,0,&pIVSSItem)))
         ErrHand(hr);
   }

   void ErrHand(HRESULT hr)
   {
   short sErrCode = HRESULT_CODE(hr);
   char cErrCode[6];
   char strMessage[50];

   strcpy(strMessage,"Error #: ");
   itoa(sErrCode, cErrCode, 10);
   strcat(strMessage, cErrCode);
   strcat(strMessage, "\n\n Please inform your SourceSafe Administrator");
   MessageBox(NULL, strMessage, NULL, MB_OK);
   }

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by David de Groot, Microsoft Corporation
Keywords          : ssother ssvb ssvc
Version           : WINDOWS:5.0; WINNT:97
Platform          : WINDOWS winnt
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.