Click here to Skip to main content
15,895,656 members
Articles / Desktop Programming / WTL

SSBrowser: A Sample Visual SourceSafe Automation

Rate me:
Please Sign up or sign in to vote.
4.59/5 (13 votes)
3 Jun 20044 min read 92.9K   3K   54  
A sample VC++ program for performing Visual SourceSafe operations in your application.
#if !defined(_SSCONNECTION_H_INCLUDED_)
#define _SSCONNECTION_H_INCLUDED_

#include "stdafx.h"

class CSSConnection
{
protected:
	CComBSTR		mstr_baseLocalDirectory;
	CComBSTR		mstr_baseSourcesafeProject;

	// Visual SourceSafe Database
	IVSSDatabasePtr mp_vssDatabase;
	BOOL mb_connected;

public:

	CSSConnection():mb_connected(FALSE)
	{
		mstr_baseSourcesafeProject = _T("$");	// root
		mstr_baseLocalDirectory = _T("");

	}

	virtual ~CSSConnection()
	{
	}


	BOOL b_Connect(LPCTSTR psz_User, LPCTSTR psz_Password, LPCTSTR psz_VSSini=NULL)
	{
		mb_connected = FALSE;


		 CLSID clsid;
		 IClassFactory *pClf;
     
		try
		{

			v_TestHr(CLSIDFromProgID(L"SourceSafe", &clsid ));
			v_TestHr(CoGetClassObject( clsid, CLSCTX_ALL, NULL, IID_IClassFactory, (void**)&pClf ));
			v_TestHr(pClf->CreateInstance( NULL, IID_IVSSDatabase, (void **) &mp_vssDatabase ));
         
			v_TestHr(mp_vssDatabase->Open((CComBSTR)psz_VSSini, 
				(CComBSTR)psz_User, 
				(CComBSTR)psz_Password));

			mb_connected = TRUE;
		}
		catch (...)
		{
			b_DisplayAnyError();
		}

		return mb_connected;
	}

	BOOL b_IsConnected()
	{
		return mb_connected;
	}

	CString str_GetBaseSourcesafeProject()
	{
		return CString(static_cast<LPCTSTR>(mstr_baseSourcesafeProject));
	}

	CString str_GetBaseLocalDirectory()
	{
		return CString(static_cast<LPCTSTR>(mstr_baseLocalDirectory));
	}

	IVSSDatabasePtr p_GetSourcesafeDatabase()
	{
		return mp_vssDatabase;
	}

	CString str_GetSrcSafeIni()
	{
		ATLASSERT(mp_vssDatabase!=NULL);
		CComBSTR str_ini;
		mp_vssDatabase->get_SrcSafeIni(&str_ini);

		return CString(static_cast<LPCTSTR>(str_ini));
	}

	CString str_GetUser()
	{
		ATLASSERT(mp_vssDatabase!=NULL);
		CComBSTR str_user;
		mp_vssDatabase->get_UserName(&str_user);

		return CString(static_cast<LPCTSTR>(str_user));
	}

	BOOL b_SetSourcesafeProject(LPCTSTR psz_BaseSourcesafeProject=NULL)
	{
		try
		{
			mstr_baseSourcesafeProject = psz_BaseSourcesafeProject;

			IVSSItemPtr vss_Item = NULL;
			if (psz_BaseSourcesafeProject==NULL)
			{
				mp_vssDatabase->get_CurrentProject(&mstr_baseSourcesafeProject);
			}

			mp_vssDatabase->get_VSSItem(mstr_baseSourcesafeProject,FALSE, &vss_Item);
			if (vss_Item == NULL)
			{
				CString str_error;
				str_error.Format(TEXT("%s was not found in the Sourcesafe database."), mstr_baseSourcesafeProject);
				throw str_error;
			}

			int i_type;
			vss_Item->get_Type(&i_type);
			if (i_type != VSSITEM_PROJECT)
			{
				CString str_error;
				str_error.Format(TEXT("%s is not a Sourcesafe Project."), mstr_baseSourcesafeProject);
				throw str_error;
			}

			vss_Item->get_Spec(&mstr_baseSourcesafeProject);
			vss_Item->get_LocalSpec(&mstr_baseLocalDirectory);
			mp_vssDatabase->put_CurrentProject(mstr_baseSourcesafeProject);

			return TRUE;
		}
		catch (...)
		{
			b_DisplayAnyError();
		}

		return FALSE;
	}

};


#endif // _SSCONNECTION_H_INCLUDED_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Roaming halfway around the globe programming in C++, MFC, COM/ATL, WTL, C#, .NET, OLEDB, ADO, ADO/X.

Living under the pleasant weather of Irvine, California, Ferdie is a Computer Engineering graduate of Mapua Institute of Technology (MIT Smile | :) ) in Philippines. Developed GIS applications in Japan for 5 years. Now a member of a team developing Windows GUI and real time software for semi-robotic equipments.

Comments and Discussions