简体   繁体   English

从C ++非托管应用程序检索C#.NET映像

[英]Retrieve C# .NET image from C++ unmanaged application

I'm using openFrameworks to render a video stream. 我正在使用openFrameworks渲染视频流。 OpenFrameworks uses the ofImage class, which stores the images as a vector of ofPixel (which stores an unsigned char vector). OpenFrameworks使用ofImage类,该类将图像存储为ofPixel的矢量(存储unsigned char矢量)。

I am using a C# .NET dll to retrieve each frame. 我正在使用C#.NET dll检索每个帧。 This library uses Bitmap to return the retrieved frames. 该库使用Bitmap返回检索到的帧。 So far I know C# .NET works under the Common Language Runtime . 到目前为止,我知道C#.NET在Common Language Runtime下工作

My question is, what .NET tools or functionalities can I use in order to comunicate native C++ code and managed C#? 我的问题是,我可以使用哪些.NET工具或功能来通信本机C ++代码和托管C#?

In order to retrieve the image I used this tutorial . 为了检索图像,我使用了本教程 It thoroughly explains how to consume a C# managed library from unmanaged code. 它彻底解释了如何从非托管代码使用C#托管库。

The process I followed is documented here . 我遵循的过程在此处记录

Here's an overview 这是概述

  • create a C++/CLI project (CLR Class Library in Visual Studio's New Project dialog) 创建一个C ++ / CLI项目(Visual Studio的“新建项目”对话框中的“ CLR类库”)
  • add a reference to the C# dll create native classes and/or functions that expose the necessary C# dll functionality to the native world; 添加对C#dll的引用,以创建将必要的C#dll功能公开给本机世界的本机类和/或函数;
  • export them using the usual means (__declspec(dllexport) or .def file) 使用通常的方法(__declspec(dllexport)或.def文件)导出它们
  • add the .lib produced by the C++/CLI project as linker input in your native project(s) 将C ++ / CLI项目产生的.lib添加为您的本机项目中的链接器输入

About the image information, it can be referred to as a byte array. 关于图像信息,它可以被称为字节数组。 It has to be pinned in the managed code using a GCHandle. 必须使用GCHandle将其固定在托管代码中。 Here is some code to illustrate this concept (Disclaimer, this answer was provided by another author in the original msdn forum post ): 以下是一些代码来说明此概念(免责声明,此答案由原msdn论坛帖子中的另一位作者提供):

#include <unordered_map>

using namespace System;
using namespace System::Runtime::InteropServices;

std::unordered_map<void *, void *> dataHandles;

void *getData() {
    array<Byte>^ arr = gcnew array<Byte>(2);
    arr[0] = 'C';
    arr[1] = '#';

    GCHandle handle = GCHandle::Alloc(arr, GCHandleType::Pinned);
    void *data = (void *)handle.AddrOfPinnedObject();
    dataHandles.insert(std::make_pair(data, (void *)GCHandle::ToIntPtr(handle)));
    return data;
}

void releaseData(void *data) {
    auto i = dataHandles.find(data);

    if (i != dataHandles.end())
        GCHandle::FromIntPtr((IntPtr)i->second).Free();
}

#pragma unmanaged

void native() {
    char *data = (char *)getData();

    for (int i = 0; i < 2; i++)
        printf("%c ", data[i]);

    releaseData(data);
}

#pragma managed

int main(array<System::String ^> ^args) {
    native();
    return 0;
}

More about specific Bitmap to unsigned char * conversion here 有关将特定Bitmap转换为unsigned char *更多信息,请unsigned char * 此处

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

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