[英]VS c++ Deleting subkey doesn't work! Error code 0
我目前正在尝试从“ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run”中删除一个子项。 问题是尽管尝试了所有可能的解决方案,但我的错误代码却是唯一的,并且唯一的问题尚未得到解决: 如何使用c ++从启动列表中删除程序 。 我在Windows x64 10上使用Visual Studio,该程序是Win32应用程序。
创建的密钥包括:
BOOL registerForLocalStartup(PCWSTR regName, PCWSTR pathToExe, PCWSTR args)
{
HKEY hKey = NULL;
LONG lResult = 0;
BOOL fSuccess = TRUE;
DWORD dwSize;
const size_t count = MAX_PATH * 2;
wchar_t szValue[count] = {};
wcscpy_s(szValue, count, L"\"");
wcscat_s(szValue, count, pathToExe);
wcscat_s(szValue, count, L"\" ");
if (args != NULL)
{
// caller should make sure "args" is quoted if any single argument has a space
// e.g. (L"-name \"Mark Voidale\"");
wcscat_s(szValue, count, args);
}
// For admin HKEY_LOCAL_MACHINE
lResult = RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, (KEY_WRITE | KEY_READ), NULL, &hKey, NULL);
fSuccess = (lResult == 0);
if (fSuccess)
{
dwSize = (wcslen(szValue) + 1) * 2;
lResult = RegSetValueExW(hKey, regName, 0, REG_SZ, (BYTE*)szValue, dwSize);
fSuccess = (lResult == 0);
}
if (hKey != NULL)
{
RegCloseKey(hKey);
hKey = NULL;
}
return fSuccess;
}
这是我的代码:
bool DeleteValueKey(HKEY hKeyRoot, std::wstring Subkey, std::wstring ValueKey)
{
HKEY hKey = NULL;
bool bReturn = false;
long result = RegOpenKeyEx(hKeyRoot, Subkey.c_str(), 0, KEY_READ | KEY_WRITE | KEY_WOW64_32KEY, &hKey);
wcout << "Result of RegOpenKeyEx: " << result << endl;
if (result == ERROR_SUCCESS)
{
long result2 = RegDeleteKeyEx(hKey, ValueKey.c_str(), KEY_WOW64_32KEY, 0);
wcout << "Result of RegDeleteKeyEx: " << result2 << endl;
if (result2 == ERROR_SUCCESS)
{
bReturn = true;
}
}
if (hKey != NULL) { RegCloseKey(hKey); }
return bReturn;
}
这就是我试图称呼的:
bool result = DeleteValueKey(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", L"test1");
if (result)
{
wcout << "SUCCESS" << endl;
}
else
{
wcout << "FAILURE: "<< GetLastError() << endl;
}*/
输出:
Result of RegOpenKeyEx: 0
Result of RegDeleteKeyEx: 2
FAILURE: 0
有一个主意吗? 我快要疯了,不敢解决这样一个公然的问题...
要删除Run 键你的价值 ,你应该使用RegDeleteKeyValue
(或RegDeleteValue
如果你支持的WinXP及以上)。
RegDeleteKeyEx
用于删除整个键(及其所有值),由于您不拥有Run键,因此您不想在此处这样做。
请参阅此博客文章 ,以获取用于描述注册表各个部分的术语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.