我的将值插入注册表的测试程序不起作用。我还没有在谷歌或这个网站上找到解决方案。以管理员身份运行程序后,立即关闭,寄存器未被修改。
HKEY hkey;
const char PATH[] = "C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe";
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0,
KEY_WRITE,
&hkey);
RegSetValueEx(hkey,
"TestWordPad",
0,
REG_SZ,
(BYTE*)PATH,
strlen(PATH));
RegCloseKey(hkey);
return 0;
在我开始调试后,输出是这样的:
'ConsoleApplication1.exe' (Win32): Loaded 'C:\Users\jakub\Desktop\ConsoleApplication1\Debug\ConsoleApplication1.exe'. Symbols loaded. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-timezone-l1-1-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l2-1-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-localization-l1-2-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-synch-l1-2-0.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-processthreads-l1-1-1.dll'. Cannot find or open the PDB file. 'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l1-2-0.dll'. Cannot find or open the PDB file. The program '[3052] ConsoleApplication1.exe' has exited with code 0 (0x0).
最佳答案
此代码无法始终如一地工作的原因有两个:
如果在 Windows Vista 或更高版本上运行,它需要管理员权限(例如提升的 UAC)才能写入 HKLM。您可以通过启动管理员命令提示符或以管理员身份运行 Visual Studio 来尝试此操作
如果此代码编译为 32 位应用程序但在 64 位系统上运行,它实际上会修改以下键:
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run
如果您考虑到这两点,代码确实有效。我建议检查每个函数的返回码。例如:
LONG lResult=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0,
KEY_WRITE,
&hkey);
如果您在没有管理员权限的情况下运行代码,您会发现这会返回 5 (ERROR_ACCESS_DENIED)。
关于c++ 没有将值插入注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38328189/