views:

83

answers:

4

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

+3  A: 

You need to look at different kinds of interop. The easiest is P/Invoke. Then is is COM Interop. And then finally you can use Managed C++

Preet Sangha
A: 

This is the concept you need to use :)

astorcas
+2  A: 

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));

  }
}
SjB
This is an example of the P/Invoke concept.
Greg Domjan
+2  A: 

Have access to the C++ library source

The cleanest way is to use C++ Interop to create a wrapper as a mixed mode assembly. This allows you to use the pragmas managed and unmanaged to switch between native and managed code. This way you can nicely wrap your C++ class with a managed C++ wrapper and call it from (of course) managed C#.

Be aware this won't work under Mono.

Don't have access to the C++ library source, but know a bit of C++?

You can write a small managed wrapper in managed C++ to call the unmanaged C++ library due to C++'s unique ability to call COM natively.

This is done using custom runtime callable wrappers, or CRCWs. You can then call your managed wrapper directly from C# using native .Net types and all.

Benefits of this (as stated by MSDN):

The interop code is built into the application, so there is no dependency on a separate assembly. Also, the exposed managed interface is customized to be more .NET-like. For example, the RenderFile method takes a System.String instead of a char*. The managed version of the COM interface is called a Custom Runtime Callable Wrapper (CRCW).

Don't have access to the C++ library source and don't know C++?

Then you are stuck with C++ COM Interop which is a bit more messy. You'll need to generate a wrapper using Tlbimp.exe.

It's messy because:

  1. Types that don't have a direct match in managed code are exposed as IntPtr pointer types (quite difficult to handle in C#)
  2. The resulting assemlblies will be very large as interfaces for everything in the library are generated (not just the stuff you want).
  3. You'll have to deloy an additional assembly with your application and manage versioning etc...
badbod99