如何读取 C++ 中的 REG_NONE 值?

发布于 2025-01-09 08:53:21 字数 1094 浏览 0 评论 0原文

我想用 C++ 从 regedit 读取 REG_NONE 值。

这是我的代码:

#include <iostream>
#include <windows.h>
using namespace std;

//--- Değişkenler ---//
//DWORD
DWORD dw_Rn_Boyut = MAX_PATH;
DWORD dw_Rn_Deger;
DWORD dw_Rn_DegerTipi = REG_NONE;

//HKEY
HKEY hkey_Rn;

//LONG
LONG long_Rn_Sonuc;

int main()
{
    long_Rn_Sonuc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_READ | KEY_WOW64_64KEY, &hkey_Rn);

    if(long_Rn_Sonuc == ERROR_SUCCESS)
    {
        long_Rn_Sonuc = RegQueryValueEx(hkey_Rn, "pathW", 0, &dw_Rn_DegerTipi, (LPBYTE)&dw_Rn_Deger, &dw_Rn_Boyut);

        if(long_Rn_Sonuc == ERROR_SUCCESS)
        {
            cout << dw_Rn_Deger;
        }
    }

    getchar();

    return 0;
}

我的应用程序显示 3801156 结果。该值是该寄存器值的十进制版本。它等于 3A 00 44

这是我想读取的 reg 值:

在此处输入图像描述

但是为什么我的应用程序不读取其他十六进制值?

我该如何解决我的问题?

I want to read REG_NONE value from regedit with C++.

Here are my codes:

#include <iostream>
#include <windows.h>
using namespace std;

//--- Değişkenler ---//
//DWORD
DWORD dw_Rn_Boyut = MAX_PATH;
DWORD dw_Rn_Deger;
DWORD dw_Rn_DegerTipi = REG_NONE;

//HKEY
HKEY hkey_Rn;

//LONG
LONG long_Rn_Sonuc;

int main()
{
    long_Rn_Sonuc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_READ | KEY_WOW64_64KEY, &hkey_Rn);

    if(long_Rn_Sonuc == ERROR_SUCCESS)
    {
        long_Rn_Sonuc = RegQueryValueEx(hkey_Rn, "pathW", 0, &dw_Rn_DegerTipi, (LPBYTE)&dw_Rn_Deger, &dw_Rn_Boyut);

        if(long_Rn_Sonuc == ERROR_SUCCESS)
        {
            cout << dw_Rn_Deger;
        }
    }

    getchar();

    return 0;
}

My app shows 3801156 as result. This value is decimal version of that reg value. It equals to 3A 00 44

Here is the reg value which I want to read:

enter image description here

But why does not my app read other hex values?

How can I fix my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-16 08:53:21

不存在 REG_NONE 值这样的东西。成功后,您的 dw_Rn_DegerTipi 变量将使用实际值类型(在本例中为 REG_BINARY)进行更新,并且您的 dw_Rn_Boyut< /code> 变量将根据读取的实际字节数进行更新。

您的问题是您为 dw_Rn_Dege 变量使用了错误的数据类型。相关数据不是一个 DWORD 值,而是一个 WCHAR 字符串,因此您需要一个 WCHAR 字符缓冲区来接收它。您告诉 RegQueryValueEx() 您分配了一个缓冲区来保存 MAX_PATH 字节,但实际上没有。因此,当 RegQueryValueEx() 尝试向 dw_Rn_Deger 的内存地址写入超过 4 个字节时,您的代码中会出现缓冲区溢出错误。

试试这个:

#include <iostream>
#include <windows.h>
using namespace std;

//--- Değişkenler ---//
WCHAR sz_Rn_Deger[MAX_PATH+1] = {};
DWORD dw_Rn_Boyut = MAX_PATH * sizeof(WCHAR);

//HKEY
HKEY hkey_Rn;

//LONG
LONG long_Rn_Sonuc;

int main()
{
    long_Rn_Sonuc = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hkey_Rn);

    if (long_Rn_Sonuc == ERROR_SUCCESS)
    {
        long_Rn_Sonuc = RegQueryValueExW(hkey_Rn, L"pathW", NULL, NULL, reinterpret_cast<LPBYTE>(&sz_Rn_Deger), &dw_Rn_Boyut);

        if (long_Rn_Sonuc == ERROR_SUCCESS)
        {
            wcout << sz_Rn_Deger;
        }

        RegCloseKey(hkey_Rn);
    }

    getchar();

    return 0;
}

There is no such thing as a REG_NONE value. On success, your dw_Rn_DegerTipi variable will be updated with the actual value type (in this case, REG_BINARY), and your dw_Rn_Boyut variable will be updated with the number of actual bytes read.

Your problem is that you are using the wrong data type for your dw_Rn_Dege variable. The data in question is not a DWORD value, it is a WCHAR character string, so you need a WCHAR character buffer to receive it. You are telling RegQueryValueEx() that you allocated a buffer to hold MAX_PATH bytes, but you really didn't. So you have a buffer overflow error in your code when RegQueryValueEx() tries to write more than 4 bytes to the memory address of dw_Rn_Deger.

Try this instead:

#include <iostream>
#include <windows.h>
using namespace std;

//--- Değişkenler ---//
WCHAR sz_Rn_Deger[MAX_PATH+1] = {};
DWORD dw_Rn_Boyut = MAX_PATH * sizeof(WCHAR);

//HKEY
HKEY hkey_Rn;

//LONG
LONG long_Rn_Sonuc;

int main()
{
    long_Rn_Sonuc = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\DownloadManager\\FoldersTree\\Compressed", 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &hkey_Rn);

    if (long_Rn_Sonuc == ERROR_SUCCESS)
    {
        long_Rn_Sonuc = RegQueryValueExW(hkey_Rn, L"pathW", NULL, NULL, reinterpret_cast<LPBYTE>(&sz_Rn_Deger), &dw_Rn_Boyut);

        if (long_Rn_Sonuc == ERROR_SUCCESS)
        {
            wcout << sz_Rn_Deger;
        }

        RegCloseKey(hkey_Rn);
    }

    getchar();

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文