Board index » cppbuilder » multiple application instance problem
Stephan Laska
![]() CBuilder Developer |
multiple application instance problem2005-01-21 06:36:08 AM cppbuilder15 Hi all, My MDI app uses a data file. The data file is registered to open an instance of the app when one double clicks on it. I do not want multiple instances of my program running. Code has been implemented to prevent multiple instances (checks for the existance of a named Event) and pass to the running instance the data file to open. I use FindWindow to get the handle to the running app then use SendMessage with the command line as the passed parameter. Everything seems to be running fine, except when no instance of the program exists, and one attepts to open several data files simultaneously (hilight several files, hit enter) What happens is that windows creates an instance and passes one file to it, then opens another and so on until all data files are accounted for. when the app opens, it checks for the existance of the Event object. If it doesn't exist, it creates it and becomes the Primary app and continues to load until everything is finished, then it signals the event. If the event does exist, I use WaitForSingleObject to halt execution until the primary app signals the event. After that, well I haven't been successful in acheiving a solution. I have tried HWND hWnd; hWnd = FindWindow( "TMyForm", 0); but this doesn't seem to be able to pick out the primary application out of the many that were currently open. I've tried capturing the primary app by specifying the window name in the FindWindow call by specifying the default window name as "notme" and changing it to Caption = "FirstInstance" at runtime, as FindWindow("TMyForm", "FirstInstance" ); but this wouldn't find the primary app. I think it's not finding it because the caption changes to show the name of the loaded data file in the primary app. Now, I am trying to use shared memory to hold a handle to the primary app // If hSignal is 0 then the signaling object doesn't exist. if( !hSignal ) { hSignal = CreateEvent( NULL, true, false, "eReaDataSignal" ); FHandle = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, sizeof(HANDLE), "ReaDataInstance1"); if( FHandle != 0 ) { DWORD dwFoom; dwFoom = sizeof( (HANDLE)Application ); FAccess = MapViewOfFile(FHandle, FILE_MAP_WRITE, 0, 0, dwFoom ); if ( FAccess != 0 ) { *FAccess = (HANDLE)Application; // this line doesn't compile } } } Is there an easier way to acheive what I am trying to do? thanks. Stephan |