1 #include <windows.h> 2 #include <stdio.h> 3 #include <tchar.h> 4 5 #define MAX_KEY_LENGTH 255 6 #define MAX_VALUE_NAME 16383 7 8 void QueryKey(HKEY hKey) 9 { 10 TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name 11 DWORD cbName; // size of name string 12 TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name 13 DWORD cchClassName = MAX_PATH; // size of class string 14 DWORD cSubKeys=0; // number of subkeys 15 DWORD cbMaxSubKey; // longest subkey size 16 DWORD cchMaxClass; // longest class string 17 DWORD cValues; // number of values for key 18 DWORD cchMaxValue; // longest value name 19 DWORD cbMaxValueData; // longest value data 20 DWORD cbSecurityDescriptor; // size of security descriptor 21 FILETIME ftLastWriteTime; // last write time 22 23 DWORD i, retCode; 24 25 TCHAR achValue[MAX_VALUE_NAME]; 26 DWORD cchValue = MAX_VALUE_NAME; 27 28 // Get the class name and the value count. 29 retCode = RegQueryInfoKey( 30 hKey, // key handle 31 achClass, // buffer for class name 32 &cchClassName, // size of class string 33 NULL, // reserved 34 &cSubKeys, // number of subkeys 35 &cbMaxSubKey, // longest subkey size 36 &cchMaxClass, // longest class string 37 &cValues, // number of values for this key 38 &cchMaxValue, // longest value name 39 &cbMaxValueData, // longest value data 40 &cbSecurityDescriptor, // security descriptor 41 &ftLastWriteTime); // last write time 42 43 // Enumerate the subkeys, until RegEnumKeyEx fails. 44 45 if (cSubKeys) 46 { 47 printf( "\nNumber of subkeys: %d\n", cSubKeys); 48 49 for (i=0; i<cSubKeys; i++) 50 { 51 cbName = MAX_KEY_LENGTH; 52 retCode = RegEnumKeyEx(hKey, i, 53 achKey, 54 &cbName, 55 NULL, 56 NULL, 57 NULL, 58 &ftLastWriteTime); 59 if (retCode == ERROR_SUCCESS) 60 { 61 _tprintf(TEXT("(%d) %s\n"), i+1, achKey); 62 } 63 } 64 } 65 66 // Enumerate the key values. 67 68 if (cValues) 69 { 70 printf( "\nNumber of values: %d\n", cValues); 71 72 for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 73 { 74 cchValue = MAX_VALUE_NAME; 75 achValue[0] = ‘\0‘; 76 retCode = RegEnumValue(hKey, i, 77 achValue, 78 &cchValue, 79 NULL, 80 NULL, 81 NULL, 82 NULL); 83 84 if (retCode == ERROR_SUCCESS ) 85 { 86 _tprintf(TEXT("(%d) %s\n"), i+1, achValue); 87 } 88 } 89 } 90 } 91 92 //void __cdecl _tmain(void) 93 //{ 94 // HKEY hTestKey; 95 // 96 // if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, 97 // TEXT("SOFTWARE\\Autodesk"), 98 // 0, 99 // KEY_READ, 100 // &hTestKey) == ERROR_SUCCESS 101 // ) 102 // { 103 // QueryKey(hTestKey); 104 // } 105 // 106 // RegCloseKey(hTestKey); 107 //} 108 109 //int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 110 int main(){ 111 HKEY hTestKey; 112 113 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, 114 TEXT("SOFTWARE\\Autodesk"), 115 0, 116 KEY_READ, 117 &hTestKey) == ERROR_SUCCESS 118 ) 119 { 120 QueryKey(hTestKey); 121 } 122 123 RegCloseKey(hTestKey); 124 }
时间: 2024-10-19 17:49:00