Need to run a c# dll from the command line

There is a trick to create unmanaged exports from c# too – https://www.nuget.org/packages/UnmanagedExports

How does it work?
Create a new classlibrary or proceed with an existing one. Then add the UnmanagedExports Nuget package. This is pretty much all setup that is required.
Now you can write any kind of static method, decorate it with [DllExport] and use it from native code. It works just like DllImport, so you can customize the marshalling of parameters/result with MarshalAsAttribute.
During compilation, the task will modify the IL to add the required exports.

class Test
{
  [DllExport("add", CallingConvention = CallingConvention.Cdecl)]
  public static int TestExport(int left, int right)
  {
     return left + right;
  } 
}

Leave a Comment