2016-04-22 42 views
0

我仍然是C的初學者,我發現一個有用的代碼在C whcih幫助我得到一個關鍵值,但我試圖打印(「SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion」 )與所有它的值和數據,但我只能得到沒有數據的值,我想獲得這兩個值,它的數據就像(ProductName:Windows 7,SystemRoot:C:\ Windows等...),如果有人可以用這段代碼幫助我,我會很感激,我使用了「RegQueryValueEx」,但它只給了我一個單一值的特定數據。枚舉註冊表項與它的數據在C

感謝

#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,achClass,&cchClassName,NULL,&cSubKeys,&cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDescriptor,&ftLastWriteTime); 
    // 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 _tmain(void) 
{ 
    HKEY hTestKey; 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
     TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"),0, KEY_READ, &hTestKey) == ERROR_SUCCESS 
    ) 

    { 
     QueryKey(hTestKey); 

    } 
    RegCloseKey(hTestKey); 
} 
+0

花一些時間來學習什麼都那些看起來像是空的奇序列的功能,特別是參數,其實* *做。它可能證明是有用的。 – WhozCraig

+0

其實我知道大部分的功能,我知道如何從RegQueryValueEX函數獲取單個數據的值,但我只是不知道如何獲取所有數據而不是單一的RegQueryValueEX – Student

回答

0

爲什麼不喜歡REGEDIT -E thorugh C程序運行命令,通過這個就可以輕鬆導出全部價值,然後使用文件處理mechnism在C. 請按照鏈接解讀爲正常的文件。
http://www.computerhope.com/issues/ch000848.htm

+0

嗯,我想知道如何使用RegQueryValueEX。 – Student