1

I tried to call the function from .dll file using java native interface ,its successfully working, But I don't know how to call function from .dll using C# ,please advice me on this.

5 Answers5

1

Look at DLLImport attribute in MSDN http://msdn.microsoft.com/en-us/library/aa664436(v=vs.71).aspx

using System;
using System.Runtime.InteropServices;

class Example
{

    [DllImport("your_dll_here.dll")]
    static extern int SomeFuncion1(int parm);
    static void Main()
    {
        int result = SomeFunction1(10);
    }
}
evpo
  • 2,436
  • 16
  • 23
1

If it's a native DLL, you need to add a DLLImport statement, importing the function you want.

The documentation is here.

The attribute looks like this, typically:

[DllImport("YourDllNameHere.dll")]
public static extern int YourFunction(int input);

This will import a function called YourFunction (which takes an int input, and returns an int) from YourDllNameHere.dll.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

Let's say your DLL name is MyLibrary and MyFunction is the function contain in your DLL .

First right click on your Reference , browse and add your DLL .
Declare your DLL as a namespace using MyLibrary;
And you can call MyFunction !

Or

Another way , you can use this msdn reference !

zey
  • 5,939
  • 14
  • 56
  • 110
0

Add that dll into your project as a referenced dll into reference folder(right click on references ,add reference then "Browse" to you DLL).then it should be available for to use as you want and just use that dll as follows in the code level.

using System;
using YourDllName;

class ExampleClass
{
 //you can use your dll functions
}
Thilina H
  • 5,754
  • 6
  • 26
  • 56
0

I like the link provided by Baldrick for DllImport attribute.

This is what I recommend.

  1. Download Dependency Walker (small application exe, no need to install).

  2. Open your DLL in the Dependecy Walker to view the exposed entry points of DLL.

  3. Declare external call to native function in C# like this.

C#:

[DllImport("Your_DLL.DLL", EntryPoint="Function_Entry_Point",CallingConvention=CallingConvention.StdCall)]
static extern IntPtr Function1();

Note:

  • If entry point is not specified the function name is considered as the entry point.
  • When you run the application make sure the native DLL is in the same folder.
Usman Zafar
  • 1,919
  • 1
  • 15
  • 11