How in C# to cosmati C-like DLL(which can dynamically load)?

For example in C exporting functions looks like this:
extern "C" __declspec(dllexport) int Run() {
 return 488;
}


In the main program I load the DLL I need and I use them ekportiruemuyu function like this:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace wfa
{
 public partial class Form1 : Form
{
 public Form1()
{
InitializeComponent();
}

 private void button1_Click(object sender, EventArgs e)
{
MyClass.LoadDll(@"D:\dev\csharp\test\Debug\cpx.dll");
 var result = MyClass.Run().ToString();
 MessageBox.Show(result, result);
}
}

 public class FunctionLoader
{
[DllImport("Kernel32.dll")]
 private static extern IntPtr LoadLibrary(string path);

[DllImport("Kernel32.dll")]
 private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

 public static Delegate LoadFunction<t>(string dllPath, string functionName)
{
 var hModule = LoadLibrary(dllPath);
 var functionAddress = GetProcAddress(hModule, functionName);
 return Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
}
}

 public class MyClass
{
 public delegate int FnDelegate();
 public static FnDelegate Run;

 public static void LoadDll(string path)
{
 Run = (FnDelegate)FunctionLoader.LoadFunction<fndelegate>(path, "Run");
}
}
}</fndelegate></t>

Bwl1EUeO-mU.jpg
full source code: https://github.com/DailyDDose/DllTesting
June 10th 19 at 15:23
3 answers
June 10th 19 at 15:25
This is the development of what you tried.
There is a Nuget package.
Can't sleep. Sent PR. - chaz commented on June 10th 19 at 15:28
actually it's a Nuget package I have installed in VisualStudio. - Marisol commented on June 10th 19 at 15:31
just now noticed the PR, thank you)
I already worked the idea in a separate repo https://github.com/DailyDDose/ExampleDLL - Marisol commented on June 10th 19 at 15:34
You have to understand that this is more a hack than a native solution. But I several years ago tried https://sites.google.com/site/robertgiesecke/Home/... from which grew DllExport. I was using it to load .NET biblioteku of Delphi. - Kaelyn.Kuhn20 commented on June 10th 19 at 15:37
Yes , this is a hack, though, and devilishly handsome. The native one solution: Component Object Model. - chaz commented on June 10th 19 at 15:40
agree. I am glad that the project was not abandoned. - Kaelyn.Kuhn20 commented on June 10th 19 at 15:43
June 10th 19 at 15:27
C# is not designed for such tasks

If you want to make calls from unmanaged code to NET assemblies, or use COM wrapper in C++ CLI
C# is not designed for such tasks

Agree. Only after all implementation is damn beautiful: just enter into the Assembly of the export table, the benefit of the PE header is from a box. - chaz commented on June 10th 19 at 15:30
nevertheless I managed to implement the idea in a separate repo https://github.com/DailyDDose/ExampleDLL - Marisol commented on June 10th 19 at 15:33
June 10th 19 at 15:29
the solution to Unmanaged Exports from Robert Giesecke 's not going, errors occur at compile time
Error The command "NSBin.bat "D:\dev\csharp\test\packages\DllExport.1.5.2\tools\..\lib\net20\DllExport.dll" "System.Runtime.InteropServices" "true"" exited with code 1.

class Test
{
 [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
 public static int Run()
{
 1082 return;
}
}

Find more questions by tags C#C