Need to run a c# dll from the command line

℡╲_俬逩灬. 提交于 2019-12-17 12:38:11

问题


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



来源:https://stackoverflow.com/questions/6138812/need-to-run-a-c-sharp-dll-from-the-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!