编写.NET互操作能力的C / C ++一个DLL [英] Writing a DLL in C/C++ for .Net interoperability

查看:122
本文介绍了编写.NET互操作能力的C / C ++一个DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序,我想用C编写的code的一部分
我打算写一个DLL巫婆将互通与.net。
我该怎么做?

In my C# application, I would like to write a part of the code in C. I plan to write a DLL witch would be interoperable with .Net. How can I do that?

推荐答案

有三个基本做正确的方式:

There are essentially three right ways to do it:


  • 使用C ++ / CLI。这是如果这个DLL会只能由.NET所使用的最佳方式。

  • 使用一个的externC兼容的API,与Windows API本身。这是最便携,但不是对您的呼叫如使用类模型重新present你的对象作为方便。结果

    • 这是最好的选择,如果你真的打算在ANSI C(不是C ++)来编写。

    • 对于这条道路,你写你的功能的externC的返回类型__stdcall __declspec(dllexport)的FUNC(PARAMS){...}

    • 您也应该使用来电者提供的非缓冲内存模型,而不是回到你的图书馆内分配的缓冲区。当你需要分配给内部状态内存的情况下,调用者应该把它看作是一个不透明的手柄,你应该提供存取函数的调用者提取数据。在任何情况下主叫有望释放你的库中分配的内存,但它是确定呼叫者问图书馆做释放。

    • Use C++/CLI. This is the optimal way if this DLL is going to be used only by .NET.
    • Use an "extern "C"" compatible API, like the Windows API itself. This is the most portable, but isn't as convenient for your callers as using a class model to represent your objects.
      • This is the best option if you really intend to write in ANSI C (not C++).
      • For this path, you write your functions as extern "C" returntype __stdcall __declspec(dllexport) func(params) { ... }
      • You should also use a "caller-provides-the-buffer" memory model, rather than returning a buffer allocated inside your library. In the cases where you do need to allocate memory for internal state, the caller should see it as an opaque handle and you should provide accessor functions for the caller to extract data. Under no circumstances should the caller be expected to deallocate memory allocated inside your library, however it is ok for the caller to ask the library to do the deallocation.

      • 的实现是从该抽象接口派生具体类,也可以有数据和辅助功能众多,因为这不影响二进制接口。

      • 这是在图书馆大量的工作,但非常便携,方便消费者使用。

      和有一件事是绝对不会做的:

      And there is one thing absolutely NOT to do:


      • 使用__declspec(dllexport)的关于C ++类。

      编辑:我也想解释选项#2一些好的做法,这将最大限度地提高便携性,使本机C / C ++的部分从非托管应用程序,以及使用的

      I want to also explain some good practices for option #2 which will maximize portability and make the native C/C++ parts usable from unmanaged applications as well.

      您可以更轻松地屏蔽用宏,这样做的通常的方法是:

      You can make that easier with a macro, the usual way of doing it is:

      在你的头文件,所有的函数声明看起来像

      In your header file, all the function declarations look like

      MYPROJECTAPI(returntype) PublicFunc(params);
      

      在你的项目中,定义是

      #define MYPROJECTAPI(returntype) \
                         extern "C" returntype __stdcall __declspec(dllexport)
      

      在消费项目

      #define MYPROJECTAPI(returntype) \
                         extern "C" returntype __stdcall __declspec(dllimport)
      

      然后您可以采用不同定义宏为其他编译器GCC一样不使用 __ declspec

      完整的解决方案看起来像(在公共头文件 myproject.h

      The complete solution would look like (in public header file myproject.h):

      #if _WIN32
      #  if BUILDMYPROJECT
      #    define MYPROJECTAPI(returntype) \
               extern "C" returntype __stdcall __declspec(dllexport)
      #  else
      #    define MYPROJECTAPI(returntype) \
               extern "C" returntype __stdcall __declspec(dllimport)
      #  endif
      #else
      #  define MYPROJECTAPI(returntype) extern "C" returntype
      #endif
      

      和那么你的Visual C ++项目将导致 BUILDMYPROJECT 建筑myproject.dll时要定义

      and then your Visual C++ project would cause BUILDMYPROJECT to be defined when building myproject.dll

      这篇关于编写.NET互操作能力的C / C ++一个DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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