locked
How to enumeration foreground app in the task manager RRS feed

  • Question

  • How can I use windows API to enumerate foreground application in the task manager?

    For example,if i open Edge & task manager.When I run my program, it will list Edge & task manager, not include background process .

    Thursday, October 24, 2019 8:59 AM

All replies

  • How can I use windows API to enumerate foreground application in the task manager?

    For example,if i open Edge & task manager.When I run my program, it will list Edge & task manager, not include background process .

    How does Task Manager categorize processes as App, Background Process, or Windows Process?
    Thursday, October 24, 2019 9:34 AM
  • The Task Manager calls EnumDesktopWindows

    to enumerate main windows and filters them on style (like WS_VISIBLE), extended style, not owned (GW_OWNER)...


    • Edited by Castorix31 Thursday, October 24, 2019 9:40 AM
    Thursday, October 24, 2019 9:37 AM
  • Thursday, October 24, 2019 10:19 AM
  • In fact.I want to use windows service to enumerate all Application. Because service run at session id 0,but my application run at another session. So i can't use API EnumWindows directly.I get some information from google,and get the suggest to use API CreateProcessAsUser. This Api almost used to call another .exe . Does anyone know how to get application windows handle by windows service.

    thank you!

    Wednesday, November 27, 2019 7:49 AM
  • In fact.I want to use windows service to enumerate all Application. Because service run at session id 0,but my application run at another session. So i can't use API EnumWindows directly.I get some information from google,and get the suggest to use API CreateProcessAsUser. This Api almost used to call another .exe . Does anyone know how to get application windows handle by windows service.

    thank you!

    This is an information that you did not tell in your original question.

    EnumWindows will enumerate the windows of the current session. But you want to enumerate the windows of all active user sessions from session 0.

    Read this:

    https://stackoverflow.com/questions/308135/how-can-i-enumerate-the-open-windows-enumwindows-of-another-user-session

    Accordingly you must CreateProcessAsUser to start your own exe in another user session. This exe calls EnumWindows(). You send the result to your current session 0 process (e.g. in a database) and then you have all open windows.

    Regards, Guido




    Wednesday, November 27, 2019 8:01 AM
  • Based on some specifications, I cannot use CreateProcessAsUser  to call .exe on the service.From the link you gave, I tried using WTSEnumerateSessions, EnumWindowStations, OpenWindowStation, EnumDesktops.I still can't get the window handle which i wanted from user sesion.Is my program written wrong or is this not feasible?
    Tuesday, December 10, 2019 6:18 AM
  • Based on some specifications, I cannot use CreateProcessAsUser  to call .exe on the service.From the link you gave, I tried using WTSEnumerateSessions, EnumWindowStations, OpenWindowStation, EnumDesktops.I still can't get the window handle which i wanted from user sesion.Is my program written wrong or is this not feasible?

    Then the code has errors or there is some other issue.

    Is the windows service running as LocalSystem?

    Post the code you are using.


    • Edited by RLWA32 Tuesday, December 10, 2019 10:12 AM
    Tuesday, December 10, 2019 10:12 AM
  • My service is running at LocalSystem(session ID 0)

    I want to get the windows handle of the application notepad ++.exe(session ID 1)

    This is my code.

    void enumWindowsHandle()
    {
        PWTS_SESSION_INFO psi;
        DWORD dwCount;
        BOOL bRet = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &psi, &dwCount);
        EnumWindowStations(EnumWindowStationProc, NULL);
    }
    BOOL CALLBACK EnumWindowStationProc(LPTSTR lpszWindowStation, LPARAM lParam)
    {    
        HWINSTA  handle = OpenWindowStation(lpszWindowStation, FALSE, NULL);
        EnumDesktops(handle, EnumDesktopProc, NULL);

        CloseWindowStation(handle);
        return true;
    }

    BOOL CALLBACK EnumDesktopProc(LPTSTR lpszDesktop, LPARAM lParam)
    {
        HDESK hDesk = OpenDesktop(lpszDesktop, 0, FALSE, MAXIMUM_ALLOWED);
        BOOL bSuccess = SetThreadDesktop(hDesk);
        EnumWindows((WNDENUMPROC)enumWindowCallback, 0);

        CloseDesktop(hDesk);
        return true;
    }

    BOOL enumWindowCallback(HWND hWnd, LPARAM lparam)
    {
        if (hWnd)
        {
            if (GetWindowTextLength(hWnd) != 0)
            {
                WCHAR title[MAX_PATH];
                GetWindowText(hWnd, title, MAX_PATH);
                WriteEventLog(L"Window Text : %s\n", title);            
            }
        }
        return TRUE;
    }

    I expected to get the windows handle then i can use api GetWindowText to get string ""notepad ++. exe"

    Is there any problem with my program?


            
    Wednesday, December 11, 2019 2:42 AM
  • You must start a process in session 1 that runs under the account of the user that is logged on to that session.  That is done with CreateProcessAsUser.

    That process, running in session 1, can find the window handle you are looking for.

    The windows service running in session 0 cannot accomplish this directly.  It only has direct access to window stations, desktops and windows that reside in session 0.

    You said earlier that you were unable to use CreateProcessAsUser.  Why is that?  Be specific.

    I suggest that the first thing you work on is starting a process in the interactive user's session from the service.  Once you get that working you can write the code to find window handles.

    Wednesday, December 11, 2019 3:59 AM
  • See the answer at https://stackoverflow.com/questions/53177524/a-process-created-with-createprocessasuserw-from-a-64-bit-process-exits-immediat for an example of how to use CreateProcessAsUser from a windows service to launch an application in the interactive user's session.

    Wednesday, December 11, 2019 3:55 PM
  • Is there an API to let the service know that the application in the user's session is suspended or running?

    Thursday, December 12, 2019 7:43 AM
  • Is there an API to let the service know that the application in the user's session is suspended or running?

    When you CreateProcessAsUser, you get the process information lpProcessInformation

    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-process_information

    With hProcess in the structure, you can check if the process is running. For example use GetExitCodeProcess, it will return STILL_ACTIVE if it is still running. https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess

    If you want to know if it's suspended, use hThread in the process information structure and check with this: https://stackoverflow.com/questions/22949725/how-to-get-thread-state-e-g-suspended-memory-cpu-usage-start-time-priori

    Regards, Guido





    • Edited by Guido Franzke Thursday, December 12, 2019 8:13 AM added suspended
    Thursday, December 12, 2019 8:09 AM
  • Due to the company's internal regulations, service.exe can only be entrained when the driver is released. This is why my service program cannot use the API CreateProcessAsUser to generate a process to enumerate application window whick running in the user's session.
    Thursday, December 12, 2019 9:03 AM
  • Due to the company's internal regulations, service.exe can only be entrained when the driver is released. This is why my service program cannot use the API CreateProcessAsUser to generate a process to enumerate application window whick running in the user's session.

    Then it is not possible to enumerate application windows in another user session.

    I could only think of a background app that is running at autostart in all user sessions when the user logs in. The app would be the same as what you want to call with CreateProcessAsUser. But you don't have full access to the app because the user himself can delete the app from autostart.

    Regards, Guido

    Thursday, December 12, 2019 9:31 AM
  • OK,It seems that if I don't use CreateProcessAsUser to create a process which run in the user 'session. I can't get the information which i want.
    Thursday, December 12, 2019 9:56 AM
  • OK,It seems that if I don't use CreateProcessAsUser to create a process which run in the user 'session. I can't get the information which i want.

    There must be an app running in each user session to enumerate windows. You cannot enumerate windows from session 0 to get windows in other sessions. You must be in that sessions.

    Start the app with CreateProcessAsUser, or if you cannot use this, you can only start an app via Autostart when the user logins in.

    Regards, Guido

    Thursday, December 12, 2019 10:08 AM
  • Due to the company's internal regulations, service.exe can only be entrained when the driver is released. This is why my service program cannot use the API CreateProcessAsUser to generate a process to enumerate application window whick running in the user's session.

    I don't understand.

    What driver?  Nothing was said about a driver in any prior post.  What does this have to do with your windows service?

    I assume that the mention of service.exe is a reference to Windows service controller.  Your service would reside in a different executable.  So this reference confuses me.

    Assuming that your service is running, what would prevent it from using CreateProcessAsUser?  Is there a specific company policy that forbids the use of this function?

    Thursday, December 12, 2019 12:03 PM