Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can pass values as arguments to another EXE while opening that EXE.

But when it is running how to pass values to that EXE.

Is there any way to do this.
Posted

You can do that by using Process's arguments :

C#
System.Diagnostics.Process.Start("executable", "arg1 arg2 \"string with space as arg3\"");

The argument part must conform to the command line format, each argument is separated by space and you can have an argument with spaces by delimiting it with double quote (")

In your new process, you can retrieve theses arguments in your Main method

C#
static void Main(string[] args)

In there, args[0] will be equal to arg1, args[0] to arg2 and args[2] to string with space as arg3
 
Share this answer
 
Comments
KUMAR619 30-Jul-14 1:53am    
Its good to use when opening that exe.
I am talking about running exe.

Just think you have given the solution for opening a new exe.
Its easy but how to access that after that. i.e. Running
Sergey Alexandrovich Kryukov 30-Jul-14 1:54am    
Correct, a 5. I provided the addition to this information in Solution 2.
—SA
The method of inter process communication you choose for communicating data between processes depends on actual requirements. Many methods allow the two processes to be running on separate machines. If they are always on the same machine Memory Mapped files can create a very workable solution.
http://msdn.microsoft.com/en-us/library/dd997372(v=vs.110).aspx[^]
 
Share this answer
 
You need to use something known as Inter Process Communication mechanism. Once you start an executable, you can communicate with it via IPC.
Process Address space is guarded from other processes by Operating System. IPC is a mechanism, provided by the OS for process-communication.
Named Pipes in your case can be useful.
 
Share this answer
 
KUMAR619 wrote:
I want to pass values to that running EXE without creating new EXE.
This is quite possible, but first I want to warn you: in most cases, this is a pretty bad idea. You want to exchange data and actually some events between different existing processes. The processes are primarily designed for isolation, not for collaboration. Indeed, the are strongly isolated, even executed in different address spaces.

First of all, think about having just one process, and what you had for the second application, rework into the class library to be used in another one.

If you still have to have different processes, for one or another reason, you need some IPC. Please see:
http://en.wikipedia.org/wiki/Inter-process_communication[^].

With .NET, it may range from using sockets, named pipes, to "classical" remoting to WFC (typically, self-hosted in the application receiving messages from another applications). In turn, both remoting and WFC use channels (and their operation is abstracted from particular channels), and the channels use internally either named pipe (this type of channel is also called "TPC", but this is slightly different meaning of this term) or network sockets. By the way, you may not need physical network; originally, sockets were development as an IPC device for communications on the same computer.
Please see:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.sockets%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-US/library/kwdt6w2k%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms754130%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/aa970268(v=vs.110).aspx[^].

Besides, you will need separate threads in one or both applications (if in one, it should be receiving application; the thread listening for messages from another application). You need to avoid polling of data, so, you waiting calls should be blocking, which is possible with named pipes or sockets.

—SA
 
Share this answer
 
In addition to Solution 1: the command line parameters are passed just as string, so your application will need to parse those string into data you need. For some parsing code, please see my CodeProject article: Enumeration-based Command Line Utility[^].

Another CodeProject article is referenced from mine and recommended.
 
Share this answer
 
Comments
KUMAR619 30-Jul-14 2:03am    
Sir Solution 1 is used to start a new Process in the form of EXE then we can pass arguments while Opening that.

But My problem is that I've done all the process above and passed the parameters via arguments. It works fine.

But If I want to change the values to that running EXE I am facing problem.
I want to pass values to that running EXE without creating new EXE.
Sergey Alexandrovich Kryukov 30-Jul-14 2:22am    
You should have told that in first place. Your question was misleading. I'll answer...
—SA
KUMAR619 30-Jul-14 2:24am    
Sorry Sir. Hoping now you have understood my question. Waiting for you valuable answer.
Thanks in Advance
Sergey Alexandrovich Kryukov 30-Jul-14 2:38am    
Done.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900