Passing data to another application with WM_COPYDATA This routine will send the parameters to the main window of the existing application instance by means of a WM_COPYDATA message. We will create a routine, SendParamsToPrevInst, to take care of this. When the program detects that another instance is running it must pass its command line parameters (if any) to that program before terminating itself. Passing the command line to a previous instance Now we need find out how to pass any command line data to a previous instance. ![]() That just about wraps up how to start a single instance of the application. SwitchToPrevInst returns true if this attempt succeeds and false if it fails. For now it suffices to know that the function activates the main window of the previous application instance and attempts to pass any command line parameters to it. We will discuss this function later in the article (see Listing 11). ![]() In the 2nd case we attempt to switch to the already running instance by calling the SwitchToPrevInst function. In this case we start our instance as well to ensure that the command line parameters get processed. When there is another instance running but for some reason we can't pass any command line parameters on to it.When there is no other instance of the application running.There are two circumstances under which CanStart returns true: On the other hand if CanStart returns false then the application terminates straight away. If CanStart returns true then the normal application start-up code is executed as normal. We do this by overriding the main form's CreateParams method as follows:Īs can be seen the application start-up code calls CanStart to determine whether this instance of the program should be started as normal. Since it's possible to have two or more different Delphi applications running at the same time, both of which use this window class name we need to give our program's main window a class name that is extremely unlikely to be used by another application. For example, if a form's class is named TForm1 then its window class name is also TForm1. There is a problem with this approach though – by default Delphi uses a form's class name as the name of the associated window class. We opt to check for the window class name regardless of the caption. This routine can check for window captions, (unreliable because they can change while a program is running), the window class name (better), or both. ![]() We use the FindWindow Windows API routine to search for a top level window of the required class. An Initial Approach Finding if your program is running Mutexes can be used to detect muliple application instances and memory mapped files can be used to exchange data. Listing 1 outlines the methodology we will be using in pseudo-code: Once such an application is detected we then pass any command line data via the WM_COPYDATA message. The method we will use to prevent multiple application instances is based on the detection of an application with a known main window class. We also look at how a duplicate instance can pass its command line parameters to the existing instance before terminating. An application does this by checking if an instance is already running and terminating it if so. This article discusses how to ensure that just a single instance of an application can be run. Passing the command line to a previous instance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |