如何将一个二进制blob从C ++传递到C#? [英] How can I pass a binary blob from C++ to C#?

查看:115
本文介绍了如何将一个二进制blob从C ++传递到C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要是一个C#开发(从大学起不是很多C ++),但是工作将一个大量的现有C ++代码集成到一个应用程序中。我有一个C ++ / CLI程序集,缓冲两个世界,从C#通过到C ++工作正常。我有的问题是,C ++类有一个方法调用,生成一个二进制blob(在C#世界中的数组 byte ),我需要在C#过程(像一个坚实的袋子传递)。



我正在寻找的是如何处理两个世界之间的缓冲区/包装器方法(C ++ / CLI)的建议。我假设我传递一个 char * 和长度,但C#看到作为字节* m假设是一些C ++ / CLI魔术)。



我也试过通过 array< Byte ^> ^ 但是没有太多的幸运翻译 char * 从C ++ lib的其余部分到 byte 数组... 。

解决方案

我拍了一下,想出了这个。没有什么疯狂,只是分配管理数组,复制数据并返回。



头:

  #pragma once 
using namespace System;
using namespace System :: Runtime :: InteropServices;
命名空间CLRLib
{
public ref class TwiddlerFunctions
{
public:
static array< Byte> ^ GetArray();
};
}

实施:

  #includeCLRLib.h
array< Byte> ^ CLRLib :: TwiddlerFunctions :: GetArray()
{
unsigned char data [] = {1,2,34,5};
//将非托管数组转换为托管数组
array< Byte> ^ arr = gcnew array< Byte>(sizeof data);
Marshal :: Copy((IntPtr)data,arr,0,arr-> Length);
return arr;
}

C#side:



b
$ b

  using System; 
使用CLRLib;
class Program
{
static void Main(string [] args)
{
byte [] arr = TwiddlerFunctions.GetArray
Console.WriteLine(String.Join(,arr)); / / pre>

I'm primarily a C# dev (not much C++ since college), but working on integrating a large collection of existing C++ code into an application. I have a C++/CLI assembly that is buffering the two worlds and have communication from C# through to C++ working fine. The question I have, is that the C++ class has a method call that generates a binary blob (think array of bytes in C# world) that I need to get in C# and process (pass around like a solid bag).

What I'm looking for is advice on how to handle the buffer/wrapper method (C++/CLI) between the two worlds. I assumed that I'd pass a char* and length, but C# sees that as a byte* (I'm assuming that is some C++/CLI "magic").

I also tried passing array<Byte ^>^ but that haven't had much luck translating the char* from the rest of the C++ lib to the byte array... and what I have doesn't smell right.

解决方案

I took a shot at it and came up with this. Nothing crazy, just allocate the managed array, copy the data and return it.

header:

#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace CLRLib
{
    public ref class TwiddlerFunctions
    {
    public:
        static array< Byte >^ GetArray();
    };
}

implementation:

#include "CLRLib.h"
array< Byte >^ CLRLib::TwiddlerFunctions::GetArray()
{
    unsigned char data[] = { 1, 2, 34, 5 };
    // convert the unmanaged array to a managed array
    array< Byte >^ arr = gcnew array< Byte >(sizeof data);
    Marshal::Copy((IntPtr)data, arr, 0, arr->Length);
    return arr;
}

C# side:

using System;
using CLRLib;
class Program
{
    static void Main(string[] args)
    {
        byte[] arr = TwiddlerFunctions.GetArray();
        Console.WriteLine(String.Join(" ", arr)); // 1 2 34 5
    }
}

这篇关于如何将一个二进制blob从C ++传递到C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆