Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function from DLL?

I'm new to C# and I'm trying to learn to usage of DLLs. I'm trying to wrap my objects in a DLL, and then use it in my program.

public class Foo   // its in the DLL {    public void Bar()    {       SomeMethodInMyProgram();    }  } 

So I try to pack this to a DLL but I can't, because compiler doesn't know what the SomeMethodInMyProgram() is.

I would like to use it like:

class Program // my program, using DLL {     static void Main(string[] args)     {        Foo test = new Foo();        test.Bar();     }  }  
like image 638
Dominik Antal Avatar asked Feb 15 '11 23:02

Dominik Antal


People also ask

How do you call a DLL function in C#?

Add the DLL via the solution explorer - right click on references --> add reference then "Browse" to your DLL - then it should be available.

How do I view DLL functions?

If a DLL is written in one of the . NET languages and if you only want to view what functions, there is a reference to this DLL in the project. Then doubleclick the DLL in the references folder and then you will see what functions it has in the OBJECT EXPLORER window.


1 Answers

Depends on what type of DLL. Is this built in .NET ? if it is unmanaged code then here is an example otherwise the Answer from Rob will work.

Unmanaged C++ dll example:

using System; using System.Runtime.InteropServices; 

You may need to use DllImport

[DllImport(@"C:\Cadence\SPB_16.5\tools\bin\mpsc.dll")] static extern void mpscExit(); 

or

[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type); 

Then each of those are called like this:

// a specific DLL method/function call mpscExit(); // user32.dll is Microsoft, path not needed MessageBox(new IntPtr(0), "Test", "Test Dialog", 0);   
like image 108
Tom Stickel Avatar answered Sep 24 '22 05:09

Tom Stickel