Call function from DLL?

106,594

Solution 1

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

Solution 2

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

Solution 3

I am late to the party here but am leaving this answer for someone pulling his/her hair out like me. So basically, I did not have the luxury of VS IDE when facing this issue.I was trying to compile the code via cmdline using csc. In order to reference a dll, just add the compiler flag /r:PathToDll/NameOfTheDll to csc.

The command would look like

csc /r:PathToDll/NameOfTheDll /out:OutputExeName FileWhichIsReferencingTheDll.cs

In FileWhichIsReferencingTheDll.cs add using namespace AppropriateNameSpace; to access the functions (by calling class.functionName if static or by creating an object of the class and invoking the function on the object).

Solution 4

you need to actually load the DLL into your application at run time, thus the Dynamic part of DLL. You also need the header file that defines what functions are in the DLL so your compile knows what functions have been defined. My knowledge here is based on C++ so how this works for C# I am not to sure, but it will be something like that...

Solution 5

This is my source code for DLL (AllInOne) having a class named Calculate which has a method GetAreaofSquare.

namespace AllInOne
{
    public class Calculate
    {   
        public double GetAreaOfSquare(double side)
        {
            return side * side;
        }
    }
}

I have added this DLL in the reference located in the solution explorer of the project, which is a console application, and added AllInOne in the system namespace. Please see carefully "using AllInOne". We can instantiate Calculate class as shown below and then can use the method GetAreaofSquare to calculate the area of the Square.

using AllInOne;

namespace UsingDLLinApplication
{
    public class GetResult
    {
        static void Main()
        {
            Calculate myEveryCalculation = new Calculate();
            double storeAreaOFSquare = myEveryCalculation.GetAreaOfSquare(4.5);
            Console.WriteLine("The area of Square is {0}", storeAreaOFSquare);
            Console.ReadLine();
        }
     }
}
Share:
106,594
Dominik Antal
Author by

Dominik Antal

Forever learning.

Updated on September 24, 2020

Comments

  • Dominik Antal
    Dominik Antal almost 4 years

    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();
        }
     } 
    
  • Jimmy
    Jimmy over 13 years
    C# is quite different to C\C++ in this regard. The .NET runtime can extract sufficient information from the .dll to determine what the method signatures are (there are no header files in c#)
  • thecoshman
    thecoshman over 13 years
    ah, well then. erm... carry on
  • TheMonkeyMan
    TheMonkeyMan almost 11 years
    The guy was clearly asking for a simple 'Add Reference' Answer why would you go and throw this at them. For a new developer Extern and DllImport are rather advanced.
  • Tom Stickel
    Tom Stickel almost 11 years
    Those are common "easy" ways DllImport is super easy, there are much more advanced ways of doing this FYI.
  • CyanCoding
    CyanCoding almost 5 years
    If the DLL isn't built in, you cannot access it this way. I recommend Tom Stickel's answer if that is the case.
  • Rob
    Rob almost 5 years
    @CyanCoding what do you mean "built in" - the DLL doesn't have to be installed on the machine in the GAC or anywhere to add it this way. I could send you a DLL I've built via email and you can attach it using this method.
  • Admin
    Admin almost 4 years
    Hi, can i call this in client computer? can I adjust the path for it?
  • Tom Stickel
    Tom Stickel almost 4 years
    @Mindex No idea really - I was forced into doing C++ in 2013 for a big chip company, and I haven't done C++ since.
  • phil294
    phil294 over 2 years
    What is mpsc and how does it relate to Foo from OP's question?