Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » check if application is running
check if application is running [message #55630] Thu, 26 November 2020 04:49 Go to next message
BetoValle is currently offline  BetoValle
Messages: 202
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 #55633 is a reply to message #55630] Thu, 26 November 2020 10:21 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
Hi,

you can use the package "Uniq" from bazaar.

check examples-bazaar/UniqTest for an example.


regards
omari.
Re: check if application is running [message #55644 is a reply to message #55633] Fri, 27 November 2020 02:55 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi,

thanks but that's not what I'm looking for. See the first need is to search the
an application running on windows, so that it does not load it "again",
and also bring it in front of all the others.

my application, loads another "java" application. In the java application there is already the same content as the one you suggested ie loading a single instance. So my goal is that the C ++ application before loading the java application, evaluate it and if it is loaded then bring it forward.

How would I use a "findWindow" function that I could search among those that are eventually open (as the task manager displays), to evaluate this. I did not identify among the existing functions in U ++ a "findWindow" function but I believe that something like this:

          HWND = findWindow ("title of the java application");
          if HWND not null or exist -> HWND-> bringToFront ();

Re: check if application is running [message #55646 is a reply to message #55644] Fri, 27 November 2020 08:44 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hi BetoValle

You can try Bazaar/SysInfo GetWindowsList() or GetProcessList() functions to see if the application is opened and get a handler to it, and after that calling Window_TopMost() to bring it to front.


Best regards
Iñaki
Re: check if application is running [message #55650 is a reply to message #55646] Fri, 27 November 2020 18:43 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi, Ok && thanks!

        
     int64 n = GetWindowIdFromCaption("title-of-windowApp",true);
     if(n>0){
        Window_TopMost(n);   
     }
	
   
Re: check if application is running [message #55651 is a reply to message #55650] Fri, 27 November 2020 20:07 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member

in time: Window_Top () on windows 10 is not working as it should!

Thanks!
Re: check if application is running [message #55652 is a reply to message #55651] Fri, 27 November 2020 20:58 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Window_Top
it should be in front of the other windows, but it certainly doesn't happen. it´s a bug!
Re: check if application is running [message #55655 is a reply to message #55652] Sat, 28 November 2020 09:31 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
try the winapi:

SetForegroundWindow(n);


regards
omari.
Re: check if application is running [message #55658 is a reply to message #55655] Sat, 28 November 2020 16:35 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi,

to test SetForegroundWindow,
this function is defined in the file winuser.h
and when inserting it #include <winuser.h> does not work
the compiler does not recognize the function.
how do i do this?
Re: check if application is running [message #55665 is a reply to message #55658] Sat, 28 November 2020 22:29 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Neither SetForegroundWindow() nor SetWindowPos() seem to work. I mean, they work for the windows created by a process, but no for the windows created by other processes. It is explained here and here.

Best regards
Iñaki
Re: check if application is running [message #55667 is a reply to message #55665] Sun, 29 November 2020 04:28 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
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 #55669 is a reply to message #55667] Sun, 29 November 2020 14:01 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
you dont need to include userxx.h,
#include <CtrlCore/CtrlCore.h>
is sufficient.
if it does not work,try to scop it as globale:
::SetForegroundWindow(w);


regards
omari.
Re: check if application is running [message #55670 is a reply to message #55669] Sun, 29 November 2020 14:31 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member
Hi
i add
#include <CtrlCore/CtrlCore.h>
::SetForegroundWindow(n);
the compiler show message
error: cannot initialize a parameter of type 'HWND' (aka 'HWND__ *') with an lvalue of type 'Upp::int64' (aka 'long long')


if this refers to what you reported about the global variable, can you tell how it can be done?

thanks
Re: check if application is running [message #55672 is a reply to message #55670] Sun, 29 November 2020 14:51 Go to previous messageGo to next message
omari is currently offline  omari
Messages: 264
Registered: March 2010
Experienced Member
ah, this is a casting error.
you have either :
change the type of 'n' to HWND;

or casting it:
::SetForegroundWindow((HWND)n);


regards
omari.
Re: check if application is running [message #55673 is a reply to message #55672] Sun, 29 November 2020 15:48 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
Registered: September 2020
Location: Brasil Valinhos SP
Experienced Member

(only when I went to publish I saw that we reached the same agreement)

before that, some mistakes and attempts here i made some speculation and i believe i have reached a viable situation although not 100% precisely because the screen of the final application does not come forward,
but right now, on the windows taskbar the application icon is "blinking" (*).

(*)temporary solution:

   n = GetWindowIdFromCaption ("Backup", true); // Backup is the title of the main window
      if (n> 0) {
         ::ShowWindow ((HWND) n, 9);
         ::SetForegroundWindow ((HWND) n);

      }



Thanks
Re: check if application is running [message #55677 is a reply to message #55673] Sun, 29 November 2020 23:32 Go to previous messageGo to next message
BetoValle is currently offline  BetoValle
Messages: 202
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! Very Happy

Re: check if application is running [message #55679 is a reply to message #55667] Mon, 30 November 2020 08:53 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
BetoValle wrote on Sun, 29 November 2020 04:28
I'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
Previous Topic: The Mysterious checkbox
Next Topic: for(;;)
Goto Forum:
  


Current Time: Thu Mar 28 14:23:26 CET 2024

Total time taken to generate the page: 0.01127 seconds