从64应用程序中删除32个注册表项

时间:2015-05-20 12:24:18

标签: c++ c windows winapi registry

我正在尝试编写通用API来处理Windows注册表。 当然,首先出现的问题是所有交叉32-64超额加上UAC在赢7后所带来的问题..

在我的API中,我可以创建新的子键,从64个应用程序到32个注册表项,但是当我尝试删除它们时, 我遇到了几个问题。

例如,我的"创建或返回一个子键(如果存在)"功能看起来像这样:

Registry::SubKey Registry::RegistryKey::subKey(const std::wstring& subkey){
    HKEY registryKey;
    auto result = RegCreateKeyEx(m_RegistryKey, subkey.c_str(), 0, //m_RegistryKey is 100% valid , for example HKEY_LOCAL_MACHINE
        NULL, REG_OPTION_NON_VOLATILE ,KEY_ALL_ACCESS | ((Registry::currentBitMode() == 64) ? KEY_WOW64_64KEY : KEY_WOW64_32KEY), 
            NULL, &registryKey, NULL); //currentBitMode return 64 or 32 , depends on what the developer set beforehand
    return Registry::SubKey(registryKey, false);
}

它完美无缺。我可以在注册表的32个子集或64下创建新的子键。

现在,这是我的删除方法:

bool Registry::RegistryKey::deleteSubKey(const std::wstring& subkey){
    std::wstring name(MAX_PATH, 0);
    HKEY subKeyHandle;

auto buffer = subkey;
//m_RegistryKey - member variable, should be valid. at least it's valid when I try it
auto isFunctionSuccessful = RegOpenKeyEx(m_RegistryKey, buffer.c_str(), 0, KEY_ALL_ACCESS | (Registry::currentBitMode() == 64) ? KEY_WOW64_64KEY : KEY_WOW64_32KEY, &subKeyHandle);

if (isFunctionSuccessful != ERROR_SUCCESS){
    std::cout << "error : " << isFunctionSuccessful << "\n";
    return false;
}

if (buffer.back() != L'\\'){
    buffer += L'\\';
}

DWORD dwSize = MAX_PATH;
isFunctionSuccessful = RegEnumKeyEx(subKeyHandle, 0, &name[0], &dwSize, NULL, NULL, NULL, nullptr);

if (isFunctionSuccessful == ERROR_SUCCESS){
    do {
        name = name.substr(0, name.find(wchar_t(0)));
        if (!deleteSubKey(buffer + name)) {
            break;
        }

        dwSize = MAX_PATH;
        name.resize(MAX_PATH);
        isFunctionSuccessful = RegEnumKeyEx(subKeyHandle, 0, &name[0], &dwSize, NULL, NULL, NULL, nullptr);
    } while (isFunctionSuccessful == ERROR_SUCCESS);
}

RegCloseKey(subKeyHandle);

isFunctionSuccessful = RegDeleteKeyEx(m_RegistryKey, subkey.c_str(), (Registry::currentBitMode() == 64) ? KEY_WOW64_64KEY : KEY_WOW64_32KEY, 0);

if (isFunctionSuccessful == ERROR_SUCCESS){
    return true;
}

return false;

}

这是我在这个(令人讨厌的)MSDN示例中的C ++变体: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724235%28v=vs.85%29.aspx

现在,问题出在这里: 例如,我在名为XXX的HKEY_LOCAL_MACHINE / Software下创建一个新的32键, 我可以用我的功能打开它,查看它的值并在regedit下查看它。

如果我尝试删除字符串&#34; Software \ XXX&#34;我收到错误代码2 - 找到文件注释。 如果我尝试删除它inr stirng SOFTWARE \ Wow6432Node \ XXX我得到错误代码5 - 访问被拒绝。 即使以admin身份运行可执行文件,我仍然会看到2错误代码。任何想法?

0 个答案:

没有答案