Search code examples
delphiwinapifile-extensionparamstr

How to detect file open requests from Explorer in Delphi?


My Delphi Alexandria program associates certain file extensions in Registry with the Application's Exename, such that Windows shows the right icon. But I am struggling to get my program to notice a request when I double-click on a file of the correct type in Explorer. What I want is to intercept the call depending on its file extension passed through Paramcount and ParaStr(1) etc. How do I set things up in the .dpr file to

  • a) notice there is a request for a filename
  • b) work out whether the program is already running
  • c) pass the parameters on if so, or
  • d) start the program with the parameter?

d) is already OK but I don't know how to set up a)
Thanks


Solution

  • A) When you register a file extension in the Registry, you have to tell Windows exactly how you want your app to be called when the file extension is executed. The typical scenario is for Windows to simply run a new instance of your app with the full file path passed in as a command line parameter. As you said, you can simply use ParamCount()/ParamStr() to look for and retrieve that parameter when your app starts running. Other choices include sending a message to a DDE server running in your app, or invoking an instance of the IDropTarget, IExecuteCommand, or IExplorerCommand interface that your app implements, etc.

    B) The most common way to detect if an existing instance of your app is already running is to have it create a named kernel object, such as a Win32 Mutex, with a persistent but unique name assigned to it. Create the object during app startup, and Windows will tell you whether the object already exists or not. If it does, then you will have to send the file path to the existing app instance and then exit immediately. Otherwise, continue running normally and process the file path as needed.

    Alternatively, as you seem to already know, you can simply look for a known window in an existing instance of the app. Which you would likely have to do anyway in order to send the file path to it. So, you can "kill 2 birds with 1 stone", so to speak.

    C) There are many Inter-Process Communication mechanisms available for sending data between processes. In this situation, using the WM_COPYDATA window message is a common choice. Other choices include using named pipes, mailslots, sockets, COM, etc. Use whatever method you want.

    D) You don't have to do anything extra for this.