简体   繁体   English

在c ++中写入注册表时出错

[英]Error writing to registry in c++

I am trying to make some changes to the registry, and after trying a few other things, I am trying now to import a registry file. 我试图对注册表进行一些更改,并在尝试了一些其他的事情后,我现在尝试导入一个注册表文件。 I was sure that I made it right - until I got the error "Cannot import path\\reg_file.reg: The specified file is not a registry script. You can only import binary registry files from within the registry editor." 我确信我做对了 - 直到我收到错误“无法导入路径\\ reg_file.reg:指定的文件不是注册表脚本。您只能从注册表编辑器中导入二进制注册表文件。”

I have been exporting, editing with Notepad, and re-importing registry files to test - but I don't know how to create them from c++. 我一直在导出,使用记事本编辑,并重新导入注册表文件进行测试 - 但我不知道如何从c ++创建它们。

The contents I placed in the reg file are copied from all the HKEY_CURRENT_USER records related to what I wanted to do (which I exported after I went through the steps of doing what the new entry is supposed to accomplish, manually). 我放在reg文件中的内容是从与我想要做的相关的所有HKEY_CURRENT_USER记录中复制的(我在完成新条目应该完成的操作后,我手动输出了这些记录)。 So they should be in the right place... 所以他们应该在正确的地方......

I used 我用了

input_stream >> reg_entry; //from original file
output_stream << reg_entry; 

to write the file - because it doesn't look binary (and must be processed based on what I read from the registry). 写入文件 - 因为它看起来不是二进制文件(并且必须根据我从注册表中读取的内容进行处理)。

How do I make this work ? 我该如何工作? I can't find a solution, and honestly, the registry scares me. 我找不到解决方案,老实说,注册表吓到了我。

You should not use input and output operators (>> and <<) for binary file read&write. 您不应该使用输入和输出运算符(>>和<<)来进行二进制文件读写。 Use read&write interfaces instead. 请改用读写接口。

ifstream fin("1.reg", ios::in|ios_base::binary);
ofstream fout("2.reg", ios::out|ios_base::binary);

if (fin.is_open() && fout.is_open())
{
    fin.seekg(0, ios::end);
    size_t len = fin.tellg();
    if (0 != len)
    {
        fin.seekg(0, ios::beg);    

        char* buf = new char[len];        
        fin.read(buf, len);        

        // Change the content here

        fout.write(buf, len);

        delete []buf;
    }
}

fin.close();
fout.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM