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???
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"
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With