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

发布于 2024-11-30 08:05:41 字数 1367 浏览 0 评论 0原文

我正在尝试将一些二进制数据从 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 技术交流群。

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

发布评论

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

评论(2

羅雙樹 2024-12-07 08:05:41

首先,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 by Read().

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.

寻找我们的幸福 2024-12-07 08:05:41

我遇到了同样的问题 - 问题是

位图->保存
写入字节并将偏移量移动到流的末尾,当您尝试获取字节时,它开始从流的末尾读取,因此会读取 0 个字节。因此,您需要从流开头开始读取偏移量:

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

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. So you need point read offset from begin of the stream:

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