Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

session ID

14 views
Skip to first unread message

T

unread,
Nov 14, 2022, 7:25:59 PM11/14/22
to
Hi All,

Windows 11 22H2

Is there an (easy) way to find session ID of
your running program?

Many thanks,
-T

Chris M. Thomasson

unread,
Nov 14, 2022, 7:29:28 PM11/14/22
to
On 11/14/2022 4:25 PM, T wrote:
> Hi All,
>
> Windows 11 22H2
>
> Is there an (easy) way to find session ID of
> your running program?

PID?

T

unread,
Nov 14, 2022, 8:24:01 PM11/14/22
to
Supposedly you can find it from your PID. Supposedly.

Chris M. Thomasson

unread,
Nov 14, 2022, 8:40:58 PM11/14/22
to
Are you experienced in Windows?

Mike Terry

unread,
Nov 14, 2022, 8:56:22 PM11/14/22
to
DWORD sessionId = 0;
BOOL ok = ProcessIdToSessionId (GetCurrentProcessId(), &nSessionId);
...

or it's returned by one of the WTS (Terminal Services) query APIs, but I think that requires
terminal services to be running. (It gets much more info, but you only want the session ID so
ProcessIdToSessionId is easy.)

Also, I recall it's in the user's security token, so you might be interested by
GetTokenInformation() - that's also more complicated, but if you need it to work on ancient Windows
versions it might be what you need. :)

For better answers probably best to post on a Windows newsgroup, as it's off topic here. (I just
remembered this from my work before I retired...)

Mike.

T

unread,
Nov 14, 2022, 9:35:22 PM11/14/22
to
If you mean programming for it, not so much. API
all drive me nuts from Raku. I can't read C very
well.


T

unread,
Nov 14, 2022, 9:38:30 PM11/14/22
to
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;
}

T

unread,
Nov 14, 2022, 11:08:42 PM11/14/22
to
If you mean I.T. support, since its inception.
I adored Windows for Workgroups 3.11 and NT 4.0.
Not so much since then. But my customers are 95%
on it, so I hold my nose.

Raku is mostly a Linux thing. I am one of their
beginner programmers. I have programed off
and on for years, but Raku only for about
four years now. I only get to program about
once a month or so. I felt very good about
the first program I got paid for about two
years ago. I field Windows Raku questions,
mostly about installation, every so often from
the Raku mailing list, which should tell you
how much they program for Windows.

I officially support Windows, Mac, and Linux,
but will work on anything, including DOS.
I have one retiree who pays me to fix his
home entertainment system everything he
kicks out a wire or two with the vacuum
cleaner. I make a whole $20 of him
each time.

Mike Terry

unread,
Nov 14, 2022, 11:12:30 PM11/14/22
to
You mean the ProcessIdToSessionId () API? That would work fine when started as system from the
scheduler - but bear in mind that for such a process it's SessioId would probably be zero.

Which kind of leads to the reason you wanted that session id...

But rather than answer that here, I recommend you go to a Windows programming group. (Unless you
have any C++ questions - those would be on topic for this newsgroup.)
That code looks quite typical for Windows - it enumerates the terminal sessions on the computer
looking for the (first) "active" session. On Windows 10 that would generally be the user logged on
via the physical console. (But in general there can be multiple user sessions active I think...)
Then it sends a message box to that windows desktop.

It's the sort of thing a system service might do - a normal user program would just call the
MessageBox API! The Report routine sends messages to an attached debugger, which is not typical -
most services would create system events, or (I prefer) write to a log file.

If you're not familiar with Windows programming, naturally you'll struggle, and moreso if you're not
C/C++ familiar! And if you choose interfaces based on COM it is just going to be a disaster, trust
me, don't go there - there's too much background you'd have to suddenly pick up and become familiar
with... (There's no COM in your sample code above.)


Mike.

T

unread,
Nov 15, 2022, 12:26:11 AM11/15/22
to
On 11/14/22 20:12, Mike Terry wrote:
> I recommend you go to a Windows programming group

You have a recommendation for one?

Bonita Montero

unread,
Nov 15, 2022, 3:36:28 AM11/15/22
to
I developed a little program that prints all sessions and their
processes. To get all processes you need to run it unter Administrator
privileges.

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

using XHANDLE = unique_ptr<void, decltype([]( void *h ) { h && h !=
INVALID_HANDLE_VALUE && CloseHandle( h ); })>;

int main()
{
XHANDLE xhSnapshot( CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) );
if( xhSnapshot.get() == INVALID_HANDLE_VALUE )
return EXIT_FAILURE;
PROCESSENTRY32W pe;
pe.dwSize = sizeof pe;
if( !Process32FirstW( xhSnapshot.get(), &pe ) )
return EXIT_FAILURE;
vector<PROCESSENTRY32W> processes;
do
processes.emplace_back( pe ),
pe.dwSize = sizeof pe;
while( Process32NextW( xhSnapshot.get(), &pe ) );
if( GetLastError() != ERROR_NO_MORE_FILES )
return EXIT_FAILURE;
unordered_multimap<DWORD, PROCESSENTRY32W const *> sessionIdToProcesses;
for( auto const &pe : processes )
{
DWORD dwSessionId;
if( !ProcessIdToSessionId( pe.th32ProcessID, &dwSessionId ) )
continue;
sessionIdToProcesses.emplace( make_pair( dwSessionId, &pe ) );
}
DWORD dwLastSession = -1;
for( auto const &sessionAndProcess : sessionIdToProcesses )
{
if( dwLastSession != sessionAndProcess.first )
dwLastSession = sessionAndProcess.first,
cout << "SID " << dwLastSession << ": " << endl;
auto const &pe = *sessionAndProcess.second;
wcout << L"\t" << pe.th32ProcessID << L": \"" << pe.szExeFile << L"\""
<< endl;
}
}

T

unread,
Nov 15, 2022, 5:41:47 AM11/15/22
to
Would you mind giving me the commands to compile,
load, and run this?


Bonita Montero

unread,
Nov 15, 2022, 8:54:54 AM11/15/22
to
cl -Ox -std:c++20 xxx.cpp
Or just create a Win32 console project with Visual Studio,
copy that source to the main source file and enable C++20
in the project settings for both Debug- and Release-Builds.


0 new messages