C# talking to C++ library - AAF SDK

…衆ロ難τιáo~ 提交于 2020-01-24 01:46:08

问题


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/.

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

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.

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?


回答1:


You have basically two ways of doing that:

Using C++ Interop (implicit PInvoke)

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.

Using good ol' PInvoke

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.



来源:https://stackoverflow.com/questions/24525394/c-sharp-talking-to-c-library-aaf-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!