注册表读写的一个例子
#include
"
stdafx.h
"
#include " iostream "
#include " string "
#include " Windows.h "
// 注册表项
#ifndef TEST_APP_REG
#define TEST_APP_REG "Software\\MySoft\\Common\\InstallRoot"
#endif
// 注册表中虚拟磁盘映射的盘符键
#ifndef TEST_APP_REG_DRIVE
#define TEST_APP_REG_DRIVE "Driver"
#endif
#ifndef RETURN_OK
#define RETURN_OK 1
#endif
#ifndef RETURN_ERROR
#define RETURN_ERROR 0
#endif
using namespace std;
int GetRegStringValue( const wstring & wstrPath, const wstring & wstrName, wstring & wstrValue)
{
HKEY hkey;
LONG lErrCode;
DWORD dwPathSize = 512 ;
BYTE bPath[ 512 ] = { 0 };
lErrCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE ,
(LPCWSTR)(wstrPath.c_str()),
NULL,
KEY_QUERY_VALUE,
& hkey);
if (lErrCode != ERROR_SUCCESS)
{
return lErrCode;
}
lErrCode = RegQueryValueEx(hkey, (LPCWSTR)(wstrName.c_str()), NULL, NULL,
(LPBYTE)bPath, & dwPathSize);
if (ERROR_SUCCESS == lErrCode)
{
// lint -save -e534
wstrValue.append((wchar_t * ) bPath);
// lint -restore
}
RegCloseKey(hkey);
return lErrCode;
}
int CreateRegForSoftward()
{
LPCWSTR lpSubKey = L " Software\\MySoft\\Common\\InstallRoot " ;
LPCWSTR lpSubKeyValue = L " Driver " ;
CONST BYTE lpData[ 512 ] = { " Y " };
HKEY hMyKey, hMyKey1;
LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE,lpSubKey,NULL,NULL,REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,NULL, & hMyKey,NULL);
if (ERROR_SUCCESS == lRet)
{
lRet = RegSetValueEx(hMyKey,lpSubKeyValue,NULL,REG_SZ,lpData, 4 );
if (lRet == ERROR_SUCCESS)
{
RegCloseKey(hMyKey);
return RETURN_OK;
}
}
RegCloseKey(hMyKey);
return RETURN_ERROR;
}
int main()
{
wstring wstrDrive, wstrPath = L " Y:\WINDDK " ;
CreateRegForSoftward();
if ( 0 != GetRegStringValue(TEXT(TEST_APP_REG), TEXT(TEST_APP_REG_DRIVE), wstrDrive))
{
return S_FALSE;
}
if (wstrPath.empty() || wstrDrive.empty())
{
return S_FALSE;
}
std::cout << _wcsnicmp(wstrPath.c_str(), wstrDrive.c_str(), 1 );
}
#include " iostream "
#include " string "
#include " Windows.h "
// 注册表项
#ifndef TEST_APP_REG
#define TEST_APP_REG "Software\\MySoft\\Common\\InstallRoot"
#endif
// 注册表中虚拟磁盘映射的盘符键
#ifndef TEST_APP_REG_DRIVE
#define TEST_APP_REG_DRIVE "Driver"
#endif
#ifndef RETURN_OK
#define RETURN_OK 1
#endif
#ifndef RETURN_ERROR
#define RETURN_ERROR 0
#endif
using namespace std;
int GetRegStringValue( const wstring & wstrPath, const wstring & wstrName, wstring & wstrValue)
{
HKEY hkey;
LONG lErrCode;
DWORD dwPathSize = 512 ;
BYTE bPath[ 512 ] = { 0 };
lErrCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE ,
(LPCWSTR)(wstrPath.c_str()),
NULL,
KEY_QUERY_VALUE,
& hkey);
if (lErrCode != ERROR_SUCCESS)
{
return lErrCode;
}
lErrCode = RegQueryValueEx(hkey, (LPCWSTR)(wstrName.c_str()), NULL, NULL,
(LPBYTE)bPath, & dwPathSize);
if (ERROR_SUCCESS == lErrCode)
{
// lint -save -e534
wstrValue.append((wchar_t * ) bPath);
// lint -restore
}
RegCloseKey(hkey);
return lErrCode;
}
int CreateRegForSoftward()
{
LPCWSTR lpSubKey = L " Software\\MySoft\\Common\\InstallRoot " ;
LPCWSTR lpSubKeyValue = L " Driver " ;
CONST BYTE lpData[ 512 ] = { " Y " };
HKEY hMyKey, hMyKey1;
LONG lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE,lpSubKey,NULL,NULL,REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,NULL, & hMyKey,NULL);
if (ERROR_SUCCESS == lRet)
{
lRet = RegSetValueEx(hMyKey,lpSubKeyValue,NULL,REG_SZ,lpData, 4 );
if (lRet == ERROR_SUCCESS)
{
RegCloseKey(hMyKey);
return RETURN_OK;
}
}
RegCloseKey(hMyKey);
return RETURN_ERROR;
}
int main()
{
wstring wstrDrive, wstrPath = L " Y:\WINDDK " ;
CreateRegForSoftward();
if ( 0 != GetRegStringValue(TEXT(TEST_APP_REG), TEXT(TEST_APP_REG_DRIVE), wstrDrive))
{
return S_FALSE;
}
if (wstrPath.empty() || wstrDrive.empty())
{
return S_FALSE;
}
std::cout << _wcsnicmp(wstrPath.c_str(), wstrDrive.c_str(), 1 );
}