Hi fellows! I’m codifying some code in one of my programs in order to avoid the users open many instances of it - exept of they confirm will do this -.
so, in Global Embeds, Program Setup, I wrote followings lines
If ~BeginUnique(GLO:ExeName)
If Message(CLIP(GLO:ExeName) & ’ already running.’,‘Warning’,ICON:Question,BUTTON:CANCEL+BUTTON:IGNORE,BUTTON:Cancel) = BUTTON:CANCEL
Return
End
End
It work’s just fine!
but, In case of the exsistance of an other instance allready running and in case of the user press the “cancel” button, instead or before of the Return statement, i wish to automáticaly switch to the existing instance.
The question: is any way to do this?
There’s an ancient ClarionMag article that I was involved with, where I wrote LaunchIDE
The idea was to prevent multi-user corruption, Carl then stepped in and added CreateProcess logic
I’m trying to recall if it switched to the running IDE - when there was one running.
Another thought is to [Post|Send]Message to the other process
then subclass the frame of other process to take focus when it receives the message.
Yes, but first you check if it’s minimized and restore it. I’m on my phone, here’s some C# I found:
public const int SW_RESTORE = 9;
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr handle);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr handle);
I think I did this in the past. By memory I think I found the active program using FindWindow API and then using SetForegroundWindow. But many years passed, maybe I forgot.
I’ve coded this option, but can’t compile & link. I’ve the user32.dll but not the user32.lib, and at link time get error.
I try the LibMaker to generate the .lib but for some reason does not work for the user32.dll
Almost certainly that’s because you have a 64Bit release of Windows and you’re using the wrong copy of USER32.DLL
You need to use the USER32.DLL located in the Windows\SysWOW64 folder - that is the 32Bit version
Just in case you were wanting to do something like this using WINDOWEXISTS(). I do this all the time with my “windowed” programs. Switching to the first program instance is over my head…
PROGRAM
MAP
INCLUDE('CWUTIL.INC'),ONCE
END
INCLUDE('EQUATES.CLW'),ONCE
WIN WINDOW('Caption'),AT(,,191,56),GRAY,FONT('Segoe UI',10)
END
CODE
IF WINDOWEXISTS('Caption')
CASE MESSAGE('Do you want to run a second instance of this program?','Run Twice',ICON:QUESTION,BUTTON:NO+BUTTON:YES,BUTTON:NO,TRUE)
OF BUTTON:NO
HALT
END
END
OPEN(WIN)
ACCEPT
END
CLOSE(WIN)