7、注册表读写的一个例子

简介: 注册表读写的一个例子 #include "stdafx.h"#include "iostream"#include "string"#include "Windows.h"//注册表项#ifndef TEST_APP_REG#define TEST_APP_REG "Software...

注册表读写的一个例子
 
  
#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 );

}

 

目录
相关文章
C#编程-28:读写注册表
C#编程-28:读写注册表
114 0
|
SQL
用批处理读取特定注册表项的路径值
用批处理命令查询注册表项的命令为 REG QUERY ... 具体的使用信息在命令行窗口中输入 REG QUERY /? 在这里我们需要查询一个具体的key-value,则使用下面的命令 REG QUERY [REG PATH] /v [KEY] 举个例子 ...
1537 0
|
C语言
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 函数形参设置 | 确保打开文件成功 | 统计文件大小 )
133 0
|
安全
注册表读写操作(模块)
注册表读写操作(模块)
788 0
64位读取注册表与32位的区别
有一个读取注册表信息的程序  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0, KEY_READ, &hKey) == ERROR_SUCCESS)/ ,在32位下完全正常,但是在64位返回值正确,但就是读不到东西。
1205 0