试图设置注册表项

时间:2016-05-11 19:01:47

标签: c++ windows winapi registry

我正在尝试设置基本的注册表项名称/值对。我以管理员身份运行两个regedit,并以管理员身份运行程序,但密钥未反映在regedit视图中。奇怪的是,我成功地击中了所有合适的条件......

#ifndef UNICODE
#define UNICODE
#endif // UNICODE

#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;


INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
{
    HKEY key;

    if(RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &key) != ERROR_SUCCESS)
    {
        cout<<"Unable to open reigstry key"<<endl;
    }
    else
    {
        cout<<"Registry key opened..."<<endl;
        cout<<key;
    }

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


    return 0;
}

0 个答案:

没有答案
相关问题