-->

Need to run a c# dll from the command line

2019-01-12 00:47发布

问题:

I have a c# dll defined like this:

namespace SMSNotificationDll
{
    public class smsSender
    {
        public void SendMessage(String number, String message)
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "c:\\Program Files\\Java\\jdk1.6.0_24\\bin\\java";
            info.WorkingDirectory = "c:\\";
            info.Arguments = "-jar SendSms.jar "+number + " "+message;
            info.UseShellExecute = false;
            Process.Start(info);
        }
    }
}

and i need to execute it from the commandline.

Is there any way I can run it through rundll32?

When I run it with this :

rundll32 SMSNotificationDll.dll, SendMessage 0749965244 hello

I get missing entry: SendMessage.

回答1:

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.



回答2:

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;
  } 
}


回答3:

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.



回答4:

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



标签: c# rundll32