简体   繁体   中英

how to pass arguments to windows services in c#?

I just want to know how to pass arguments to windows services . The problem is , i am developed a windows application and then called it from Onstart() method . Now , i want to call the particular function from another project .

 protected override void OnStart(string[] args)
        {

            Process.Start("C:\\Program Files\\macro.exe");

        }

I want to call a function inside the macro.exe project with arguments . Any suggestions

If you want to use arguments passed to service, you can try:

Process.Start(@"C:\Program Files\macro.exe", String.Join(" ", args))

Note that using @Tudor answer you can have better control on started process, redirect input/output/error, show/hide window and many other nice things.

I wonder if at all you can call a function inside another EXE. May be you can make a common DLL and refer it in macro.exe and your own application. If thats not a choice then you can either use sockets or file system to communicate with the EXE. For eg. you pass your EXE 2 command line arguments "MyFunction", "Hello" as shown in previous comments above and the Exe grabs those args deduces that it needs to call MyFunction with argument Hello and to get back the result the EXE writes it to a predefined path and you read it from the file.

If the Exe is running and you can not pass command line to it, then may be the Exe keeps scanning an input file to get commands from external sources like your app. Or better still it listens on a port on which you fire commands to it.

I am not sure if there can be another more starightforward solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM