Click to See Complete Forum and Search --> : Running an External Application only once


dfaison
March 22nd, 2004, 09:08 AM
What I need to do is run only one instance of an external program and have it perform certain functions that can be sent by having it "run" a particular script-like file.
However, using CreateProcess() causes the application to open a copy of the application each time one of these files is called.
I need to have the external application open only once, and remain open for several different calls.

For a simplified example, Notepad should open file1.txt, but then later open file2.txt (the same Notepad, without exiting, and without creating a new Notepad).

I've not learned enough to figure out MSDN on my own, nor seen any examples of this anywhere. Anyone?

Myself dot NET
March 22nd, 2004, 01:12 PM
That is a pretty complicated task. There is no general purpose method to do what you describe. Like with your Notepad example, I know of no easy way to have it open another file once it is open. Maybe you could exploit drag and drop functionality, but that is just an idea. For other applications, there is no guarantee that you will be able to such a task.

If the child application is one that you are writing, you could have the main application (the one that calls CreateProcess()) communicate with the child process via some form of IPC (pipes, etc.)

dfaison
March 22nd, 2004, 02:37 PM
Thanks, hopefully someone knows how.
To be more specific:
I'm writing application1 with CreateProcess() to open another larger & pre-existing (not modifiable) Application2, and sending it a file1 that contains instructions for Application2 (actually, an Application2 specific text file with it's own command language).
Application2 performs the instructions & writes a report which Application1 reads, processes, and either rewrites file1 or selects file2 or file3 (etc.) to send back to Application2 to perform.
This needs to be done 6 to 12 different times per use of Application1 and - just using CreateProcess() - I end up with 6 to 12 different copies of Application2 running, except that the computer usually crashes on opening about the 5th copy.
As Application2 is communicating with other programs and instruments, simply shutting it down after each use (necessary to get past the 5th copy) is extremely time consuming, and on occasion it cannot even restore the communications until the computer is manually shutdown.
The NotePad analogy is apt, as each time you double click a *.txt file, another copy of Notepad is opened. The same thing happens each time I tell Application2 'here is file#'.
Application2 has a menu item to select these files, but I need to make the file selection automatic rather than manual as it is now, which would be the equivalent of telling an already open Notepad to open a specific file.

j0nas
March 22nd, 2004, 04:43 PM
You will have to implement some magic in order get this to work. I'm thinking of a server like app. I mean, your app2 (the script parser) must be able to communicate with the program that starts it. You can do that with IPC (inter-process communication).

I've got two design ideas for you (have never done anything like this myself):

1. Implement a COM automation server into your script parser. Make use of the automation server in the first app (the one that currently starts the parser with CreateProcess).

2. Another interesting design idea is to use/implement it around the IShellExecuteHook interface. If your first app (application1) starts the second app with ShellExecute instead of CreateProcess, you can implement IShellExecuteHook COM object that does the magic talking with the server (application2)... sending commands like open file foo.txt etc.

Sam Hobbs
March 22nd, 2004, 07:23 PM
There are many aspects and possible solutions to this.

First, note that Notepad is a SDI application. There are some MDI applications that are registered such that when a document of it's type is opened, it is opened using an existing instance of the application. I don't know how that works.

If you can modify application2, then application1 could execute application2 only once and you could use IPC to send files (filenames) from application1 to application2. Perhaps WM_COPYDATA would be enough for that.

Another possibility is to look at solutions for limiting an application to a single instance. Some of them probably have ways to send the command line to another instance of the application.