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,155 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 40,286 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. Todd Chester 646 Reputation points
    2022-11-19T00:38:49.073+00:00

    Hi RLWA,

    I do apologize. I can not longer find anything you write me that shows up on my eMail.

    Your DLL works perfectly -- both from teh command line and from Task Scheduler. I am trying to learn how to do WTSSendMessageA on my own. Self torture.

    Right now, my WTSSendMessageA works from the command line, returning a "1" (True), but from the Task Schedular, it returns a "0" (False).

       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    # C++ BOOL is actually DWORD  
       );  
    

    What are you using from "bWait"? I am sending it a "1"; I am sending "Timeout" a "0"