Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Dll using C# program

Tags:

c#

dll

I have a C# form application...i created a Dll...now i want to launch that dll using this program. how do i do it?

#include <windows.h>

typedef int (*function1_ptr) ();

function1_ptr function1=NULL;

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { 

HMODULE myDll = LoadLibrary("Dll1.dll"); 

    if(myDll!=NULL) {  
        function1 = (function1_ptr) GetProcAddress(myDll,"function1");  

        if(function1!=NULL)  
            function1();
        else
            exit(4);

        FreeLibrary(myDll);
    }
    else
        exit(6);
    GetLastError();

    return 0;
}

This was the code used to test my dll...i.e Dll1.dll..function1 was the function within dll1.dll.....can i do something similar with the C# code???

like image 388
Vinod K Avatar asked Feb 02 '11 16:02

Vinod K


People also ask

How do I run a DLL file?

Click Start > All Programs > Accessories and right-click on "Command Prompt" and select "Run as Administrator" OR in the Search box, type CMD and when cmd.exe appears in your results, right-click on cmd.exe and select "Run as administrator" At the command prompt, enter: REGSVR32 "PATH TO THE DLL FILE"

What is DLL in C?

In Windows, a dynamic-link library (DLL) is a kind of executable file that acts as a shared library of functions and resources. Dynamic linking is an operating system capability. It enables an executable to call functions or use resources stored in a separate file.

How do I load a DLL in C++?

Using a DLL in C++ code using LoadLibraryA function Now you need to first load the library using the LoadLibrary function which is defined in libloaderapi. h header and aliased to LoadLibraryW using #define macros. It accepts only one argument: a long pointer to wide string as the file name of the library.


2 Answers

To do what your code example does, use the following C# code:

public static class DllHelper
{
    [System.Runtime.InteropServices.DllImport("Dll1.dll")]
    public static extern int function1();
}

private void buttonStart_Click(object sender, EventArgs e)
{
    try
    {
        DllHelper.function1();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}      

The above example is a C# program that calls a function in non .NET based DLL. The example below is a C# program that calls a function in a .NET based DLL.

try
{
    System.Reflection.Assembly dll1 = System.Reflection.Assembly.LoadFile("Dll1.dll");
    if (dll1 != null)
    {
        object obj = dll1.CreateInstance("Function1Class");
        if (obj != null)
        {
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("function1");
            mi.Invoke(obj, new object[0]);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Is any of the the 2 examples what you want? Or are you trying to call a C# function in a DLL from your example code?

like image 132
Fun Mun Pieng Avatar answered Sep 28 '22 00:09

Fun Mun Pieng


I assume you want to use the functionality of the DLL? If so, create a reference to the DLL and consume it in your C# forms application. In other words, create a "user" interface for application logic contained in a DLL. If this does not make sense, you should look up how to add a reference to a project.

like image 39
Gregory A Beamer Avatar answered Sep 28 '22 00:09

Gregory A Beamer