简体   繁体   English

如何将 IStream(C++) 转换为 System::IO::Stream(c#),反之亦然

[英]How to convert IStream(C++) to System::IO::Stream(c#) and vice versa

I have 2 libraries one is c++ and the other one c#.我有 2 个库,一个是 c++,另一个是 c#。

Name of C++ library->LibA C++ 库名称->LibA

Name of C#->LibB C#的名称->LibB

In LibA, 2 main APIs will be there:在 LibA 中,将有 2 个主要 API:

  1. Serialize-> Serialize API will generate IStream as output with the given inputs.序列化-> 序列化 API 将使用给定的输入生成 IStream 作为 output。
  2. Deserialize-> Deserialize API will take IStream as input and deserializes the stream and gets actual data from it.反序列化->反序列化 API 将 IStream 作为输入,反序列化 stream 并从中获取实际数据。
#pragma once
struct GPosition
{
    double x;
    double y;
    double z;
};
struct GUnitVector
{
    double x;
    double y;
    double z;
};
struct GLine
{
    GPosition m_rootPoint;    /* Point on the line, should be in region of interest */
    GUnitVector m_direction;    /* Gradient */
    double m_start;        /* Parameter of start point, must be <= m_end */
    double m_end;          /* Parameter of end point */
};



class GraphicSerializer
{
public:

    GUID objectID;

    uint32_t aspectID;
    uint32_t controlFlag;
    vector<const GLine *> geomVector;

    void Serialize(IStream &pStream);
    void Deserialize(IStream* pStream);
};

In LibB, 4 APIs will be there:在 LibB 中,将有 4 个 API:

  1. Object GetObjectFromStream(Stream s)-> Takes stream as input and deserializes it and returns Objects Object GetObjectFromStream(Stream s)-> 以 stream 作为输入并反序列化并返回 Objects
  2. PutToDB(Object A)-> persists the given object to DB PutToDB(Object A)-> 将给定的 object 持久化到 DB
  3. Stream GetStreamFromObject(Object a)-> Takes object as input serializes it and returns it. Stream GetStreamFromObject(Object a)-> 将 object 作为输入序列化并返回。
  4. Object GetFromDB(objectID)-> Gets the object from DB based on id Object GetFromDB(objectID)-> 根据 id 从 DB 获取 object
public class CommonBase
    {
        public Guid id { get; set; };
        public byte[] Bytes { get; set; } //contains aspect, control flag, vec<GLine>
    };

    public interface IGraphicRepository
    {
        CommonBase Get(Guid guid);
        bool Put(CommonBase graphic);
    }

    public static class GraphicStreamUtility
    {
        public static CommonBase GetCommonBaseFromStream(Stream stream);
        public static void SerializeCommonBaseToStream(CommonBase obj, Stream stream);
    }

Now I'm writing C++/CLI to use stream generated by libA in libB and vice versa.现在我正在编写 C++/CLI 以使用 libB 中的 libA 生成的 stream,反之亦然。 So that I can persist and retrieve the objects to and from DB.这样我就可以持久化并从数据库中检索对象。

Can anyone please let me know how to convert IStream to.Net Stream and.Net stream to IStream.谁能告诉我如何将 IStream 转换为.Net Stream 和.Net stream 到 IStream。

Stream^ CGDMStreamCLI::ConvertIStreamToStream(IStream *pStream)
{
    ULARGE_INTEGER ulSize{};
    IStream_Size(pStream, &ulSize);
    size_t size = ulSize.QuadPart;
    auto buffer = std::make_unique<char[]>(size);
    ULONG numRead{};
    HRESULT hr = pStream->Read(buffer.get(), size, &numRead);

    if (FAILED(hr) || size != numRead)
    {
        throw std::exception("Failed to read the stream");
    }

    cli::array<Byte>^ byteArray = gcnew cli::array<Byte>(numRead+2);

    // convert native pointer to System::IntPtr with C-Style cast
    Marshal::Copy((IntPtr)buffer.get(), byteArray, 0, numRead);

    return gcnew System::IO::MemoryStream(byteArray);
}

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

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