简体   繁体   English

如何将 IStream 实例中的数据读入 char 指针?

[英]How do I read the data from an IStream instance into a char pointer?

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.我试图将一些二进制数据从我的 IStream 实例(因为 Gdiplus::Image 只保存到 IStream 派生对象或文件路径)复制到一个字符指针,我可以通过知道分配的二进制大小来简单地从中读取并拥有访问指针。

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:在我处理 Upload 实例的函数中,我这样做:

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.提前致谢。

PS: The test Image-instance successfully saves to the file after reading from m_hBufferStream. PS:测试Image-instance从m_hBufferStream读取后成功保存到文件中。

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. 首先,不需要IStream::Read()来精确地读取指定的字节数-只需读取不超过该数目的字节即可。 Actual number is stored inside the veriable pointed to by the third parameter. 实际数字存储在第三个参数所指向的验证中。

Second, you don't check the HRESULT returned by Read() . 其次,您无需检查Read()返回的HRESULT

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. 更好的策略是在循环中调用Read() ,检查其返回值,并根据实际读取的字节数来调整指向缓冲区的指针。

i faced the same problem - matter is我遇到了同样的问题 - 问题是

bitmap->Save write bytes and moves offset to the end of the stream, and when you try to get bytes it starts to read from the end of stream and thus would read 0 bytes.位图-> 保存写入字节并将偏移量移动到流的末尾,当您尝试获取字节时,它会从流的末尾开始读取,因此将读取 0 个字节。 So you need point read offset from begin of the stream:所以你需要从流的开始点读取偏移量:

LARGE_INTEGER   li = {0};
m_hBufferStream->Seek(li, STREAM_SEEK_SET, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM