我很确定我的RegSetSetValueExA工作正常,我正在编写的数据是(CONST BYTE*)&setValue。我的setvalue是一个DWORD,我已经用RegOpenKeyExA写了这个给注册表,它工作得很好。我认为问题来自于RegCreateKeyExA,因为我正在用它创建我的新密钥。
而且,由于某种原因,我的REG_DWORD要求我用二进制语言编写。
https://gyazo.com/e418587d579a3e540656f06a2524901f
我尝试过查看其他线程,但是每个人的问题似乎都不同,因为它们使用的是RegOpenKeyExA。
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <cstdio>
#include "Strsafe.h"
// Stolen microsoft error code credits:msdn
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
// end of stolen code
int main()
{
DWORD Disposition = REG_CREATED_NEW_KEY;
BYTE lpData[32];
DWORD setValue = 2;
PHKEY throwAwayKey = 0;
DWORD lpType = { REG_DWORD };
DWORD lpcbData = { sizeof(lpData) };
HKEY hKey = 0;
char regPath[64] = "Software\\Policies\\Microsoft\\Windows\\System";
char lpValueName[32] = "DisableCMD";
long RegCKExA = RegCreateKeyExA(HKEY_CURRENT_USER, regPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &Disposition);
if (RegCKExA == ERROR_SUCCESS)
{
std::cout << "Successfully executed RegCreatKeyExA\n";
}
else
{
std::cout << "An error has occurred while executing RRegCreateKeyExA. Error code: ";
ErrorExit((LPTSTR)TEXT("RegCreateKeyExA"));
getchar();
return EXIT_FAILURE;
}
long regQVExA = RegQueryValueExA(hKey, lpValueName, NULL, &lpType, (LPBYTE)lpData, &lpcbData);
if (regQVExA == ERROR_SUCCESS)
{
std::cout << "Successfully executed RegQueryValueExA and DisableCMD is already on this computer. Press ENTER to continute\n";
getchar();
return ERROR_SUCCESS; // Difference is it returns here if DisableCMD exists
}
else
std::cout << "DisableCMD not found. Starting creation of DisableCMD registry value. Press ENTER to continue";
getchar();
auto regSVExA = RegSetValueExA(hKey, lpValueName, 0, REG_DWORD, (CONST BYTE*)&setValue, lpcbData);
if (regSVExA == ERROR_SUCCESS)
{
std::cout << "Successfully executed RegSetValueExA\n";
getchar();
}
else
{
std::cout << "An error has occurred while executing RegSetValueExA. Error code: ";
getchar();
return EXIT_FAILURE;
}
RegCloseKey(hKey);
return 0;
}发布于 2019-04-12 05:23:02
我将您的函数重构为WriteDWORD和ReadDWORD。注意,代码实际上是,与代码非常相似的。那我为什么要费心?嗯,有一个微妙的区别,就是我将DWORD设置为输入/输出类型,而不是字节数组。
LSTATUS WriteDWORD(LPCSTR lpPath, LPCSTR lpValueName, DWORD dwData)
{
LSTATUS status = ERROR_SUCCESS;
HKEY hKey = NULL;
DWORD dwDisposition = 0;
status = RegCreateKeyExA(HKEY_CURRENT_USER, lpPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (status != ERROR_SUCCESS)
{
return status;
}
status = RegSetValueExA(hKey, lpValueName, 0, REG_DWORD, (CONST BYTE*) &dwData, sizeof(DWORD));
RegCloseKey(hKey);
return status;
}
LSTATUS ReadDWORD(LPCSTR lpPath, LPCSTR lpValueName, DWORD* pdwData)
{
LSTATUS status = ERROR_SUCCESS;
HKEY hKey = NULL;
DWORD dwDisposition = 0;
status = RegCreateKeyExA(HKEY_CURRENT_USER, lpPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (status != ERROR_SUCCESS)
{
return status;
}
DWORD dwType = 0;
DWORD cbData = sizeof(DWORD);
status = RegQueryValueExA(hKey, lpValueName, NULL, &dwType, (LPBYTE)pdwData, &cbData);
RegCloseKey(hKey);
return status;
}下面是一个示例用法:
int main()
{
char szPath[64] = "Software\\Policies\\Microsoft\\Windows\\System";
char szValueName[32] = "DisableCMD";
WriteDWORD(szPath, szValueName, 1234);
DWORD dwValue = 0;
ReadDWORD(szPath, szValueName, &dwValue); // dwValue now contains 1234
return 0;
}请注意,我做了一些事情:
DWORD dwData做作者,用DWORD* pdwData做读者。DWORD cbData = sizeof(DWORD); (即4)我希望这能让你深入了解你问题中的“二进制”部分。一个DWORD是4个字节。当您将它写入注册表时,您要它存储一个32位数字或4个字节的DWORD。当您从注册表中读取它时,要在应用程序中进行重构,您应该提供一个指向DWORD的指针。由于您给它一个字节数组,32位数字填充了您提供的数组的前4个字节。你可能不明白,但这是正确的行为。
如果使用std::cout,您会发现由于重载的C++类型,它对相同的4个字节的反应不同。如果你用的是DWORD,你就会看到你的号码。但是,由于字节数组中有它,所以它将是二进制乱七八糟的。
https://stackoverflow.com/questions/55644668
复制相似问题