gpt4 book ai didi

c++ - WinApi:无法读取注册表

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:53 28 4
gpt4 key购买 nike

我正在尝试使用 winapi 和 c++ 读取注册表。

代码运行,但结果不是注册表的内容在 hexdump 之后只是 0xCD 一遍又一遍地重复。 (所以,好像数据没有被 RegQueryValueEx 修改过,只是 malloc 的结果)我也尝试以管理员身份运行,但没有成功。

这是我使用的代码:

HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\Shell\\Bags\\1\\Desktop", 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
return;

//Read & save
DWORD BufferSize = TOTALBYTES;
DWORD cbData;
DWORD dwRet;

LPBYTE data = (LPBYTE)malloc(BufferSize);
cbData = BufferSize;

DWORD type = REG_BINARY;

dwRet = RegQueryValueEx(hKey, "IconLayouts", NULL, &type, data, &cbData);

while (dwRet == ERROR_MORE_DATA) {

BufferSize += BYTEINCREMENT;
data = (LPBYTE)realloc(data, BufferSize);
cbData = BufferSize;

dwRet = RegQueryValueEx(hKey, "IconLayouts", NULL, &type, data, &cbData);
}

if (dwRet == ERROR_SUCCESS)
{
//Write current registry to a file
std::ofstream currentRegistryFile(DIRECTORY + currentDesktop + ".bin");
if (!currentRegistryFile) {
log(currentDesktop + " file couldn't be opened.");
return;
}
for (int i = 0; i < cbData; i++)
currentRegistryFile << (data)[cbData];
}
else
log("Couldnt read registry");


//Close registry
RegCloseKey(hKey);

最佳答案

您的保存代码是问题所在。它实际上是越界访问数组:

for (int i = 0; i < cbData; i++)
currentRegistryFile << (data)[cbData];

请注意,您正在使用 cbData 的常量值索引 data 而不是循环变量 i。改变那个。

关于c++ - WinApi:无法读取注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491063/

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