如何将 IStream 实例中的数据读入 char 指针?
我正在尝试将一些二进制数据从 IStream 实例(因为 Gdiplus::Image 只保存到 IStream 派生对象或文件路径)复制到 char 指针,我可以通过知道分配的二进制大小来简单地读取该指针并具有访问指针。
我的类如下:
Upload::Upload(Gdiplus::Bitmap* bitmap, CLSID clsEncoderId)
{
int result;
STATSTG statResult;
result = CreateStreamOnHGlobal(0, TRUE, &m_hBufferStream);
if (result != S_OK)
MessageBoxW(NULL, _T("Upload::Upload(): Could not create stream"), _T("Point"), MB_OK | MB_ICONERROR);
else
{
if (bitmap->Save(m_hBufferStream, &clsEncoderId, NULL) != Gdiplus::Ok)
MessageBoxW(NULL, _T("Upload::Upload(): Could not save() image"), _T("Point"), MB_OK | MB_ICONERROR);
}
if (m_hBufferStream->Stat(&statResult, STATFLAG_NONAME) != S_OK)
return;
Gdiplus::Image test(m_hBufferStream, TRUE);
test.Save(_T("hejs.png"), &clsEncoderId, NULL);
m_iSize = statResult.cbSize.LowPart;
}
char* Upload::GetBinaryData()
{
char* buffer = (char*)malloc(m_iSize);
ULONG size = 0;
m_hBufferStream->Read(buffer, m_iSize, &size);
return buffer;
}
在处理 Upload 实例的函数中,我这样做:
char* pBuffer = upload->GetBinaryData();
buffer.write(pBuffer, upload->GetSize());
但是存储的内存是错误的(奇怪的是,它看起来像是一种模式)。
我做错了什么?
提前致谢。
附: 测试图像实例从 m_hBufferStream 读取后成功保存到文件。
I'm trying to copy some binary data from my IStream instance (since Gdiplus::Image only saves to IStream-deriving objects, or a file path) to a char pointer from which I can read simply by knowing the allocated binary size and have access to the pointer.
My class is as follows:
Upload::Upload(Gdiplus::Bitmap* bitmap, CLSID clsEncoderId)
{
int result;
STATSTG statResult;
result = CreateStreamOnHGlobal(0, TRUE, &m_hBufferStream);
if (result != S_OK)
MessageBoxW(NULL, _T("Upload::Upload(): Could not create stream"), _T("Point"), MB_OK | MB_ICONERROR);
else
{
if (bitmap->Save(m_hBufferStream, &clsEncoderId, NULL) != Gdiplus::Ok)
MessageBoxW(NULL, _T("Upload::Upload(): Could not save() image"), _T("Point"), MB_OK | MB_ICONERROR);
}
if (m_hBufferStream->Stat(&statResult, STATFLAG_NONAME) != S_OK)
return;
Gdiplus::Image test(m_hBufferStream, TRUE);
test.Save(_T("hejs.png"), &clsEncoderId, NULL);
m_iSize = statResult.cbSize.LowPart;
}
char* Upload::GetBinaryData()
{
char* buffer = (char*)malloc(m_iSize);
ULONG size = 0;
m_hBufferStream->Read(buffer, m_iSize, &size);
return buffer;
}
In my function that processes the Upload instance I do this:
char* pBuffer = upload->GetBinaryData();
buffer.write(pBuffer, upload->GetSize());
But the memory stored is wrong (oddly it seems like a pattern though).
What am I doing wrong?
Thanks in advance.
P.S.:
The test Image-instance successfully saves to the file after reading from m_hBufferStream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
IStream::Read()
不需要精确读取指定数量的字节 - 它需要读取不超过该数量的字节。实际数字存储在第三个参数指向的变量内。其次,您不检查
Read()
返回的HRESULT
。更好的策略是在循环中调用 Read(),检查其返回值并根据实际读取的字节数调整指向缓冲区的指针。
First of all,
IStream::Read()
is not required to read exactly the specified number of bytes - it is required to read no more than that number. Actual number is stored inside the veriable pointed to by the third parameter.Second, you don't check the
HRESULT
returned byRead()
.A much better strategy would be to call
Read()
in a loop, check its return value and adjust the pointer to the buffer according to how many bytes have been actually read.我遇到了同样的问题 - 问题是
i faced the same problem - matter is