How do I enable popups from Task Scheduler

Todd Chester 646 Reputation points
2022-11-12T09:05:02.983+00:00

Hi All,

Windows 11 Pro 22H2

I wrote a program that will pop up various pieces of information (MessageBoxW from user32).

When run from a command shell or an icon on the desk top, the pop ups run fine.

But no pop ups when run from the Task Scheduler.

How do I configure the Task Scheduler for the pop ups?

Many thanks,
-T

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,105 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,021 Reputation points
    2022-11-12T14:55:56.48+00:00

    Following is the sample C++ code for using WTSSendMessage from a task running in non-interactive session 0.

    Although the logged-on user's interactive session id will usually be 1 this is not guaranteed to always be the case. So the sample uses the WTSEnumerateSessions function to obtain the session id for the active session. The session id is one of the parameters passed to WTSSendMessage.

    #define WIN32_LEAN_AND_MEAN  
    #include <Windows.h>  
    #include <WtsApi32.h>  
    #pragma comment(lib, "wtsapi32")  
      
    #include <stdio.h>  
    #include <tchar.h>  
      
    void Report(LPCTSTR pszFormat, ...)  
    {  
        TCHAR szMsg[512];  
        va_list pArg;  
      
        va_start(pArg, pszFormat);  
        _vsntprintf_s(szMsg, ARRAYSIZE(szMsg), ARRAYSIZE(szMsg) - 1, pszFormat, pArg);  
        va_end(pArg);  
      
        OutputDebugString(szMsg);  
    }  
      
      
    int _tmain()  
    {  
        TCHAR szTitle[]{ _T("Information") };  
        TCHAR szMsg[]{ _T("Hello From TaskMessageBox") };  
        DWORD dwResp{};  
      
        PWTS_SESSION_INFO pwsi{};  
        DWORD dwSession{}, dwCount{};  
      
        if (!WTSEnumerateSessions(WTS_CURRENT_SERVER, 0, 1, &pwsi, &dwCount))  
        {  
            Report(_T("WTSEnumerateSessions failed with error code %d\n"), GetLastError());  
            return 0;  
        }  
      
        for (DWORD i = 0; i < dwCount; i++)  
        {  
            if (pwsi[i].State == WTSActive)  
            {  
                dwSession = pwsi[i].SessionId;  
                break;  
            }  
        }  
      
        WTSFreeMemory(pwsi);  
          
        if (!dwSession)  
        {  
            Report(_T("No active sessions\n"));  
            return 0;  
        }  
      
        BOOL fSuccess = WTSSendMessage(WTS_CURRENT_SERVER, dwSession, szTitle, ARRAYSIZE(szTitle) * sizeof(TCHAR),  
        				szMsg, ARRAYSIZE(szMsg) * sizeof(TCHAR), MB_ICONINFORMATION | MB_OK, 60, &dwResp, TRUE);  
      
        if (fSuccess)  
            Report(_T("WTSSendMessage succeeded, response was %d\n"), dwResp);  
        else  
            Report(_T("WTSSendMessage failed, error code was %d\n"), GetLastError());  
      
        return 0;  
    }  
      
    

13 additional answers

Sort by: Most helpful
  1. RLWA32 40,021 Reputation points
    2022-11-12T11:09:41.783+00:00

    In order to display a user interface tasks must be configured to run only when the user is logged on. Otherwise they will run in non-interactive session 0 and any user interface they attempt to display will not be visible.

    0 comments No comments

  2. Todd Chester 646 Reputation points
    2022-11-12T11:13:32.67+00:00

    The program is running as Administrator (no choice). The user is not logged in as Administrator.


  3. Todd Chester 646 Reputation points
    2022-11-12T12:48:41.81+00:00

    I figured out what I am going to do.

    My program write a two line log file with the date and time and the status every time it runs.

    Since the user has to be logged in to see the alerts, I will write a second program (ran as the user when logged in) that picks up that log file and sends pops up accordingly. And run it five minutes after the first program.


  4. Todd Chester 646 Reputation points
    2022-11-12T13:42:54.65+00:00

    C++

    BOOL WTSSendMessageA(
    [in] HANDLE hServer,
    [in] DWORD SessionId,
    [in] LPSTR pTitle,
    [in] DWORD TitleLength,
    [in] LPSTR pMessage,
    [in] DWORD MessageLength,
    [in] DWORD Style,
    [in] DWORD Timeout,
    [out] DWORD *pResponse,
    [in] BOOL bWait
    );

    It is not an easy code from Raku (Perl 6), but I am going to try it anyway.

    Would you mind posing your code?

    0 comments No comments