试图在注册表中创建一个值 - C++ - RegSetValueEx [英] trying to create a value in registry - C++ - RegSetValueEx

查看:155
本文介绍了试图在注册表中创建一个值 - C++ - RegSetValueEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 C++ 中向注册表添加一个值,但程序显示错误.我有一个非常简单的代码,当程序运行时出现错误:无法设置注册表值 value_name(RegSetValueEx 失败)

I tried to add a value to registry in c++, but the program shows error. I have a very simple code, when the program run I got the error: Unable to set registry value value_name (RegSetValueEx failed)

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <Windows.h>

using namespace std;


int main() {


    HKEY key;
    if (RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion"), &key) != ERROR_SUCCESS)
    {
        cout << "unable to open registry";
    }



    if (RegSetValueEx(key, TEXT("value_name"), 0, REG_SZ, (LPBYTE)"value_data", strlen("value_data")*sizeof(char)) != ERROR_SUCCESS)
    {
        RegCloseKey(key);
        cout << "Unable to set registry value value_name";
    }
    else
    {
        cout << "value_name was set" << endl;
    }

}

推荐答案

您需要以管理员身份运行或提升权限才能对 HKLM 进行更改.尝试(您可以以管理员身份运行 Visual Studio 以通过 F5 进行调试)或改为在 HKEY_CURRENT_USER 下工作.

You need to Run as Administrator, or elevated, to make changes to HKLM. Try either that (you can run Visual Studio as admin to debug via F5) or work under HKEY_CURRENT_USER instead.

这篇关于试图在注册表中创建一个值 - C++ - RegSetValueEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆