That call is only effective if you are not running
as administrator from the Task Scheduler. It would
be easy to port to Raku.
This is the sample code I was given, that I
thoroughly do not understand.
Hmmmm.. Did not realize I even had to free memory
or risk a memory leak.
I just want my stinking session ID. I have everything
else coded and working (from Raku).
#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;
}