C# 与 C++ 库对话 - AAF SDK [英] C# talking to C++ library - AAF SDK

查看:48
本文介绍了C# 与 C++ 库对话 - AAF SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 c# 中读取 AAF 文件(描述已编辑的视频和音频序列).AAF 是开源的,并且有一个 C++ SDK 可用 http://sourceforge.net/projects/aaf/.

I'm trying to read AAF files (that describe edited video and audio sequences) in c#. AAF is open source, and there's a C++ SDK available http://sourceforge.net/projects/aaf/.

我是一名 C# 编码员,虽然我了解一些 C++ 编码范例,但我的 C++ 基本上不存在.

I'm a C# coder and while I understand some of the C++ coding paradigms, my C++ is basically non-existent.

我认为可以使用 pinvoke 进行交互,我之前使用它与 Win32 交互.使用 win32,我从 pinvoke.net 中提取了调用信息,但我不完全确定如何使用第三方 SDK 从头开始​​定义 pinvoke 入口点.

I presume the intereaction is possible using pinvoke which I've used before for interacting with Win32. With win32 I've pulled calling information from pinvoke.net, but I'm not entirely sure how I go about defining the pinvoke entry points from scratch with a third party SDK.

谁能帮我开始做这件事?例如,调用第三方 C++ 库的第一步是什么?是否有工具可以帮助自动化此过程?

Can anyone help me to get started on this? For instance, what are the first steps in pinvoking a third party C++ library? Are there tools for helping automate this process?

推荐答案

基本上有两种方法可以做到:

You have basically two ways of doing that:

Visual C++ 可以与本机 C++ 代码互操作.因此,您可以使用 Visual C++ 作为本机 C++ 和 C# 之间的桥梁.基本上,您必须在 Visual C++ 中编写包装器(或代理")类才能与本机 C++ 对象进行交互.在幕后,此方法使用带有特定选项的 PInvoke(因此称为隐式 PInvoke"),但它的优点是保留了本机 C++ 的面向对象结构.请注意,此方法不可移植到 Mono.

Visual C++ can interop with native C++ code. So you can use Visual C++ as a bridge between the native C++ and C#. Basically you have to write wrapper (or "proxy") classes in visual C++ to interact with your native C++ objects. Under the hood, this method uses PInvoke (therefore "implicit PInvoke") with specific options, but it has the advantage of retaining the object-oriented structure of the native C++. Note that this method is not portable to Mono.

您可以以与您完全相同的方式将 PInvoke 与 C++ 结合使用将 PInvoke 与 C 一起使用.但是有一个限制:你不能以这种方式调用 C++ 方法,所以你失去了 C++ 面向对象的结构.如果您使用的库提供 C API,您可以使用它.否则,您必须创建一个小的 C++ 包装器,它将以 C 风格的自由函数的形式包装 C++ API.如果您有很多类要以这种方式包装,则可以使用 SWIG 自动包装.作为奖励,SWIG 通过一些大师代码生成在 C# 中重新创建了面向对象的结构.但是使用 SWIG 需要一些设置,如果您只需要一些功能,我认为没有必要.

You can use PInvoke with C++ the exact same way as you use PInvoke with C. There is however a limitation: you cannot PInvoke C++ methods this way, so you lose the C++ object-oriented structure. If the library that you are using provides a C API, you can use that. Otherwise, you have to create a small C++ wrapper that will wrap the C++ API in the form of C-style free functions. If you have a lot of classes to wrap this way, you can use SWIG to wrap automatically. As a bonus, SWIG recreates the object-oriented structure in C# via some guru code generation. However using SWIG requires some setup and I don't think it is necessary if you only need a few functions.

这篇关于C# 与 C++ 库对话 - AAF SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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