Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C++

A Context Menu Handler for Windows Users That Can't Let Go

Rate me:
Please Sign up or sign in to vote.
4.79/5 (13 votes)
31 May 20062 min read 126.2K   2.2K   39  
A context menu handler that makes it easier to work with paths, command windows, and program arguments from within Explorer.
#define _CCPERSIST_CPP_
#include "StdAfx.h"
#include ".\ccpersist.h"

LPCTSTR g_szCMDCMX_RegKey = TEXT("Software\\Harrington\\CMDCMX");
LPCTSTR g_szCMDCMXRun_RegKey = TEXT("Software\\Harrington\\CMDCMX\\Run Param History");
LPCTSTR g_szCMDCMXApplicationForDosBox_StringValue = TEXT("Application For Command Window");
LPCTSTR g_szCMDCMXShowRunOnFileMenuItem_DwordValue = TEXT("Show Run MenuItem");
LPCTSTR g_szCMDCMXShowRunOnFileMenuItem_StringValue = TEXT("Add Run MenuItem To");
LPCTSTR g_szCMDCMXShowOpenInCMDWNDMenuItem_DwordValue = TEXT("Show OpenInCMDWindow MenuItem");
LPCTSTR g_szCMDCMXShowCopyPathNameMenuItem_DwordValue = TEXT("Show CopyPathName MenuItem");
LPCTSTR g_szCMDCMXShowCopyShortPathNameMenuItem_DwordValue = TEXT("Show CopyShortPathName MenuItem");

CPersist::CPersist(void)
{
}

CPersist::~CPersist(void)
{
}

HRESULT CPersist::ShowMenuItem( int& nShow, LPCTSTR szKey )
{
	HRESULT hRes;
	DWORD dwDisp;
	HKEY hKey = NULL;

	// Open the key... create it if it doesn't already exist.
	if( (hRes = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, g_szCMDCMX_RegKey, 0, NULL,
		REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &hKey, &dwDisp)) == ERROR_SUCCESS )
	{
		DWORD dwType, dw = sizeof(int);
		// default to true.
		nShow = 1;
		hRes = ::RegQueryValueEx(  hKey, szKey, 0, &dwType, (UCHAR*)&nShow, &dw );
		::RegCloseKey( hKey );
	}
	return hRes;
}

int CPersist::ReadFromRegistry_FileMenuItemsToAdd( bool& bShowRunOnFileMenuItem, bool& bShowOpenInDosMenuItem, bool& bShowCopyPathNameMenuItem, bool& bShowCopyShortPathNameMenuItem )
{
	int nShow = 0;
	int nShowCount = 0;

	nShowCount = ReadFromRegistry_FolderMenuItemsToAdd(bShowOpenInDosMenuItem, bShowCopyPathNameMenuItem, bShowCopyShortPathNameMenuItem);

	ShowMenuItem( nShow, g_szCMDCMXShowRunOnFileMenuItem_DwordValue );
	if( nShow )
	{
		bShowRunOnFileMenuItem = true;
		nShowCount++;
	}
	return nShowCount;
}

int CPersist::ReadFromRegistry_FolderMenuItemsToAdd( bool& bShowOpenInDosMenuItem, bool& bShowCopyPathNameMenuItem, bool& bShowCopyShortPathNameMenuItem )
{
	int nShow = 0;
	int nShowCount = 0;

	ShowMenuItem( nShow, g_szCMDCMXShowOpenInCMDWNDMenuItem_DwordValue );
	if( nShow )
	{
		bShowOpenInDosMenuItem = true;
		nShowCount++;
	}

	ShowMenuItem( nShow, g_szCMDCMXShowCopyPathNameMenuItem_DwordValue );
	if( nShow )
	{
		bShowCopyPathNameMenuItem = true;
		nShowCount++;
	}

	ShowMenuItem( nShow, g_szCMDCMXShowCopyShortPathNameMenuItem_DwordValue );
	if( nShow )
	{
		bShowCopyShortPathNameMenuItem = true;
		nShowCount++;
	}
	return nShowCount;
}

int CPersist::ReadFromRegistry_FileTypesToAddRunMenuItemTo( LPTSTR pszTypes )
{
	DWORD dwDisp;
	HKEY hKey = NULL;
	HRESULT hRes = E_FAIL;

	// Open the key... create it if it doesn't already exist.
	if( ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, g_szCMDCMX_RegKey, 0, NULL,
		REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &hKey, &dwDisp) == ERROR_SUCCESS )
	{
		DWORD dwType, dw = (2048*sizeof(TCHAR));
		_tcscpy( pszTypes, TEXT("exe, com, bat") );
		hRes = ::RegQueryValueEx(  hKey, g_szCMDCMXShowRunOnFileMenuItem_StringValue, 0, &dwType, (UCHAR*)pszTypes, &dw );
		::RegCloseKey( hKey );
	}
	if( SUCCEEDED(hRes) )
	{
		int i = 0;
		while( pszTypes[i] != NULL )
		{
			pszTypes[i] = LOWORD(tolower(pszTypes[i]));
			i++;
		}
		return 1;
	}
	return 0;
}

int CPersist::ReadFromRegistry_RunApplication( LPTSTR lptstrApplication )
{
	DWORD dwDisp;
	HKEY hKey = NULL;
	HRESULT hRes = E_FAIL;

	// Open the key... create it if it doesn't already exist.
	if( (hRes = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, g_szCMDCMX_RegKey, 0, NULL,
		REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &hKey, &dwDisp)) == ERROR_SUCCESS )
	{
		DWORD dwType = REG_SZ, dw = (MAX_PATH*sizeof(TCHAR));
		DWORD dwVersion = ::GetVersion(); // Get major and minor version numbers of Windows
		// Do the right thing for the OS - WinNT wants cmd.exe, Win95 wants command.com.
		if (dwVersion < 0x80000000)                // Windows NT
			_tcscpy( lptstrApplication, TEXT("cmd.exe") );
		else
			_tcscpy( lptstrApplication, TEXT("command.com") );

		hRes = ::RegQueryValueEx(  hKey, g_szCMDCMXApplicationForDosBox_StringValue, 0, &dwType, (UCHAR*)lptstrApplication, &dw );
		::RegCloseKey( hKey );

		if( SUCCEEDED(hRes) )
			return 1;
	}
	return 0;
}

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
Web Developer
United States United States
16yrs of GUI programming experience gained at: (most recent first) BlackBall, Veritas, Seagate Software, Arcada, Stac, Mountain, and Emerald Systems.

Languages/Scripting: C, C++, JAVA, BASIC, JAVASCRIPT, HTML, XML, PHP, and SQL

Tools: MS Visual Studio, MS Visual SourceSafe, CVS, PVCS, Bounds Checker, VMWare, ToDoList, InstallShield, and Office Applications

Libraries and API: RTL, STL, WIN32, MFC, ATL, .NET, ActiveX, DirectX, COM, DCOM, Shell Extensions, and Shell Namespaces

Strengths: Honest, communicative, keen eye for usability, good at estimating workload and completion dates, ready to take on grunt work, team player, experienced working with QA, localization, Tech Pubs, Sales, and Marketing teams.

Comments and Discussions