-
读取REG_SZ 类型的注册表键值
1 // 读取 REG_SZ 类型键值的代码 2 3 HKEY hKey = NULL; 4 DWORD dwSize = 0; 5 DWORD dwDataType = 0; 6 LPBYTE lpValue = NULL; 7 LPCTSTR const lpValueName = _T("TcpPort"); 8 9 LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, 10 _T("SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp"), 11 0, 12 KEY_QUERY_VALUE, 13 &hKey); 14 if(ERROR_SUCCESS != lRet) 15 { 16 // Error handling (see this FAQ) 17 // return; 18 } 19 // Call once RegQueryValueEx to retrieve the necessary buffer size 20 ::RegQueryValueEx(hKey, 21 lpValueName, 22 0, 23 &dwDataType, 24 lpValue, // NULL 25 &dwSize); // will contain the data size 26 27 // Alloc the buffer 28 lpValue = (LPBYTE)malloc(dwSize); 29 30 // Call twice RegQueryValueEx to get the value 31 lRet = ::RegQueryValueEx(hKey, 32 lpValueName, 33 0, 34 &dwDataType, 35 lpValue, 36 &dwSize); 37 ::RegCloseKey(hKey); 38 if(ERROR_SUCCESS != lRet) 39 { 40 // Error handling 41 // return; 42 } 43 // Enjoy of lpValue... 44 cout << "port ----------------------- " << lpValue << endl; 45 46 // free the buffer when no more necessary 47 free(lpValue); 48 49 // 此段代码来源:http://forums.codeguru.com/showthread.php?247020-Windows-SDK-Registry-How-can-I-read-data-from-the-registry&s=
-
读取REG_DWORD 类型的注册表键值
1 // 读取 REG_DWORD 类型的注册表键值代码 2 3 long lRet; 4 HKEY hKey; 5 DWORD port; 6 DWORD dwType = REG_DWORD; 7 DWORD dwValue; 8 lRet = RegOpenKeyEx( 9 HKEY_LOCAL_MACHINE, 10 "SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\Tcp", 11 0, 12 KEY_QUERY_VALUE, 13 &hKey 14 ); //打开注册表 15 if(lRet == ERROR_SUCCESS)//读操作成功 16 { 17 lRet = RegQueryValueEx( 18 hKey, 19 "DefaultPort", 20 0, 21 &dwType, 22 (LPBYTE)&port, 23 &dwValue 24 ); //如果打开成功,则读 25 if(lRet == ERROR_SUCCESS) 26 { 27 printf(" 打开成功,则读:"); 28 cout << "DefaultPort: " << port << endl; 29 //mcfile << "DefaultPort: " << port << endl; 30 mcfile << "<port>" << port << "</port>" << endl; 31 } 32 //以下是读取失败 33 else 34 { 35 printf(" 读取失败:"); 36 } 37 38 } 39 //以下是打开失败 40 else 41 { 42 printf(" 打开失败:"); 43 } 44 RegCloseKey(hKey);//记住,一定要关闭
-
读取REG_MULTI_SZ 类型的注册表键值
1 #include "windows.h" 2 #include "tchar.h" 3 #include "conio.h" 4 #include "stdio.h" 5 6 #define MY_KEY _T("PathToMyRegistryKey\MyRegistryKey") // Registry key 7 #define MY_VALUES _T("NameOfTheREG_MULTI_SZListOfValues") // Registry values 8 #define NEW_VALUE _T("MyNewValue") // New value 9 #define FIND_VALUE _T("AnExistingValue") // We will insert the new value after this one 10 11 int _tmain(int argc, _TCHAR* argv[]) 12 { 13 LONG lResult = 0; 14 HKEY hKey = NULL; 15 LPTSTR lpValues = NULL; 16 LPTSTR lpValue = NULL; 17 LPTSTR lpNewValues = NULL; 18 LPTSTR lpNewValue = NULL; 19 DWORD cbValues = 0; 20 DWORD cbNewValues = 0; 21 DWORD cbNewValue = 0; 22 BOOL bFound = FALSE; 23 24 __try 25 { 26 // OPEN THE REGISTRY KEY 27 // 28 _tprintf(_T("RegOpenKeyEx...")); 29 lResult = RegOpenKeyEx( 30 HKEY_LOCAL_MACHINE, 31 MY_KEY, 32 0, 33 KEY_ALL_ACCESS, 34 &hKey 35 ); 36 if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x "), lResult); return 1; } 37 _tprintf(_T("SUCCESS ")); 38 39 // READ THE REG_MULTI_SZ VALUES 40 // 41 // Get size of the buffer for the values 42 _tprintf(_T("RegQueryValueEx...")); 43 lResult = RegQueryValueEx( 44 hKey, 45 MY_VALUES, 46 NULL, 47 NULL, 48 NULL, 49 &cbValues 50 ); 51 if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x "), lResult); return 1; } 52 _tprintf(_T("SUCCESS ")); 53 54 // Allocate the buffer 55 _tprintf(_T("malloc...")); 56 lpValues = (LPTSTR)malloc(cbValues); 57 if (NULL == lpValues) { _tprintf(_T("ERROR 0x%x "), GetLastError()); return 1; } 58 _tprintf(_T("SUCCESS ")); 59 60 // Get the values 61 _tprintf(_T("RegQueryValueEx...")); 62 lResult = RegQueryValueEx( 63 hKey, 64 MY_VALUES, 65 NULL, 66 NULL, 67 (LPBYTE)lpValues, 68 &cbValues 69 ); 70 if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x "), lResult); return 1; } 71 _tprintf(_T("SUCCESS ")); 72 73 // SHOW THE VALUES 74 // 75 _tprintf(_T(" ************************** ")); 76 _tprintf(_T("OLD VALUES ")); 77 _tprintf(_T("************************** ")); 78 lpValue = lpValues; 79 for (; '