0
我目前正試圖從「Software \ Microsoft \ Windows \ CurrentVersion \ Run」中刪除一個子項。問題是喘口氣嘗試了所有可能的解決方案,我的錯誤代碼是獨一無二的,唯一的問題沒有得到回答:how to remove program from startup list using c++。我在Windows x64上使用Visual Studio,程序是Win32應用程序。VS C++刪除子鍵不起作用!錯誤代碼0
與創建的密鑰:
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;
}*/
OUTPUT:
Result of RegOpenKeyEx: 0
Result of RegDeleteKeyEx: 2
FAILURE: 0
有人有想法嗎?我快瘋了沒有beeing能夠解決這樣的問題,公然...
你爲什麼不檢查註冊表函數的返回值,看看實際的錯誤是什麼 - 你的代碼丟棄這一重要信息。 –
你的程序是32位還是64位?你的Windows 32位或64位?你得到什麼錯誤代碼? ':: GetLastError()'返回什麼? –
我使用Windows x64,我的應用程序是x32。我得到的錯誤代碼是0. – Qubasa