How to open a file by doubleclicking when program is running

lucroels used Ask the Experts™
I'm new to this list, so please bear with me.
I've been lurking for some time and am really impressed by the level of expertise of the anwers.

The problem:
I can start up the app and show the file doubleclicked by the user in the explorer by using the Paramcount and ParamStr functions.

I prohibit the running of a second instance of the app with the following code (in the program source):
(BTW: Quantum is the name of the app)

  hMutex : THandle;
  runningApp : THandle;
  hMutex := CreateMutex (nil, false, 'Quantum');
  if (GetLastError = ERROR_ALREADY_EXISTS) Or (hMutex = 0) then
      runningApp := FindWindow (nil, 'Quantum');
      if not isWindowVisible (runningApp) then
        PostMessage (runningApp, wm_User, 0, 0);
      SetForegroundWindow (runningApp);
      {standard program init}

In response to the (Post)Message I do a simple 'Application.Restore;'

This works, whenever the user tries to startup another instance or doubleclicks a 'Quantum' file the running app comes to the front.

What eludes me completely is how to open the file that was doubleclicked when my app is running.

I hope someone can point me in the right direction.


luc roels
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You will have to send data to the instance. Have a look at WM_COPYDATA message.
So the second instance copies the file name to a buffer and sends it to the first instance.
In your app you catch thw WM_COPYDATA and act upon the file name sent.

Another way:

1. Add one TEdit named edtExternal and one TButton named btnExternal. Place them on the form. Set their TabStop to false. Then move them over a Panel for example and using popup menu select 'Send to back'. Doing so these two controls will became hidden. In OnClick event of btnExternal add your code for opening a new file which name is in edtExternal.Text. Then add the following in your code above:

  runningApp, extEdt, extBtn : THandle;
  FileName: string;
  FileName := // get it from ParamStr
  runningApp := FindWindow (nil, 'Quantum');
  // set edtExternal.Text = FileName
  extEdt := FindWindowEx(runningApp, 0, nil, 'edtExternal');
  if IsWindow(extEdt) then begin
    // press btnExternal
    extBtn := FindWindowEx(runningApp, 0, nil, 'btnExternal');
    if IsWindow(extBtn) then begin
      PostMessage(extBtn, BM_CLICK, 0, 0);
Regards, Geo
Remove these two ShowMessage calls from the code above, please. I'm sorry.


WM_COPYDATA was definitely the way to go. It took me a while but I got it working. I first thought to rate you a B but then reconsidered because your comment was right up on the mark and by forcing me to go out looking myself for the details I did learn a few things... and I did not want to spoil your perfect track record. You once again thanks very much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial