简体   繁体   中英

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."

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++.

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). 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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