locked
Execute a task if user is not logged in, but display a window when logged in RRS feed

  • Question

  • Hi,

    I have a question concerning scheduled tasks.  I have a service which has a webpage where the user can click on a button. If the button is clicked, a task should be queued by the service, which updates e.g. drivers or the service itself. If the user is logged on, a window should display the current step and overall progress.

    So far I have tried to called RegisterTaskdefinition with TASK_LOGON_GROUP which displayed the window, but was not working when the user was not logged on. This is not really an option as of course the task shell be also executed when nobody is logged on.

    I also tried TASK_LOGON_SERVICE_ACCOUNT and TASK_LOGON_PASSWORD, but unfortunately that does not display the window when the user is logged on.

    So here comes my question: Is there any option or do I need to set some useroptions so that the task is a) executed when nobody is logged on and b) displays a window, when a user is logged on?

    Thank you for your help in advance.
    Tuesday, October 8, 2013 11:51 AM

Answers

  • I think you can use WindowStation. If the current window station is interactive then the window can be displayed. Normally the name of interactive window station is as follows

    \Sessions\1\Windows\WindowStations\WinSta0 - Window station of Dialog based application

    \Windows\WindowStations\Service-0x0-3e7$  - Window station of a service

    Refer below sample

    #include "stdafx.h"
    #include "windows.h"
    #include h>
    using namespace std;
     
    BOOL __stdcall EnumWindowsStationProc(LPTSTR lpszWindowStation, LPARAM lParam);
    BOOL __stdcall EnumDesktopProc(LPTSTR lpszDesktop, LPARAM lParam);
    BOOL __stdcall EnumWindowsProc(HWND hwnd, LPARAM lParam);
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        EnumWindowStations(EnumWindowsStationProc, 0);
        cout << "Press any key...";
        getch();
        return 0;
    }
     
    BOOL __stdcall EnumWindowsStationProc(LPTSTR lpszWindowStation, LPARAM lParam)
    {
        cout << lpszWindowStation << endl;
        HWINSTA hwinsta = OpenWindowStation(lpszWindowStation, 
            0,WINSTA_ALL_ACCESS);
        if (hwinsta != NULL ) {
            EnumDesktops(hwinsta, EnumDesktopProc, 0);
            CloseWindowStation(hwinsta);
        }
        return true;
    }
     
    BOOL __stdcall EnumDesktopProc(LPTSTR lpszDesktop, LPARAM lParam)
    {
        cout << "   - " << lpszDesktop << endl;
        HDESK hdesk = OpenDesktop(lpszDesktop, 0,0, READ_CONTROL);
        if (hdesk != NULL) {
            EnumDesktopWindows(hdesk, EnumWindowsProc, 0);
            CloseDesktop(hdesk);
        }
        return true;
    }
     
    BOOL __stdcall EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
        char wintext[250];
        int buflen = GetWindowText(hwnd, wintext, 250);
        if (buflen > 0 && buflen < 250) {
            cout << "        - " << wintext << endl;
        }
        return true;
    }

     

    Thanks, Renjith V R

    Friday, October 25, 2013 1:05 PM

All replies

  • What if more than one user is logged on? Do you want a window to appear for each user?

    Tuesday, October 8, 2013 7:26 PM
  • I think you can use WindowStation. If the current window station is interactive then the window can be displayed. Normally the name of interactive window station is as follows

    \Sessions\1\Windows\WindowStations\WinSta0 - Window station of Dialog based application

    \Windows\WindowStations\Service-0x0-3e7$  - Window station of a service

    Refer below sample

    #include "stdafx.h"
    #include "windows.h"
    #include h>
    using namespace std;
     
    BOOL __stdcall EnumWindowsStationProc(LPTSTR lpszWindowStation, LPARAM lParam);
    BOOL __stdcall EnumDesktopProc(LPTSTR lpszDesktop, LPARAM lParam);
    BOOL __stdcall EnumWindowsProc(HWND hwnd, LPARAM lParam);
     
    int _tmain(int argc, _TCHAR* argv[])
    {
        EnumWindowStations(EnumWindowsStationProc, 0);
        cout << "Press any key...";
        getch();
        return 0;
    }
     
    BOOL __stdcall EnumWindowsStationProc(LPTSTR lpszWindowStation, LPARAM lParam)
    {
        cout << lpszWindowStation << endl;
        HWINSTA hwinsta = OpenWindowStation(lpszWindowStation, 
            0,WINSTA_ALL_ACCESS);
        if (hwinsta != NULL ) {
            EnumDesktops(hwinsta, EnumDesktopProc, 0);
            CloseWindowStation(hwinsta);
        }
        return true;
    }
     
    BOOL __stdcall EnumDesktopProc(LPTSTR lpszDesktop, LPARAM lParam)
    {
        cout << "   - " << lpszDesktop << endl;
        HDESK hdesk = OpenDesktop(lpszDesktop, 0,0, READ_CONTROL);
        if (hdesk != NULL) {
            EnumDesktopWindows(hdesk, EnumWindowsProc, 0);
            CloseDesktop(hdesk);
        }
        return true;
    }
     
    BOOL __stdcall EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
        char wintext[250];
        int buflen = GetWindowText(hwnd, wintext, 250);
        if (buflen > 0 && buflen < 250) {
            cout << "        - " << wintext << endl;
        }
        return true;
    }

     

    Thanks, Renjith V R

    Friday, October 25, 2013 1:05 PM