tags:

views:

69

answers:

4

I am wondering if someone could point me in the right direction. You know how for example, in most IDEs, if you open a source file with "open with", it runs the program and opens it up? and then if you open another one, it opens it in a new tab in the same process?

My question is NOT how to add a program to the shell commands, but rather:

  1. How would a C# application "receive" a PDF file for example?

  2. How would the application open the file in the same process when another file is run with it (not having to instances of the program)?

+1  A: 

Your program would have to be able to talk to other instances of itself, and say "hey, I'm already open, what are you trying to do, let me do it for you."

Nate Bross
The relationship is usually the other way: "Hey, is anyone already open? Can you do this for me?"
Ben S
Fair enough :-)
Nate Bross
+5  A: 

When the second program instance starts up, before loading any interface components, it checks to see if another program instance is already running. If so, it communicates to it in some fashion (program specific: this can be sockets, inter process interrupts, shared memory, etc..) that it should open this new file.

After communicating this to the first instance, the second program instance will just terminate since it's no longer needed.

Ben S
Very true. And to complement with the answer to question 1: The file name is included as a parameter to the new instance of your program, as in "C:\WINDOWS\notepad.exe C:\myfile.txt". In your program, read the parameter string to obtain the file name.
Andreas Rejbrand
Thanks for adding that. I had completely missed that part of the question!
Ben S
+1  A: 

Here is a nicely detailed post that explains the proper implementation:

http://www.iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx

This thread contains a discussion and sample for handling the command line arguments (this is how files are "passed to" your application): http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic62109.aspx

Jay
A: 

Microsoft programs usually have a ddeexec key in the shell configuration; this will cause the shell to send a DDE command to the already running app, if it exists.

Mark Ransom
DDE is deprecated.
SLaks
Maybe so, but I notice Office 2007 still uses it. I think the last comment in this blog post sums it up nicely: http://blogs.msdn.com/oldnewthing/archive/2008/07/30/8790272.aspx
Mark Ransom