Nico
Nico

Reputation: 1956

Need to run a c# dll from the command line

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.

Upvotes: 13

Views: 23289

Answers (4)

Nick Bilak
Nick Bilak

Reputation: 201

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

Upvotes: 17

Emond
Emond

Reputation: 50672

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

Upvotes: 1

Andy
Andy

Reputation: 30418

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500515

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.

Upvotes: 14

Related Questions