RegCreateKeyEx 成功,但没有添加密钥
RegCreateKeyEx succeeding, but no key added
我正尝试在 Windows 上进行 C++ 编程以进行逆向工程,但我一直在尝试获得 Windows 注册表项。函数 RegCreateKey 和 RegSetValueEx 正在返回 ERROR_SUCCESS,但在检查注册表时缺少密钥。
代码如下:
void AddRunKey() {
wchar_t subkey[512];
wchar_t cmd[512];
wcscpy_s(subkey, L"Test");
wcscpy_s(cmd, L"%windir%\system32\cmd.exe");
HKEY runKey;
long res;
res = RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\Microsoft\Windows\CurentVersion\Run", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &runKey, NULL);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
res = RegSetValueEx(runKey, subkey, 0, REG_EXPAND_SZ, (BYTE*)cmd, wcslen(cmd) + 1);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
RegCloseKey(runKey);
}
int _tmain() {
AddRunKey();
}
我在 Visual Studio、发布模式、64 位 Windows 10 - 64 位虚拟机上编译它。 运行代码时不返回错误。
打开Windows注册表编辑器时,在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run下找不到密钥
是什么导致了这种行为?我该如何解决?
编辑(更新了密钥路径):RegCloseKey returns 0
我看到你的代码中有几个错误。
您需要转义文件路径中的 \
个字符。
您在关键路径中拼错了 CurentVersion
。它需要改为 CurrentVersion
。
无论 RegCreateKeyEx()
成功还是失败,您都在无条件地调用 RegSetValueEx()
和 RegCloseKey()
。
您需要在 RegSetValueEx()
的最后一个参数中以 bytes 而非 characters 指定值大小。
试试这个:
void AddRunKey() {
wchar_t subkey[512];
wchar_t cmd[512];
wcscpy_s(subkey, L"Test");
wcscpy_s(cmd, L"%windir%\system32\cmd.exe");
HKEY runKey;
long res = RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, NULL, 0, KEY_SET_VALUE, NULL, &runKey, NULL);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
else
{
res = RegSetValueEx(runKey, subkey, 0, REG_EXPAND_SZ, (BYTE*)cmd, (wcslen(cmd) + 1) * sizeof(cmd[0]));
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
RegCloseKey(runKey);
}
}
int _tmain() {
AddRunKey();
}
我正尝试在 Windows 上进行 C++ 编程以进行逆向工程,但我一直在尝试获得 Windows 注册表项。函数 RegCreateKey 和 RegSetValueEx 正在返回 ERROR_SUCCESS,但在检查注册表时缺少密钥。
代码如下:
void AddRunKey() {
wchar_t subkey[512];
wchar_t cmd[512];
wcscpy_s(subkey, L"Test");
wcscpy_s(cmd, L"%windir%\system32\cmd.exe");
HKEY runKey;
long res;
res = RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\Microsoft\Windows\CurentVersion\Run", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &runKey, NULL);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
res = RegSetValueEx(runKey, subkey, 0, REG_EXPAND_SZ, (BYTE*)cmd, wcslen(cmd) + 1);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
RegCloseKey(runKey);
}
int _tmain() {
AddRunKey();
}
我在 Visual Studio、发布模式、64 位 Windows 10 - 64 位虚拟机上编译它。 运行代码时不返回错误。
打开Windows注册表编辑器时,在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run下找不到密钥
是什么导致了这种行为?我该如何解决?
编辑(更新了密钥路径):RegCloseKey returns 0
我看到你的代码中有几个错误。
您需要转义文件路径中的 \
个字符。
您在关键路径中拼错了 CurentVersion
。它需要改为 CurrentVersion
。
无论 RegCreateKeyEx()
成功还是失败,您都在无条件地调用 RegSetValueEx()
和 RegCloseKey()
。
您需要在 RegSetValueEx()
的最后一个参数中以 bytes 而非 characters 指定值大小。
试试这个:
void AddRunKey() {
wchar_t subkey[512];
wchar_t cmd[512];
wcscpy_s(subkey, L"Test");
wcscpy_s(cmd, L"%windir%\system32\cmd.exe");
HKEY runKey;
long res = RegCreateKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, NULL, 0, KEY_SET_VALUE, NULL, &runKey, NULL);
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
else
{
res = RegSetValueEx(runKey, subkey, 0, REG_EXPAND_SZ, (BYTE*)cmd, (wcslen(cmd) + 1) * sizeof(cmd[0]));
if (res != ERROR_SUCCESS) {
std::cout << "fail\n";
}
RegCloseKey(runKey);
}
}
int _tmain() {
AddRunKey();
}