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-14T20:53:37.793+00:00

    Hi RLWA,

    First off, I do have to apologize. I find this forum to be extremely awkward to use. I have an extremely difficult time find things you have previously written me. I constantly have to go back to my eMail and find your stuff there. Not to mention constantly having to clear my cache, So if it seems like I am ignoring what you say, it is because I can't find it. Also, I do not now C, so I have to guess a lot.

    There is a problem with WTSEnumerateSessions.
    https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsenumeratesessionsa

    Raku runs my program i its run line: raku WindowsRaidCheclk.p6 display. WTSEnumerateSessions returns a list of everyone. Now it is extremely easy to read through a list with Raku, but the name of the program shows as "Raku.exe" not "my program*.pl6". So, which one of the various open Raku's is the one I am currently in.

    From your previous post, which I can only see in my eMail, you ran WTSSendMessage from TaskSchedular as administrator and got it to work. I am trying to duplicate that. (You never answered me if you were using W11 or server 2008 by the way or I, again, could not find it.) You call WTSSendMessage with

    BOOL fSuccess = WTSSendMessage(WTS_CURRENT_SERVER, dwSession, szTitle, ARRAYSIZE(szTitle) * sizeof(TCHAR),
    szMsg, ARRAYSIZE(szMsg) * sizeof(TCHAR), MB_ICONIN

    The variable I am after is "dwSession". As far as I can tell, you are deriving its value from

    for (DWORD i = 0; i < dwCount; i++)
    {
    if (pwsi[i].State == WTSActive)
    {
    dwSession = pwsi[i].SessionId;
    break;
    }
    }

    Again, I do not understand C, so I have no idea what WTSActive is as I do not see it defined. I am "presuming" it means that the program is "active". Where is it defined?

    I also do not see pwsi defined, so I have no idea what that is either. It looks like an array of session ID's to me. But where did you populate it? Where is it defined?

    And how are you telling one running program with the same name apart from another program of the same name (multiple raku's, etc.)?
    (Have you seen how many Chrome's are running in the backgroud?)

    Speaking of C and Raku, keep in mind that a variable in C is a predefined place in memory. In Raku, it is a structure surrounding a value. I have to know exactly what the structure is in C to emulate it in Raku. See

    https://docs.perl6.org/language/nativecall

    It is a nightmare.

    And this forum's structure/layout does not help, 1600 character limit and all. Plus having to unhide the comments, which I have to clear cache and refresh to even get to open.

    -T


  2. RLWA32 40,286 Reputation points
    2022-11-15T00:38:49.683+00:00

    I uploaded a zip file to onedrive. It contains a DLL, a C header file and an import library for the DLL. The function signature is very similar to WTSSendMessage except you don't have to provide the server handle, the session id or the length in bytes of the strings for the message and the title. You can download the file from https://1drv.ms/u/s!AmnqrCFBv4nDglN8H7MvrppwzgvU?e=l6Wsvf

    Don't forget to unblock the zip file after you download it and before you extract the contents.

    I'm providing this to you as a sample, a demo without any warranties. It is therefore not extensively tested and you assume all responsibility for using it in your own projects. It is not intended to be redistributed to any other users.


  3. Todd Chester 646 Reputation points
    2022-11-15T09:27:39.41+00:00

    Would you also write another copy that added the program ID and Session number to the pop up so I can see what is happening?


  4. Todd Chester 646 Reputation points
    2022-11-17T00:02:05.36+00:00

    This time it worked! Thank you!