本文介绍了我如何使用C ++ DLL在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢进口C ++ DLL在我的C#应用​​程序,我怎么能做到这一点?,是什么概念,我需要使用此?

i like import c++ dll in my c# application how can i Do this?, what is concept i need to use for this?

推荐答案

这的,与MinGW的编译DLL。
在C-锋利你可以按照下面的代码:

suppose this DLL that compile with MinGW. in C-sharp you can follow this code:

using System.Runtime.InteropServices;
using System;

class call_dll {

  [StructLayout(LayoutKind.Sequential, Pack=1)]
  private struct STRUCT_DLL {
    public Int32  count_int;
    public IntPtr ints;
  }

  [DllImport("mingw_dll.dll")]
  private static extern int func_dll(
      int an_int,
      [MarshalAs(UnmanagedType.LPArray)] byte[] string_filled_in_dll,
      ref STRUCT_DLL s
   );

  public static void Main() {

    byte[] string_filled_in_dll = new byte[21];


    STRUCT_DLL struct_dll = new STRUCT_DLL();
    struct_dll.count_int = 5;
    int[]  ia = new int[5];
    ia[0] = 2; ia[1] = 3; ia[2] = 5; ia[3] = 8; ia[4] = 13;

    GCHandle gch    = GCHandle.Alloc(ia);
    struct_dll.ints = Marshal.UnsafeAddrOfPinnedArrayElement(ia, 0);

    int ret=func_dll(5,string_filled_in_dll, ref struct_dll);

    Console.WriteLine("Return Value: " + ret);
    Console.WriteLine("String filled in DLL: " + System.Text.Encoding.ASCII.GetString(string_filled_in_dll));

  }
}

这篇关于我如何使用C ++ DLL在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:53