How to trap Visual SourceSafe OLE errors (175758)



The information in this article applies to:

  • Microsoft Visual SourceSafe for Windows 5.0
  • Microsoft Visual SourceSafe for Windows 6.0
  • Microsoft Visual Studio 97

This article was previously published under Q175758

SUMMARY

When you write an application that drives SourceSafe by using OLE automation, you may want to trap the error codes that are defined in the Ssauterr.h file. Visit the following Microsoft Developer Network (MSDN) Web site and then click the "VC++ Error Header File" link: This article describes how to trap these error codes from Visual Basic and Visual C++.

Note You cannot do this in C++ if you use the code that Class Wizard generates to generate a wrapper for the Ssapi.dll file.

MORE INFORMATION

Visual Basic Code

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

The following code is 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 following DriveVSS function is based on the code in the following article in the Microsoft Knowledge Base:

169928 How to open a SourceSafe database with OLE Automation in C++

In this code, pVdb is a pointer to the IVSSDatabase interface.

Copy the following code:
   #include <winerror.h> //for HRESULT_CODE
   #include <string.h>   //for strcpy, strcat
   #include <stdlib.h>   // for itoa
				
Paste the code in the header files that are listed in 169928.
   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

Modification Type:MajorLast Reviewed:4/28/2004
Keywords:kbhowto kbinterop KB175758