|
// 读取 REG_SZ 类型键值的代码
HKEY hKey = NULL;
DWORD dwSize = 0;
DWORD dwDataType = 0;
LPBYTE lpValue = NULL;
LPCTSTR const lpValueName = _T("TcpPort");
LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\MSSQLServer\\MSSQLServer\\SuperSocketNetLib\\Tcp"),
0,
KEY_QUERY_VALUE,
&hKey);
if(ERROR_SUCCESS != lRet)
{
// Error handling (see this FAQ)
// return;
}
// Call once RegQueryValueEx to retrieve the necessary buffer size
::RegQueryValueEx(hKey,
lpValueName,
0,
&dwDataType,
lpValue, // NULL
&dwSize); // will contain the data size
// Alloc the buffer
lpValue = (LPBYTE)malloc(dwSize);
// Call twice RegQueryValueEx to get the value
lRet = ::RegQueryValueEx(hKey,
lpValueName,
0,
&dwDataType,
lpValue,
&dwSize);
::RegCloseKey(hKey);
if(ERROR_SUCCESS != lRet)
{
// Error handling
// return;
}
// Enjoy of lpValue...
cout << "port ----------------------- " << lpValue << endl;
// free the buffer when no more necessary
free(lpValue);
// 此段代码来源:[url]http://forums.codeguru.com/showthread.php?247020-Windows-SDK-Registry-How-can-I-read-data-from-the-registry&s=[/url] |
|