第一次提问,希望我做对了...
我正在尝试编写一个小程序来更改 Internet Explorer 主页的 reg 密钥,但是每次运行它时,我都会收到密钥位置打开失败的错误。任何想法为什么?(尝试在管理员中运行)
//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;
您的代码有几个问题:
将子项指定为RegCreateKeyEx()
或时,请勿包含前导斜杠RegOpenKeyEx()
。
您将 URL 作为 ANSI 字符串传递给需要 Unicode 字符串的函数。并且您指定了错误的数据大小。
您从 获取错误代码GetLastError()
,但 Registry 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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句