I didn't know a way so here's a little C program to do it for you.
PROGRAM: RegKeyExists PURPOSE: Allows one to determine if a registry key exists from a batch file. The return value indicates if the key exists. Return values 1 - The key exists. 0 - The key does not exist. -1 - The parameter was not valid. USAGE: RegKeyExists- The complete key to query. Do NOT surround parameters with spaces with quotes. Just type the key in as it appears in the registry editor. Example Keys: HKCUSoftwareMicrosoftSourcesafeDatabases HKCR.act HKLMSoftwareMicrosoftWindows NTCurrentVersionAeDebug The first four characters of the argument indicate the section of the registry to look in: HKCR = HKEY_CLASSES_ROOT HKCC = HKEY_CURRENT_CONFIG HKCU = HKEY_CURRENT_USER HKLM = HKEY_LOCAL_MACHINE HKUR = HKEY_USERS HKPD = HKEY_PERFORMANCE_DATA HKDD = HKEY_DYN_DATA Any other values will cause a return of -1. HISTORY: 1.0 - August 6, 2001 - John Robbins (john@wintellect.com) #include "stdafx.h" HKEY ConvertPredefinedPart (TCHAR * szParam); int _tmain (int argc, _TCHAR* argv[]) { // Do the simple check. if (argc < 2) { return (-1); } // Allocate a big buffer to hold all the command line parameters. // I need to concatinate them all together to account for spaces // in the list. TCHAR * szParam = new TCHAR [2048]; szParam [0] = _T ('0'); for (int i = 1; i < argc; i++) { _tcscat (szParam , argv[i]); if (i != (argc - 1)) { // Plop a space between them. _tcscat (szParam , _T (" ")); } } // Slide past the first five characters. TCHAR * szKey = _tcschr (szParam , _T ('\')); // No slash is a problem. if (NULL == szKey) { delete [] szParam; return (-1); } // Set the slash to NULL. *szKey = _T ('0'); // Bump one past the slash. szKey++; // szParam points to the predefined key and szKey points to the // dynamic key. // The value I'll return. int iRet = -1; // Determine the predefined value. HKEY hPredefinedKey = ConvertPredefinedPart (szParam); if (0 != hPredefinedKey) { HKEY hKeyOpened = 0; // Try to open the key with the KEY_READ access, which is // the minimum read access necessary to check if the key // exists. LONG lRet = RegOpenKeyEx (hPredefinedKey, szKey, 0, KEY_READ, &hKeyOpened); if (ERROR_SUCCESS == lRet) { // Cool! It opened, so it exists. // Always close anything you open/create. RegCloseKey (hKeyOpened); // Set the return value. iRet = 1; } else { // The key does not exist. iRet = 0; } } else { iRet = -1; } // Delete the memory. delete [] szParam; return (iRet); } HKEY ConvertPredefinedPart (TCHAR * szParam) { HKEY hKey = 0; if (0 == _tcsicmp (szParam , _T ("HKCR"))) { hKey = HKEY_CLASSES_ROOT; } else if (0 == _tcsicmp (szParam , _T ("HKCC"))) { hKey = HKEY_CURRENT_CONFIG; } else if (0 == _tcsicmp (szParam , _T ("HKCU"))) { hKey = HKEY_CURRENT_USER; } else if (0 == _tcsicmp (szParam , _T ("HKLM"))) { hKey = HKEY_LOCAL_MACHINE; } else if (0 == _tcsicmp (szParam , _T ("HKUR"))) { hKey = HKEY_USERS; } else if (0 == _tcsicmp (szParam , _T ("HKPD"))) { hKey = HKEY_PERFORMANCE_DATA; } else if (0 == _tcsicmp (szParam , _T ("HKDD"))) { hKey = HKEY_DYN_DATA; } return (hKey); }
This was first published in August 2001
Join the conversationComment
Share
Comments
Results
Contribute to the conversation