Enum Registry Key and Value

Enum Registry Key and Value

Enum Registry Key

使用 RegEnumKeyEx api。以下範例列舉所有 UnInstall 軟體的資訊。


#include <stdio.h>
 
void EnumReg(LPTSTR lpSubKey) 
{
	HKEY hKey, subKey;
	LONG lResult;
	char displayName[MAX_PATH], newKey[MAX_PATH], chKey[MAX_PATH];
	DWORD dwSize=MAX_PATH, retCode;
	FILETIME fTime;
	int i;
	 
	lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey , 0, KEY_READ, &hKey);
 
	for(i=0, retCode = ERROR_SUCCESS; retCode != ERROR_NO_MORE_ITEMS; i++) 
	{
		dwSize = MAX_PATH;
		retCode = RegEnumKeyEx(hKey, i, chKey, &dwSize, NULL, NULL, NULL, &fTime);
		sprintf(newKey, "%s\\%s", lpSubKey, chKey);
		lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, newKey , 0, KEY_READ, &subKey);
		if(lResult == ERROR_SUCCESS) 
		{
			dwSize = MAX_PATH;
			lResult = RegQueryValueEx(subKey, "DisplayName", 0, NULL, (LPBYTE)displayName, &dwSize);
			if (lResult == ERROR_SUCCESS) printf("%s\n", displayName);
		}
		RegCloseKey(subKey);
	}
	RegCloseKey(hKey);
}
 
int main() 
{
	EnumReg("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
	return 0;
}

Enum Registry Value

使用 RegEnumValue api。以下範例列舉所有 usb audio device的資訊。


#include <stdio.h>
 
void main()
{
	HKEY hKey;
	LONG lReturn;
	DWORD dwValue = 0, dwType, dwData;
	DWORD dwValName;
	TCHAR szValueName[256];
	TCHAR szData[1024];
	BOOL bIsPath;
	 
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			"SYSTEM\\CurrentControlSet\\Services\\usbaudio",
			0, KEY_READ, &hKey) != ERROR_SUCCESS)
	{
		RegCloseKey(hKey);
		return;
	}
	else
		bIsPath=TRUE;
	 
	while(bIsPath==TRUE)  
	{
		dwData = sizeof(szData);
		dwValName = sizeof(szValueName);
		 
		lReturn = RegEnumValue(hKey, dwValue++, szValueName, &dwValName,
		NULL, &dwType, (UCHAR*)szData, &dwData);
		if(lReturn == ERROR_NO_MORE_ITEMS)
			break;
		printf("%s = %s\n", szValueName, szData);
	}
	 
	RegCloseKey(hKey);
}
Dotblogs 的標籤: