首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使运行时没有错误,C++也不会更改注册表。

即使运行时没有错误,C++也不会更改注册表。
EN

Stack Overflow用户
提问于 2021-12-19 21:24:50
回答 2查看 189关注 0票数 0

在我达到这个阶段之后,尝试用c++编写代码来更改注册表项,但是这段代码仍然不编辑注册表,即使在以admin的身份运行时也是如此。

要更改注册表,需要根据我使用的问题来修改4个函数,其中每个函数都返回一个零,这意味着函数完成时没有出现错误,但在注册表gui中仍然没有更改任何值。

SecurityHealth暂停服务正在我的计算机上运行,并具有路径%windir%\system32\SecurityHealthSystray.exe并键入REG_EXPAND_SZ

我甚至尝试创建一个类似于SecurityHealth的新条目,但是仍然没有任何更改。

我编译为admin,运行为admin

代码语言:javascript
运行
复制
HKEY open_reg()
{
    int result;
    LPCSTR lpSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    HKEY hKey; 

    result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_QUERY_VALUE|KEY_WRITE|KEY_READ|KEY_SET_VALUE, &hKey);

    if ( result != 0)
    {
        cout << " Failed to open registry. - [ "<< result<< "]" <<endl;
    }
    else
    {
        cout << "Found registry key. - [" << result<<"]" << endl;
    }
    return hKey;
}




HKEY find_reg_value(HKEY handle)
{
    

    LPCSTR lpValueName = "SecurityHealth";
    DWORD BufferSize = TOTALBYTES;
    DWORD cbData;
    int dwRet;

    PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );
    cbData = BufferSize;

    cout << "\nRetrieving the data..." << endl;

    dwRet = RegQueryValueExA( handle,
                             lpValueName,
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );

    if ( dwRet == 0 ) 
    { 
        cout << "Successfully quered [" << dwRet << "]"<<endl;
    }
    else 
    {
        cout << "Failed to query  Error code : [" << dwRet << "]"<<endl;
    } 

    return handle;
}






void set_reg_value(HKEY handle)
{
    
    int result;
    LPCSTR lpValueName = "SecurityHealth";
    std::string file = "C:\\Windows\\System32\\cmd.exe";
    
    const  char * sth = file.c_str();
    unsigned char m_Test[file.size()];
    strcpy((char*)m_Test, sth);

    DWORD DATA_SIZE = file.size()+1;

    result = RegSetValueExA(handle,lpValueName,0,REG_EXPAND_SZ,m_Test,DATA_SIZE);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value [" << result << "]"<<endl;
    }
    else 
    {
        cout << "Failed to change value  Error code : [" << result << "]"<<endl;
    } 
    RegCloseKey (handle);
}


int main()
{
    cout << "testing windows registry " << endl;
    HKEY reg_handle = open_reg();
    HKEY handler = find_reg_value(reg_handle);
    set_reg_value(handler);
    system("PAUSE");
    return 0;   
}

终端中已编译的exe输出。

代码语言:javascript
运行
复制
testing windows registry
Found registry key. - [0]

Retrieving the data...
Successfully quered [0]
Successfully changed value [0]
Press any key to continue . . .

g++ regutil.cpp编译

EN

Stack Overflow用户

发布于 2021-12-19 23:01:23

它们中的每一个都返回一个零,这意味着函数完成时没有错误,但在注册表GUI中仍然没有更改任何值。

唯一可能发生的方法是:

  1. 在进行更改后,您不会更新GUI。
  2. 您正在修改注册表的另一个区域,然后是查看,即如果您正在修改32位注册表,但正在查看64位注册表,反之亦然。有关使用32位和64位注册表视图的详细信息,请阅读MSDN上的注册表重定向器受WOW64影响的注册表项访问备用注册表视图

尽管如此,您的代码中还有许多其他错误。

尝试更像这样的东西:

代码语言:javascript
运行
复制
HKEY open_reg()
{
    HKEY hKey = NULL;
    int result = RegOpenKeyExA( HKEY_LOCAL_MACHINE,
                             "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                             0,
                             KEY_QUERY_VALUE | KEY_SET_VALUE /* | KEY_WOW64_(32|64)KEY if needed */,
                             &hKey );

    if ( result != 0 )
    {
        cout << " Failed to open Registry, Error " << result << endl;
        return NULL;
    }
    else
    {
        cout << "Opened Registry key" << endl;
        return hKey;
    }
}

void query_reg_value(HKEY handle)
{
    DWORD cbBuffer = TOTALBYTES;
    std::vector<char> buffer(cbBuffer);

    cout << "\nRetrieving the data..." << endl;

    int result = RegQueryValueExA( handle,
                             "SecurityHealth",
                             NULL,
                             NULL,
                             reinterpret_cast<LPBYTE>(buffer.data()),
                             &cbBuffer );

    if ( result == 0 ) 
    { 
        cout << "Successfully quered: ";
        while (cbBuffer != 0 && buffer[cbBuffer-1] == '\0') --cbBuffer; // ignore null terminator(s)
        cout.write(buffer.data(), cbBuffer);
        cout << endl;
    }
    else 
    {
        cout << "Failed to query, Error " << result << endl;
    }
}

void set_reg_value(HKEY handle)
{
    std::string file = "C:\\Windows\\System32\\cmd.exe";

    int result = RegSetValueExA( handle,
                             "SecurityHealth",
                             0,
                             REG_EXPAND_SZ,
                             reinterpret_cast<LPCBYTE>(file.c_str()),
                             file.size()+1);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value" << endl;
    }
    else 
    {
        cout << "Failed to change value, Error " << result << endl;
    }
}

int main()
{
    cout << "testing Windows Registry" << endl;
    HKEY hKey = open_reg();
    if (hKey) {
        query_reg_value(hKey);
        set_reg_value(hKey);
        RegCloseKey(hKey);
    }
    system("PAUSE");
    return 0;   
}

但是,应该注意的是,默认情况下,只有管理员用户可以写访问HKLM密钥,而大多数用户都有只读访问权限。因此,除非你知道自己在做什么,否则在同一时间打开一把钥匙在HKLM下阅读和书写并不是一个好主意。你应该打开一个只读的钥匙,从它读,然后关闭它。写作也一样。例如:

代码语言:javascript
运行
复制
HKEY open_reg(bool isWriting)
{
    HKEY hKey = NULL;
    int result = RegOpenKeyExA( HKEY_LOCAL_MACHINE,
                             "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                             0,
                             (isWriting ? KEY_SET_VALUE : KEY_QUERY_VALUE) /* | KEY_WOW64_(32|64)KEY if needed */,
                             &hKey );

    if ( result != 0 )
    {
        cout << " Failed to open Registry, Error " << result << endl;
        return NULL;
    }
    else
    {
        cout << "Opened registry key" << endl;
        return hKey;
    }
}

void query_reg_value()
{
    HKEY hKey = open_reg(false);
    if (!hKey) return;

    DWORD cbBuffer = TOTALBYTES;
    std::vector<char> buffer(cbBuffer);

    cout << "\nRetrieving the data..." << endl;

    int result = RegQueryValueExA( hKey,
                             "SecurityHealth",
                             NULL,
                             NULL,
                             reinterpret_cast<LPBYTE>(buffer.data()),
                             &cbBuffer );

    if ( result == 0 ) 
    { 
        cout << "Successfully quered: ";
        while (cbBuffer != 0 && buffer[cbBuffer-1] == '\0') --cbData; // ignore null terminator(s)
        cout.write(buffer.data(), cbBuffer);
        cout << endl;
    }
    else 
    {
        cout << "Failed to query, Error " << result << endl;
    }

    RegCloseKey(hKey);
}

void set_reg_value()
{
    HKEY hKey = open_reg(true);
    if (!hKey) return;

    std::string file = "C:\\Windows\\System32\\cmd.exe";

    int result = RegSetValueExA( hKey,
                             "SecurityHealth",
                             0,
                             REG_EXPAND_SZ,
                             reinterpret_cast<LPCBYTE>(file.c_str()),
                             file.size()+1);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value" << endl;
    }
    else 
    {
        cout << "Failed to change value, Error " << result << endl;
    }

    RegCloseKey(hKey);
}

int main()
{
    cout << "testing Windows Registry" << endl;
    query_reg_value();
    set_reg_value();
    system("PAUSE");
    return 0;   
}
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70415450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档