Need to run a c# dll from the command line

后端 未结 4 868
日久生厌
日久生厌 2020-12-03 14:51

I have a c# dll defined like this:

namespace SMSNotificationDll
{
    public class smsSender
    {
        public void SendMessage(String number, String mess         


        
相关标签:
4条回答
  • 2020-12-03 15:34

    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;
      } 
    }
    
    0 讨论(0)
  • 2020-12-03 15:35

    See this question you can't run a .NET dll using rundll32

    0 讨论(0)
  • 2020-12-03 15:39

    Why don't you just create a simple console application which refers to the DLL as a class library?

    namespace SMSNotificationDll
    {
        public class SmsSenderProgram
        {
            public static void Main(string[] args)
            {
                // TODO: Argument validation
                new smsSender().SendMessage(args[0], args[1]);
            }
        }
    }
    

    Btw, I'd rename smsSender to something like SmsSender.

    0 讨论(0)
  • 2020-12-03 15:48

    RunDll32 only works with DLLs specifically designed to be called from it. See http://support.microsoft.com/kb/164787 for more information.

    The easiest way to run the code in that DLL from the command line would be to make a simple C# command line app whose sole purpose is to call that method.

    0 讨论(0)
提交回复
热议问题