| 
 | 
 | 
 
Home » Community » Newbie corner » check if application is running 
	
		
		
			| check if application is running [message #55630] | 
			Thu, 26 November 2020 04:49   | 
		 
		
			
				
				
				
					
						  
						BetoValle
						 Messages: 206 Registered: September 2020  Location: Brasil Valinhos SP
						
					 | 
					Experienced Member  | 
					 | 
		 
		 
	 | 
 
	
		Hi, 
how to do this: 
check if the application is running and if positive then bring to front and exit the current application. 
 
Thanks
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	
		
		
			| Re: check if application is running [message #55667 is a reply to message #55665] | 
			Sun, 29 November 2020 04:28    | 
		 
		
			
				
				
				
					
						  
						BetoValle
						 Messages: 206 Registered: September 2020  Location: Brasil Valinhos SP
						
					 | 
					Experienced Member  | 
					 | 
		 
		 
	 | 
 
	
		 
Hi, 
 
I'm sorry to disagree and I'm going to demonstrate that it works using SetForegroundWindow. 
In the case of the question of this open topic I will state the situation: 
 
I have a compiled application (in U ++) that is only used to load another application in java (jar file), since java does not generate an executable. 
 
So I have App U ++: 
 
pkBack :: pkBack ()
{
  / * int64 n = GetWindowIdFromCaption ("Backup", true); // Backup is the title of the main window
     if (n> 0) {
         Window_Top (n);
     } else {* /
    CtrlLayout (* this, "Window title");
Icon (PkBackImg :: icon ());
String dir = AppendFileName (GetCurrentDirectory (), "");
String jre = AppendFileName (GetCurrentDirectory (), "\\ jre \\ bin \\ java.exe");
const String fjar = "BackJPW.jar";
bool c = FileExists (jre);
if (c) {
} else
{
PromptOK ("jre is not present!" + Jre);
}
c = FileExists (fjar);
if (c) {
Upp :: LaunchMy (jre, "- jar" + fjar, dir, 0);
} else
{
PromptOK ("jar is not present!" + Fjar);
}
//}
   
    Upp :: Exit ();
   
}
GUI_APP_MAIN
{
pkBack (). Run ();
}
 
 
what is commented above is because it doesn't work !!! 
 
The above application loaded in java, checks if it is unique 
and if it is already loaded, trigger an action so that the 
window is in front of the others (This is exactly the objective 
with U ++ ie to evaluate before calling the java application) 
 
below the java code that works perfectly 
and involves exactly calling the function 
SetForegroundWindow (java libraries User32 
WinDef ): 
 
 private static boolean focusApp () {
        // Set focus to App
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow (null, "Backup"); // window title
        boolean ret = false;
        if (hwnd == null) {
        } else {
            User32.INSTANCE.ShowWindow (hwnd, 9); // SW_RESTORE
            User32.INSTANCE.SetForegroundWindow (hwnd); // bring to front
            ret = true;
        }
        return ret;
    }
    public static void main (String [] args) throws ClassNotFoundException, AlreadyLockedException {
        String appId = "appBackid";
        boolean alreadyRunning;
        try {
            JUnique.acquireLock (appId);
            alreadyRunning = false;
        } catch (AlreadyLockedException e) {
            alreadyRunning = true;
        }
        if (! alreadyRunning) {
            fBack form_ = new fBack ();
            form_.setVisible (true);
            form_.setLocationRelativeTo (null); // centralize form
        } else {
            focusApp ();
        }
    } 
 
 
so my intention is to transfer the code from java to U ++, precisely because the initial load is done in U ++ !! 
 
as I'm a newbie, I still don't have the ability to write the code and transport it to U ++. It is very similar to what was done to produce Window_Top, but I don't know how to do it. 
In U ++ I know they are defined in winuser.h / winapifamily.h but in separate groups. 
 
thankful 
 
 
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	| 
		
 |  
	
		
		
			| Re: check if application is running [message #55677 is a reply to message #55673] | 
			Sun, 29 November 2020 23:32    | 
		 
		
			
				
				
				
					
						  
						BetoValle
						 Messages: 206 Registered: September 2020  Location: Brasil Valinhos SP
						
					 | 
					Experienced Member  | 
					 | 
		 
		 
	 | 
 
	
		 
Hi,  
 
Wow && Wow!! 
after much, much code evaluation on the internet the "only one that worked 100% was this one below (identified in 
https://bugreports.qt.io/browse/QTBUG-37435)! 
(believe me: the others did not satisfy !!!) 
 
 
	HWND w = ::FindWindowA(NULL,"Backup");
	if(w!=NULL){
                
                HWND hCurWnd = ::GetForegroundWindow();
		DWORD dwMyID = ::GetCurrentThreadId();
		DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
		::AttachThreadInput(dwCurID, dwMyID, TRUE);
		::SetWindowPos(w, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
		::SetWindowPos(w, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
		::SetForegroundWindow(w);
		::AttachThreadInput(dwCurID, dwMyID, FALSE);
		::SetFocus(w);
		::SetActiveWindow(w);
        } 
 
 
Thanks!      
 
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	
		
		
			| Re: check if application is running [message #55679 is a reply to message #55667] | 
			Mon, 30 November 2020 08:53   | 
		 
		
			
				
				
				  | 
					
						  
						koldo
						 Messages: 3453 Registered: August 2008 
						
					 | 
					Senior Veteran  | 
					 | 
		 
		 
	 | 
 
	
		BetoValle wrote on Sun, 29 November 2020 04:28I'm sorry to disagree and I'm going to demonstrate that it works using SetForegroundWindow. You are right. The trick was adding "::" at the beginning. This line does it all works for me: 
::SetWindowPos(w, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW)  
Look at the "SWP_SHOWWINDOW" at the end. 
Fixed in SysInfo.cpp.
		
		
  Best regards 
Iñaki
		
 |  
	| 
		
	 | 
 
 
 |   
Goto Forum:
 
 Current Time: Tue Nov 04 14:19:53 CET 2025 
 Total time taken to generate the page: 0.12154 seconds 
 |   
 |  
  |