Swiching to other windows program, my EXE already running

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?

I haven’t done this… but here are some ideas
c# - Switch to another running process - Stack Overflow

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.

https://www.capesoft.com/docs/RunOnce/RunOnce.htm

It’s been a while, isn’t it as simple as calling SetForegroundWindow() using the HWND of the window you want to activate?

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);

private void BringToForeground(IntPtr extHandle)
{
    if (IsIconic(extHandle))
    {
        ShowWindow(extHandle, SW_RESTORE);
    }
    SetForegroundWindow(extHandle);
}

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.

In the procedure definitions add something like:

CounterRunning LONG
CounterThread LONG,STATIC

In the window Init, right after CODE add

if ~CounterThread
   CounterThread  = Thread()
   CounterRunning = TRUE
else
   POST(EVENT:GainFocus,,CounterThread)
   ReturnValue = Level:Fatal
   Return ReturnValue
end

Then in the .Kill method after parent:

if CounterRunning
CounterThread = 0
end

Any efforts to start another instance will result in just being switched to the already running instance.
This has worked for me.

That’s works for one insurance of 1 Procedure on 1 Thread. The OP wants the EXE to only be run once, i.e. 1 Process

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)

yes, you’ve the rigt on this. Now I can resume my quest.
thanks!