C++ Windows功能“ lockResource()”返回资源中的一半数据
我正在尝试从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strlen
假设参数为零终止字符串。它计算字符,直到达到零终止为止。就您而言,似乎资源是二进制的。在这种情况下,它可能包含具有值0的字节,
strlen
将其视为字符串的末尾。因此,什么
strlen
返回是无关紧要的。您可以使用size_sheet
从sizeofresource
返回,以了解datac
指向的数据的大小。更新:
更新的问题不再包含
strlen
的用法。但是行:创建一个类似的问题。初始化
std :: String
从char*
假定char*
指向零终止字符串。因此,如果缓冲区包含零,则结果字符串将仅包含字符,直到第一个零为止。您可以初始化
std :: String
避免零终止假设的下列方法:将缓冲区的长度授予
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 usesize_sheet
returned fromSizeofResource
to know the size of the data pointed bydatac
.Update:
The updated question does not contain a usage of
strlen
anymore. But the line:Create a similar problem. Initializing an
std::string
from achar*
assumes thechar*
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: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 achar*
and the buffer's length.问题是以下行:
这是从零终端的字符串中构造
std :: String
。但是Datac
不是零件终止的字符串,因为您说的是二进制数据。而是使用string(const char* s,size_t n);
ctor Overload:The problem is this line:
This constructs a
std::string
from a null-terminated string. Butdatac
is not a null-terminated string, as you said it's binary data. Instead, use thestring (const char* s, size_t n);
ctor overload: