C++ Windows功能“ lockResource()”返回资源中的一半数据

发布于 2025-01-23 16:37:12 字数 1110 浏览 3 评论 0原文

我正在尝试从DLL读取嵌入式资源,其中包含一个加密文件。从lockResource()读取它,仅返回一半的数据。

有趣的是,我检查了sizeofresource(),资源的大小应该是它。

因此,我尝试访问该文件而不是嵌入式资源:

 std::ifstream enc("Logs.enc" , std::ios::binary);              // Accessing encrypted file
    std::string ciphertext = std::string((std::istreambuf_iterator<char>(enc)), std::istreambuf_iterator<char>());
    int size = ciphertext.size();   //  Returns the correct size

这有效,我试图找到它们的共同点,并尝试删除std :: ios :: ios :: binary < /code>,它的行为与访问文件作为资源时的行为相似。

这是我尝试以资源访问它的尝试:

 HGLOBAL SHEET_DATA;      // Imagine this has the encrypted file
        if (SHEET_DATA) {
            char* datac = nullptr;
            datac = (char*)LockResource(SHEET_DATA);
             std::string data = datac;
            long size_sheet = SizeofResource(dll, SHEET);  // 

            int real_size = data.size();    // Returns the wrong size
        }

我尝试搜索是否有任何内容,例如lockResource()函数,以二进制模式访问数据,但是我找不到任何结果。

谢谢

I am trying to read an embedded resource from a dll, it contains an encrypted file. Reading it from LockResource() , only returns half the data.

The funny thing is that I checked SizeOfResource() and the size of the resource is what it is supposed to be.

So I tried to access the file without it being an embedded resource :

 std::ifstream enc("Logs.enc" , std::ios::binary);              // Accessing encrypted file
    std::string ciphertext = std::string((std::istreambuf_iterator<char>(enc)), std::istreambuf_iterator<char>());
    int size = ciphertext.size();   //  Returns the correct size

This worked , I tried to find something they have in common and I tried to remove the std::ios::binary and it had similar behavior to when accessing the file as a resource.

Here is my attempt to Access it as a resource :

 HGLOBAL SHEET_DATA;      // Imagine this has the encrypted file
        if (SHEET_DATA) {
            char* datac = nullptr;
            datac = (char*)LockResource(SHEET_DATA);
             std::string data = datac;
            long size_sheet = SizeofResource(dll, SHEET);  // 

            int real_size = data.size();    // Returns the wrong size
        }

I tried to search if there was anything such as a LockResource() function that accessess the data in binary mode , but I couldn't find any results.

Thank you

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

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

发布评论

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

评论(2

玩套路吗 2025-01-30 16:37:12

strlen假设参数为零终止字符串。它计算字符,直到达到零终止为止。

就您而言,似乎资源是二进制的。在这种情况下,它可能包含具有值0的字节,strlen将其视为字符串的末尾。

因此,什么strlen返回是无关紧要的。您可以使用size_sheetsizeofresource返回,以了解datac指向的数据的大小。

更新:
更新的问题不再包含strlen的用法。但是行:

std::string data = datac;

创建一个类似的问题。初始化std :: Stringchar*假定char*指向零终止字符串。因此,如果缓冲区包含零,则结果字符串将仅包含字符,直到第一个零为止。
您可以初始化std :: String避免零终止假设的下列方法:

std::string data(datac, size_sheet); 

将缓冲区的长度授予std :: String :: String将强制强制用完整的缓冲区初始化(忽略零)。

update2:如下@iinspectable所评论,如果数据不是真正的字符串,请在更合适的容器中握住它 - 例如std :: vector&lt; char&gt;。它还具有接受char*和缓冲区长度的构造函数。

strlen is assuming the parameter is a zero terminated string. It counts the chars until it gets to the zero termination.

In your case it seems like the resource is binary. In this case it may contain bytes with the value 0, which strlen treats as the end of the string.

Therefore what strlen returns is irrelevant. You can use size_sheet returned from SizeofResource to know the size of the data pointed by datac.

Update:
The updated question does not contain a usage of strlen anymore. But the line:

std::string data = datac;

Create a similar problem. Initializing an std::string from a char* assumes the char* is pointing to a zero terminated string. So if the buffer contains zeroes the resulting string will contain only the characters till the first zero.
You can initialize the std::string the following way to avoid the assumption of the zero termination:

std::string data(datac, size_sheet); 

Giving the length of the buffer to the ctor of std::string will force initializing with the complete buffer (ignoring the zeroes).

Update2: As @IInspectable commented below, if the data is not really a string, better hold it in a more suitable container - e.g. std::vector<char>. It also has a constructor accepting a char* and the buffer's length.

意中人 2025-01-30 16:37:12

问题是以下行:

 std::string data = datac;

这是从零终端的字符串中构造std :: String。但是Datac不是零件终止的字符串,因为您说的是二进制数据。而是使用string(const char* s,size_t n); ctor Overload:

 std::string data(datac, size_sheet);

The problem is this line:

 std::string data = datac;

This constructs a std::string from a null-terminated string. But datac is not a null-terminated string, as you said it's binary data. Instead, use the string (const char* s, size_t n); ctor overload:

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