读写注册表

读:
C/C++ code
#include
< afx.h >
#include
< windows.h >
#include
< iostream >
#include
< string >
#include
< fstream >
using namespace std;

int main()
{
    HKEY hKEY;
    LPCTSTR data_Set
= " SOFTWARE//Microsoft//WZCSVC//Parameters//Interfaces//{2DA2A52D-F802-4DE6-B192-4B99AF59CA2A}// " ;
   
long ret = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0 ,KEY_READ, & hKEY); // 打开表项
    if (ret != ERROR_SUCCESS)
    {
        cerr
<< " 错误:无法打开有关的hKEY " << endl;
        exit(
1 );
    }

    LPBYTE result
= new BYTE[ 128 ];
    DWORD type
= REG_BINARY; // 二进制类型
    DWORD len = 128 ; // 定义数据长度

    ret
= ::RegQueryValueEx(hKEY, " ActiveSettings " ,NULL, & type,result, & len); // 查询项值
    if (ret != ERROR_SUCCESS)
    {
        cerr
<< " 错误:无法查询有关的注册表信息 " << endl;
        exit(
1 );
    }

    ofstream
out ( " reg " , fstream:: out | fstream::binary); // 保存项值的文件
    if ( ! out )
    {
        cerr
<< " reg.txt error " << endl;
        exit(
1 );
    }

   
out << result; // 写出项值

    delete [] result;
// 释放
    ::RegCloseKey(hKEY); // 关闭表项

   
return 0 ;
}

 

写:

C/C++ code
#include
< afx.h >
#include
< windows.h >
#include
< iostream >
#include
< string >
#include
< fstream >
using namespace std;

int main()
{
    HKEY hKEY;
    LPCTSTR data_Set
= " SOFTWARE//Microsoft//WZCSVC//Parameters//Interfaces//{2DA2A52D-F802-4DE6-B192-4B99AF59CA2A}// " ;

    FILE
* fp = fopen( " reg " , " rb " ); // 打开文件
    if ( ! fp)
    {
        cerr
<< " reg.txt error " << endl;
        exit(
1 );
    }
    DWORD start
= ftell(fp);
    fseek(fp,
0 , SEEK_END);
    DWORD len
= ftell(fp) - start; // 获得文件大小
    fseek(fp, start, SEEK_SET);

    cout
<< len << endl;
    LPBYTE data
= new BYTE[len];
    fread(data, len,
1 , fp);

    DWORD type
= REG_BINARY; // 二进制类型
    LONG ret = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0 ,KEY_WRITE, & hKEY); // 打开表项
    if (ret != ERROR_SUCCESS) // 如果无法打开hKEY,则中止程序的执行
    {
        cerr
<< " 错误:无法打开有关的hKEY " << endl;
        exit(
1 );
    }

    ret
= ::RegSetValueEx(hKEY, " ActiveSettings " ,NULL,type,data,len); // 写项值
    if (ret != ERROR_SUCCESS)
    {
        cerr
<< " 错误:无法设置有关的注册表信息 " << endl;
        exit(
1 );
    }

    delete [] data;
    ::RegCloseKey(hKEY);
   
   
return 0 ;
}

 

 

 

你可能感兴趣的:(File,null,delete,byte,FP)