views:

82

answers:

3

Alright so what I am trying to do is essentially create a program that, when it is already executed, can be "executed" again, detect the already existing process and instead of creating another process, execute a function in the existing process.

Any ideas?

It's possible I am going at this all wrong. I am trying to adapt this open-source Virtual Printer (http://sourceforge.net/projects/imageprinter/) to an application of mine. The printer prints for example a PDF to a file and it seems also has a feature to launch an application. I need to grab the PDF into my own application for handling at this point and the user needs to be able to append more pdfs by printing them to this printer.

The way I was planning to handle it is to have the application check that folder for new files everytime it opens and a command is called when a new file is printed. Is there another way to import the pdf data more directly to a running process?

+4  A: 

1: The typical solution is to use a named mutex for this. See this question asked on MSDN for more details.

2: Use one of these functions (maybe FindWindowEx?) and (if needed) some way to get the information for locating the HWND between the two processes. Sorry I can't be more clear, there are way too many ways to do this and it varies from application to application.

3: After you know the other application is running, you can use SendMessage with your own UINT message (make sure it doesn't clash with an existing message) and make sure to handle the special message in your message loop.

Travis Gockel
that answers preventing another process, but is it possible to then send a command to the already running process?
hmind
@hmind: that's stage two, you'll need to use an interprocess communication mechanism to let the 1st process know what should be done. Use SendMessage, WM_COPYDATA if you've got a window handle, a named pipe if you don't. Those are the obvious choices, there are others.
Hans Passant
Sorry: I've revised my answer for clarity and whatnot.
Travis Gockel
+1  A: 

Sounds like you might want to start googling around for information about the Running Object Table... Here is sample with and MFC Client calling a .net Application that registers itself with the Running object table so they can perform inter process communication.

Running Object Table: Provider in .NET, consumer in MFC

What exactly are you trying to do here, I am sure you have a good reason for what you are doing but there might be other approaches that get the same result in a simpler way.

Steve Sheldon
A: 

There are two stages to the answer:

I am asumming a Win32 API program, rather than MFC/.net

  1. Named mutexs, as pointed out.
  2. WM_USER messages. In your message loop, set up a case to handle messages you defined. This message will cause the execution of your target function.

Basically, look up inter process communication in the win32 api.

Daniel Goldberg