c++レジストリ操作

14151 ワード

BOOL fIsBrowser(std::string& paraStrBrowser)
{//         

	if (std::string::npos != paraStrBrowser.find("Opera"))
	{
		paraStrBrowser = "Opera" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Internet Explorer"))
	{
		paraStrBrowser = "IE" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Firefox"))
	{
		paraStrBrowser = "Firefox" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Safari"))
	{
		paraStrBrowser = "Safari" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Tencent Traveler"))
	{
		paraStrBrowser = "  TT" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("       "))
	{
		paraStrBrowser = "Sogo" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("  "))
	{
		paraStrBrowser = "  " ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("360     "))
	{
		paraStrBrowser = "360" ;
		return TRUE ;
	}
	else if (std::string::npos != paraStrBrowser.find("Chrome"))
	{
		paraStrBrowser = "Chrome" ;
		return TRUE ;
	}

	paraStrBrowser.erase() ;
	return FALSE ;
}

std::string strGetSubKeyValue(HKEY paraHKey, std::string& paraStrRegPath)
{
	HKEY hKey;
	long lRet = RegOpenKeyEx(paraHKey, paraStrRegPath.c_str(), 0, KEY_READ,&hKey);
	if (0 != lRet)
	{
		MessageBox(NULL, "              ", "Error", MB_OK) ;
		exit(1) ;
	}

	int		dwIndex = 0 ; 
	char	szValueName[MAX_PATH] = {0} ; 
	DWORD	dwValueVameLen = MAX_PATH ; 
	DWORD	dwType; 
	union
	{
		BYTE data[1024]; 
		DWORD idata; 
	}lpdata; 
	DWORD dwDataLen = sizeof(lpdata); 
	DWORD Type ;
	memset(&lpdata, 0, sizeof(lpdata)); 
	while(RegEnumValue(hKey, dwIndex, (LPTSTR)szValueName, &dwValueVameLen, 0, &Type, lpdata.data, &dwDataLen) != ERROR_NO_MORE_ITEMS) 
	{ 
		if (!strcmp(szValueName, "DisplayName"))
		{//     Value      

			std::string strTemp((char*)lpdata.data) ;
			if (fIsBrowser(strTemp))
			{//      ,          ,    
				return strTemp ;
			}
			else
			{
				return "" ;
			}
		}

		dwIndex++; 
		dwValueVameLen  =   sizeof(szValueName); 
		dwDataLen		=   sizeof(lpdata); 
		memset(&lpdata, 0, sizeof(lpdata)); 
	}

	RegCloseKey(hKey) ;

	return "" ;
}

std::string& strGetS_1_5_21Path(std::string& paraStrPath)
{
	const int MAX_KEY_LENGTH = 255 ;
	const int MAX_VALUE_NAME = 16383 ;

	HKEY hKey ;
	if (0 != RegOpenKeyEx(HKEY_USERS, NULL, 0, KEY_READ, &hKey))
	{
		MessageBox(NULL, "             ", "Error", MB_OK) ;
		exit(1) ;
	}

	DWORD dwCntSubKeys = 0 ;
	if(RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwCntSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
	{
		MessageBox(NULL, "             ", "Error", MB_OK) ;
		exit(1) ;
	}

	char			szKey[MAX_KEY_LENGTH] = {0} ;
	DWORD			dwKeyLength = MAX_KEY_LENGTH ;
	for (int i = 0; i < dwCntSubKeys; ++i)
	{
		memset(szKey, 0 ,MAX_KEY_LENGTH) ;
		dwKeyLength = MAX_KEY_LENGTH ;
		//DWORD n1 = RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL) ;
		if (RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL))
		{
			MessageBox(NULL, "             ", "Error", MB_OK) ;
			exit(1) ;
		}
		else
		{
			//   SubKey Reg  
			paraStrPath = szKey ;
			if (std::string::npos != paraStrPath.find("S-1-5-21-") && std::string::npos == paraStrPath.find("_Classes"))
			{
				paraStrPath += "\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" ;
				return paraStrPath ;
			}
		}
	}

	return paraStrPath ;
}

vector<pair<std::string, float>>& vecGetBrowser(HKEY hKeyRoot, HKEY& hKey, const std::string& strRegPath, vector<pair<std::string, float>>& retVecBrowser)
{
	//vector<pair<std::string, float>> retVecBrowser ;

	const int MAX_KEY_LENGTH = 255 ;
	const int MAX_VALUE_NAME = 16383 ;

	//std::string strRegPath("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;

	//          
	//HKEY hKey ;
	if (0 != RegOpenKeyEx(hKeyRoot, strRegPath.c_str(), 0, KEY_READ, &hKey))
	{
		MessageBox(NULL, "             ", "Error", MB_OK) ;
		exit(1) ;
	}

	//         Key Subkey
	DWORD dwCntSubKeys = 0 ;
	if(RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwCntSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
	{
		MessageBox(NULL, "             ", "Error", MB_OK) ;
		exit(1) ;
	}

	if (!dwCntSubKeys)
	{
		return retVecBrowser ;
	}

	char			szKey[MAX_KEY_LENGTH] = {0} ;
	DWORD			dwKeyLength = MAX_KEY_LENGTH ;
	std::string		strTmp("") ;
	std::string		strRegPathTmp("") ;
	for (int i = 0; i < dwCntSubKeys; ++i)
	{
		memset(szKey, 0 ,MAX_KEY_LENGTH) ;
		dwKeyLength = MAX_KEY_LENGTH ;
		//DWORD n1 = RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL) ;
		if (RegEnumKeyEx(hKey, i, szKey, &dwKeyLength, NULL, NULL, NULL, NULL))
		{
			MessageBox(NULL, "             ", "Error", MB_OK) ;
			exit(1) ;
		}
		else
		{
			//   SubKey Reg  
			strRegPathTmp = strRegPath ;
			strRegPathTmp += szKey ;
			strRegPathTmp += "\\" ;

			//   SubKey Value,        
			strTmp = strGetSubKeyValue(hKeyRoot, strRegPathTmp) ;
			if (!strTmp.empty())
			{	
				pair<std::string, float> pairTemp ;
				pairTemp.first	= strTmp ;
				pairTemp.second = 0 ;

				//            ,      
				if (retVecBrowser.end() == find(retVecBrowser.begin(), retVecBrowser.end(), pairTemp))
				{
					retVecBrowser.push_back(pairTemp) ;
				}
			}
		}
	}

	return retVecBrowser ;
}

void vCaculateAverage(vector<pair<std::string, float>>& retVecBrowser)
{
	for (int i = 0; i < retVecBrowser.size(); ++i)
	{
		retVecBrowser[i].second = float(1) / retVecBrowser.size() ;
	}
}
  
//          
std::string strPath("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;
vecGetBrowser(HKEY_LOCAL_MACHINE, hKey, strPath, pBrowser) ;
std::string strS_1_5_21Path("") ;
vecGetBrowser(HKEY_USERS, hKey, strGetS_1_5_21Path(strS_1_5_21Path), pBrowser) ;
vCaculateAverage(pBrowser) ;
            
// QueryKey - Enumerates the subkeys of key and its associated values.
//     hKey - Key whose subkeys and values are to be enumerated.
 
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
 
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
 
void QueryKey(HKEY hKey) 
{ 
    TCHAR    achKey[MAX_KEY_LENGTH];   // buffer for subkey name
    DWORD    cbName;                   // size of name string 
    TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
    DWORD    cchClassName = MAX_PATH;  // size of class string 
    DWORD    cSubKeys=0;               // number of subkeys 
    DWORD    cbMaxSubKey;              // longest subkey size 
    DWORD    cchMaxClass;              // longest class string 
    DWORD    cValues;              // number of values for key 
    DWORD    cchMaxValue;          // longest value name 
    DWORD    cbMaxValueData;       // longest value data 
    DWORD    cbSecurityDescriptor; // size of security descriptor 
    FILETIME ftLastWriteTime;      // last write time 
 
    DWORD i, retCode; 
 
    TCHAR  achValue[MAX_VALUE_NAME]; 
    DWORD cchValue = MAX_VALUE_NAME; 
 
    // Get the class name and the value count. 
    retCode = RegQueryInfoKey(
        hKey,                    // key handle 
        achClass,                // buffer for class name 
        &cchClassName,           // size of class string 
        NULL,                    // reserved 
        &cSubKeys,               // number of subkeys 
        &cbMaxSubKey,            // longest subkey size 
        &cchMaxClass,            // longest class string 
        &cValues,                // number of values for this key 
        &cchMaxValue,            // longest value name 
        &cbMaxValueData,         // longest value data 
        &cbSecurityDescriptor,   // security descriptor 
        &ftLastWriteTime);       // last write time 
 
    // Enumerate the subkeys, until RegEnumKeyEx fails.
    
    if (cSubKeys)
    {
        printf( "
Number of subkeys: %d
", cSubKeys); for (i=0; i<cSubKeys; i++) { cbName = MAX_KEY_LENGTH; retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime); if (retCode == ERROR_SUCCESS) { _tprintf(TEXT("(%d) %s
"), i+1, achKey); } } } // Enumerate the key values. if (cValues) { printf( "
Number of values: %d
", cValues); for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) { cchValue = MAX_VALUE_NAME; achValue[0] = '\0'; retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, NULL, NULL, NULL); if (retCode == ERROR_SUCCESS ) { _tprintf(TEXT("(%d) %s
"), i+1, achValue); } } } } void __cdecl _tmain(void) { HKEY hTestKey; if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft"), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS ) { QueryKey(hTestKey); } RegCloseKey(hTestKey); }
もう つの
// reg.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include "windows.h"
#include "malloc.h"
 
int main(int argc, char* argv[])
{   
    DWORD dwIndex=0,NameSize,NameCnt,NameMaxLen,Type;
    DWORD KeySize,KeyCnt,KeyMaxLen,DateSize,MaxDateLen; 
    HKEY hKey;
    char *szKeyName;
    char *szValueName;
    LPBYTE szValueDate;
    //       ---------------------------------------------------------------
    LPCTSTR SubKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,SubKey,0,KEY_ALL_ACCESS,&hKey)!=
        ERROR_SUCCESS)
    {
        printf("RegOpenKeyEx  ");
        return 0;
    }
    //      ---------------------------------------------------------------
    if(RegQueryInfoKey(hKey,NULL,NULL,NULL,&KeyCnt,&KeyMaxLen,NULL,&NameCnt,&NameMaxLen,&MaxDateLen,NULL,NULL)!=ERROR_SUCCESS)
    {
        printf("RegQueryInfoKey  ");
        ::RegCloseKey(hKey);
        return 0;
    }
    //      ---------------------------------------------------------------
    for(dwIndex=0;dwIndex<KeyCnt;dwIndex++)        //    
    {
        KeySize=KeyMaxLen+1;            //  RegQueryInfoKey        0    ,    1
        szKeyName=(char*)malloc(KeySize);
        RegEnumKeyEx(hKey,dwIndex,szKeyName,&KeySize,NULL,NULL,NULL,NULL);//    
        printf("%s
",szKeyName); } // --------------------------------------------------------------- for(dwIndex=0;dwIndex<NameCnt;dwIndex++) // { DateSize=MaxDateLen+1; NameSize=NameMaxLen+1; szValueName=(char *)malloc(NameSize); szValueDate=(LPBYTE)malloc(DateSize); RegEnumValue(hKey,dwIndex,szValueName,&NameSize,NULL,&Type,szValueDate,&DateSize);// if(Type==REG_SZ) { ///* ......*/ printf("%s
",szValueName); } if(Type==REG_DWORD) { } } RegCloseKey(hKey); // --------------------------------------------------------------- if (ERROR_SUCCESS!=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\MyKey",&hKey)) { printf(" !
"); return 0; } else { printf(" !
"); } if(ERROR_SUCCESS==RegDeleteKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\MyKey")) { printf(" !
"); } else { printf(" !
"); RegCloseKey(hKey); return 0; } RegCloseKey(hKey); // --------------------------------------------------------------- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,SubKey,0,KEY_ALL_ACCESS,&hKey)!=ERROR_SUCCESS) { printf(" HKEY !
"); return 0; } char *szValueName1="QQ"; char *szValueDate1="This is QQ"; UINT cbLen=strlen(szValueDate1); char *szValueName2="TT"; UINT tmp=16; UINT *szValueDate2=&tmp; if(RegSetValueEx(hKey,szValueName1,0,REG_SZ,(const unsigned char *)szValueDate1,cbLen)==ERROR_SUCCESS) { printf(" REG_SZ !
"); } else { printf(" REG_SZ !
"); return 0; } if(RegSetValueEx(hKey,szValueName2,0,REG_DWORD,(const unsigned char *)szValueDate2,4)==ERROR_SUCCESS) { printf(" REG_DWORD !
"); } else { printf(" REG_DWORD !
"); RegCloseKey(hKey); return 0; } RegCloseKey(hKey); return 0; }