生而自由

自由而无用的灵魂

VC读写注册表

仅作记录。

bool WriteRegValue(HKEY rootKey, CString regPath, CString regKey, CString regValue)
{
	HKEY hKey;
	if (ERROR_SUCCESS != RegOpenKeyEx(rootKey, regPath, 0, KEY_WRITE, &hKey))
		return false;

	LPTSTR lpszData = new TCHAR[regValue.GetLength() + 1];
	lstrcpy(lpszData, regValue);

	bool result = true;

	if (ERROR_SUCCESS != RegSetValueEx(hKey, regKey, 0, REG_SZ, (LPBYTE)lpszData, (regValue.GetLength() + 1)*sizeof(TCHAR)))
	{
		result = false;
	}

	delete[] lpszData;

	RegCloseKey(hKey);

	return result;
}

 

CString readRegValue(HKEY rootKey, CString regPath, CString regKey)
{
	CString result;

	HKEY hKey;
	if (ERROR_SUCCESS != RegOpenKeyEx(rootKey, regPath, 0, KEY_READ, &hKey))
		return result;

	TCHAR *pBuffer = new TCHAR[1024];
	DWORD bufferSize = 1024 * sizeof(TCHAR);

	DWORD dwRet = RegQueryValueEx(hKey, regKey, NULL, NULL, (LPBYTE)pBuffer, &bufferSize);
	while (ERROR_MORE_DATA == dwRet)
	{
		delete[] pBuffer;
		pBuffer = new TCHAR[bufferSize/sizeof(TCHAR)];

		dwRet = RegQueryValueEx(hKey, regKey, NULL, NULL, (LPBYTE)pBuffer, &bufferSize);
	}

	result = CString(pBuffer);

	delete[] pBuffer;

	RegCloseKey(hKey);

	return result;
}

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注