2011-11-15 124 views
0

從msdn.microsoft.com - 枚舉註冊表子項:枚舉註冊表子項

http://msdn.microsoft.com/En-US/library/ms724256.aspx

// 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("\nNumber of subkeys: %d\n", 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\n"), i+1, achKey); 
      } 
     } 
    } 

    // Enumerate the key values. 

    if (cValues) 
    { 
     printf("\nNumber of values: %d\n", 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\n"), 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); 
} 

我如何可以修改代碼以:

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\n"), i+1, achValue); 
      } 

輸出到控制檯的所有RegEnumValue函數可以返回的值, 函數在msdn上: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724865%28v=vs.85%29.aspx 我要輸出這些瓦爾太:

__out  LPTSTR lpValueName, 
    __inout  LPDWORD lpcchValueName, 
    __out_opt LPDWORD lpType, 
    __out_opt LPBYTE lpData, 
    __inout_opt LPDWORD lpcbData 

我已經嘗試不同的東西,但每次我改變任何NULL變種從 該功能:

RETCODE = RegEnumValue(的hKey,我, achValue, & cchValue, NULL, NULL, NULL, NULL);

我甚至鴕鳥政策得到achValue

感謝使用Windows 7 64位視覺很多

PD Studio 2010的最終

回答

1

第一四個NULL值是lpReserved和必須設置爲NULL。

第二個是lpType,你應該可以自己得到那個。

第三和第四被配對,並且必須被設置爲NULL或兩個非空。它們的功能非常類似於achValuecchValue,其中第一個是用於接收數據的緩衝區,第二個是指向必須首先具有緩衝區大小然後用數據大小填充的大小的指針。

下面的代碼工作我的Vista機器上:

 const DWORD maxValueBytes=300; 
     BYTE valueBytes[maxValueBytes]; 
     DWORD valueSize=maxValueBytes; 
     DWORD valueType=0; 
     cchValue = MAX_VALUE_NAME; 
     achValue[0] = '\0'; 
     retCode = RegEnumValue(hKey, i, 
      achValue, 
      &cchValue, 
      NULL, 
      &valueType, 
      valueBytes, 
      &valueSize); 

     if (retCode == ERROR_SUCCESS) 
     { 
      _tprintf(TEXT("(%d) %s (%d - %d bytes)\n"), i+1, achValue,valueType,valueSize); 
      switch (valueType) { 
       case REG_BINARY: 
        _tprintf(TEXT(" The value is binary (0x%X, 0x%X, 0x%X ...)\n"),valueBytes[0],valueBytes[1],valueBytes[2]); 
        break; 
       case REG_DWORD: 
       //case REG_DWORD_LITTLE_ENDIAN: 
        _tprintf(TEXT(" The value is a DWORD (%d)\n"),*(DWORD *)valueBytes); 
        break; 
       case REG_DWORD_BIG_ENDIAN: 
        _tprintf(TEXT(" The value is a DWORD (big endian) (%d)\n"),(valueBytes[0]<<24)|(valueBytes[1]<<16)|(valueBytes[2]<<8)|valueBytes[3]); 
        break; 
       case REG_EXPAND_SZ: 
       case REG_SZ: 
        _tprintf(TEXT(" The value is a string\n")); 
        break; 
       case REG_LINK: 
        _tprintf(TEXT(" The value is a link\n")); 
        break; 
       case REG_MULTI_SZ: 
        _tprintf(TEXT(" The value is a multi-string\n")); 
        break; 
       case REG_NONE: 
        _tprintf(TEXT(" There is no spoon... sorry, value\n")); 
        break; 
       case REG_RESOURCE_LIST: 
        _tprintf(TEXT(" The value is a resource list\n")); 
        break; 
       default: 
        _tprintf(TEXT(" Unknown value type\n")); 
        break; 
      } 
     } 
     else 
     { 
      _tprintf(TEXT("error reading value %d - %d\n"),i+1,retCode); 
     } 
+0

很好的贏得了勝利7非常感謝 –

+0

@thor不客氣。如果這個答案對你有幫助,你應該點擊左邊的複選標記來接受它。您應該對您提出的其他問題也做同樣的事情。 – IronMensan

+0

RegEnumValue支持windows xp下? –

2

爲什麼不使用.NET Framework類與註冊表的工作?

http://msdn.microsoft.com/en-us/library/df4afx57%28v=vs.80%29.aspx

+0

也許他不希望他的可執行文件依賴於.NET框架。 –

+0

@NorbertWillhelm他正在使用Windows註冊表,因此他不太可能擁有不合法的理由。 (不說不可能,只是不太可能) –

0

爲了幫助有問題與枚舉註冊表子項其他人:

我的一些鍵沒有被RegEnumKeyEx「看」() 。 我花了半天時間才明​​白我的問題是32/64位問題。

默認情況下,在WOW64上運行的32位應用程序將訪問32位註冊表視圖,並且64位應用程序將訪問64位註冊表視圖。

MSDN : Alternate Registry View找出如何正確設置samDesired參數。