gpt4 book ai didi

c - 为什么我无法打开我的注册表项

转载 作者:行者123 更新时间:2023-11-30 20:32:34 26 4
gpt4 key购买 nike

第一次问问题,希望我做得对......

我正在尝试编写一个小程序来更改 Internet Explorer 主页的注册表 key ,但每次运行它时,我都会收到 key 位置打开不成功的错误。有什么想法吗? (尝试在管理员中运行)

//this string array will be the value for the new home page (w/ null termination)
char newHomePage[] = "https://www.youtube.com/watch?v=gwJ_LgYYvpU \0";
HKEY homePageKey = NULL; //handle for the key once opened


//Open reg key we wish to change, if this fails then abort

//reg key for home page
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"\\SOFTWARE\\Microsoft\\Internet Explorer\\Main", 0, KEY_SET_VALUE, &homePageKey) == ERROR_SUCCESS)
{
printf("Key location open successful \n");
if (RegSetValueExW(homePageKey, L"Start Page", 0, REG_SZ, (LPBYTE)&newHomePage, sizeof(char)) == ERROR_SUCCESS)
{
printf("Key changed in registry \n");
}
else
{
printf("Key not changed in registry \n");
printf("Error %u ", (unsigned int)GetLastError());
}
RegCloseKey(homePageKey);
}
else
{
printf("Error: %u \n", (unsigned int)GetLastError());
printf("Key location open UNsuccessful \n");
system("pause");
RegCloseKey(homePageKey);
return 0;
}

return 0;

最佳答案

您的代码存在几个问题:

  1. 在为 RegCreateKeyEx()RegOpenKeyEx() 指定子项时,请勿包含前导斜杠。

  2. 您将 URL 作为 ANSI 字符串传递给需要 Unicode 字符串的函数。并且您指定了错误的数据大小。

  3. 您从 GetLastError() 获取错误代码,但注册表 API 不使用 SetLastError() 报告错误。错误代码改为在函数返回值中返回。

试试这个:

//this string array will be the value for the new home page (w/ null termination)
const wchar_t newHomePage[] = L"https://www.youtube.com/watch?v=gwJ_LgYYvpU\0";
HKEY homePageKey = NULL; //handle for the key once opened

//Open reg key we wish to change, if this fails then abort
//reg key for home page
LONG lResult = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Internet Explorer\\Main", 0, KEY_SET_VALUE, &homePageKey);
if (lResult == ERROR_SUCCESS)
{
printf("Key location open successful \n");
lResult = RegSetValueExW(homePageKey, L"Start Page", 0, REG_SZ, (LPBYTE)newHomePage, sizeof(newHomePage)); // or (lstrlenW(newHomePage)+1)*sizeof(wchar_t)
if (lResult == ERROR_SUCCESS)
{
printf("Key changed in registry \n");
}
else
{
printf("Key not changed in registry \n");
printf("Error %ld \n", lResult);
}
RegCloseKey(homePageKey);
}
else
{
printf("Key location open UNsuccessful \n");
printf("Error: %ld \n", lResult);
}
system("pause");
return 0;

关于c - 为什么我无法打开我的注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47288309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com