user2905737
user2905737

Reputation: 11

How Call the function from .dll using c# program

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.

Upvotes: 1

Views: 341

Answers (5)

Usman Zafar
Usman Zafar

Reputation: 1979

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.

Upvotes: 0

Baldrick
Baldrick

Reputation: 11840

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.

Upvotes: 1

Thilina H
Thilina H

Reputation: 5804

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
}

Upvotes: 0

zey
zey

Reputation: 6105

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 !

Upvotes: 0

evpo
evpo

Reputation: 2531

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

Upvotes: 1

Related Questions