Hi,
Im trying to find out how i would pass a value, in this case a string, to another process
My app detects if another instance of it is currently running, if it is then it shuts down, but when the user opens a file with it i want to pass the string (filename) over to the exsisting window so it can open before this one shuts down.
Any help or a point in the right direction would be excellent. Thanks, -Dave

Passing a variable to another process
Cerem Beyazıt
I have been working on it for a while and still no luck, i cant seem to find any examples when searching where someone has done this outside a simple command line app that does nothing but count.
Anyone help
Burnt1ce
Thanks Mark but i think i would be better of going with ipc.
But im having some trouble with it, i can get any of the examples off the web to work, they compile, but i dont know how to pass the message sent to a text box or even a message box so i can see what im doing. I have posted my code below. Thanks, -Dave
//in the form
public
Form1(string[] args){
InitializeComponent();
IpcChannel chan = new IpcChannel("Server"); ChannelServices.RegisterChannel(chan, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemotingServer", WellKnownObjectMode.SingleCall);}
//the creation of the program
[
STAThread] static void Main(string[] args){
Process[] RunningProcesses = Process.GetProcessesByName("WindowsApplication1"); if (RunningProcesses.Length == 1){ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(args));}
else{ IpcChannel chan = new IpcChannel("Client"); ChannelServices.RegisterChannel(chan, false); RemoteObject remObject = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "ipc://Server/RemotingServer"); if (remObject != null)remObject.ReplyMessage(
"You there ");ShowWindowAsync(RunningProcesses[0].MainWindowHandle, 2);
ShowWindowAsync(RunningProcesses[0].MainWindowHandle, 9);
}
}
DannoCoy
msksurfer
It works perfect!
With the headace this thing caused my i will fly you to italy for that pizza!
Thanks Mark, appreciate you taking the time to do that out for me.
All the best,
-Dave
waywar405368
This link should have everything you need to get a client server scenario working: http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx
Mark.
Gareth Hunter
As well as using remoting you could use something as simple as a text file that your applications can share information on, or a memory mapped file which is an area of memory that processes can share (see http://msdn2.microsoft.com/en-us/library/aa366537.aspx). Of course with these methods you would need to be polling the data store to see if it changes.
Mark.
sj_h1
Hi
first off, in .net 2.0 you can configure your program to allow only one instance of itself (via the properties window)
secondly, you would need to implement remoting to cross the Application boundaries
example: http://www.codeguru.com/csharp/csharp/cs_network/remoting/article.php/c10199/
what you would need to is the following:
listen on a certain channel,
if a second instance of the app is openend with a filename send the filename to the first
(via the channel on which the first listens) and close the second..
Hope this helps you out, please close the thread if it does
John Woodiwiss
Just one follow up question, when i went to intergrate it into my windows forms based application it works just fine, when it goes to create the new from it checks if any are running it creates a client and sends out the arguments, but how do they get to the original program! .
Sorry if im missing something really obvious, but i just dont see how i can do it. I have uploaded the code if someone would mind taking a look at it for two minutes. WindowsApplication1
Thanks, -Dave
Jayx
Hi,
take a look at http://www.markdawson.org/software/windowsapplication2.zip. Basically what I do in this application is if it is the first instance of the application starting I create a server, when the form loads you will see a textbox that says doc1.doc in it. Next when you launch the exe again it will call a method in the remoting client to pass the new document name, this creates a client that connects to the server and then creates a remote object (this object really lives on the server instance of the application), then a method is called which passes in the new document path. The method raises an event on the server that a new path has been received and updates the textbox in the first instance of the application, the second instance of the form then closes.
Run this code outside of the debugger.
Hope that helps you out, if so you owe me a pizza because I burnt mine while writing this code :-)
Take care
Mark.