Making Multiple Instances of an Application run in the Same Process Space


This article was contributed by V. Rama Krishna.

If you are familiar with Internet Explorer 4.0 you would find that in the Advanced Internet Settings there is an option "Browse in a New Process". If you uncheck this check box and run IExplorer.exe and then start task manager (or Process Viewer) you would find that there is no IExplorer in the processes yet the Internet Explorer appears. Checking "Browse in a new process" and starting Internet Explorer runs it as IExplorer.exe again.

In the first case instead of a separate process being created for Internet Explorer a thread is created in explorer.exe and instances of Internet Explorer run in different threads within the process space of explorer.exe. Recently, I also found the need to do the same in my application. This article demonstrates how this could be achieved.

This is partcularly useful when lots of data need to be shared among instances of applications. Typically the first instance of the application loads data from the disk file or initializes through other methods and this data needs to be more or less constant throughout the application. Memory mapped file is obviously an alternative but it requires quite a bit of pointer arithmetic and to a lot of extent convenience is lost.This alternative is convinient but has certain risks associated like if a single instance of the application crashes then all the instances are gone.

Implementation

Coming to the implementation, it's quite simple. Any instance of the application launched either through explorer or through API calls such as CreateProcess, first detects if a window of a particular class exists, if it doesnot it creates one. If the window does exists the newly created process simple sends a message to the window and transfers the control to the other running instance. The already running instance of the application launches a separate thread that encapsulates an instance of an application like creating the window etc.



[Any source code goes here]



Download demo project - [size in KB] KB

Download source - [size in KB] KB

Date Posted: August 11, 1998