Finding Current/Active Session ID

A

Ahmed Perlom

Hi all,



How can I know if the session ID that a certain process is running on is
actually the active session that he consol is connected to. I can get the
session id of a process by



Process P = Process.GetCurrentProcess();

And then check P.SessionID property.



Anyone has an idea?



Thanks
 
J

Jani Järvinen [MVP]

Hello!
How can I know if the session ID that a certain process is running on is
actually the active session that he consol is connected to.

For starters, I suggest reading this blog post:

http://blogs.msdn.com/oldnewthing/archive/2006/08/22/712677.aspx

The thing is, with Windows XP and *especially* Terminal Services, there is
more than one console. Which console do you want?

And what would you need to do?

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
A

Ahmed Perlom

Hi, thanks for the reply and for the link, that was informative for sure.



Based on what I read, I believe I want to know if a certain process is
running on the session (desktop) that has the keyboard and mouse. I
understand that there might be more than one user session (using the fast
user switch capability on WinXP) but I want to check for the session with
the user currently interacting with the computer.



I found the function "WTSGetActiveConsoleSessionId" from the terminal
services, but it's an unmanaged code, and I was wondering if there is any
..NET managed way to get the same info.



Thanks for the help,
 
J

Jani Järvinen [MVP]

Hello!
I found the function "WTSGetActiveConsoleSessionId" from the terminal
services, but it's an unmanaged code, and I was wondering if there is any
.NET managed way to get the same info.

You *can* call Win32 APIs from managed code! That is why a technology called
Platform Invoke or P/Invoke exists. See this page on MSDN for more info:

http://msdn.microsoft.com/library/d...us/csref/html/vcwlkplatforminvoketutorial.asp

Now, according to www.pinvoke.net (a very useful web site for API-level
developers) you could call WTSGetActiveConsoleSessionId by copying the
following to your C# code:

[DllImport("kernel32.dll")]
static extern int WTSGetActiveConsoleSessionId();

I haven't tried this myself, but it ought to work.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
A

Ahmed Perlom

Thanks for the reply. I tried the PInvoke solution and it works. The
problem it is unmanaged code that need to run in full-trust mode. I was
looking for a managed code solution that would run in a lower privilege.

Cheers


Jani Järvinen said:
Hello!
I found the function "WTSGetActiveConsoleSessionId" from the terminal
services, but it's an unmanaged code, and I was wondering if there is any
.NET managed way to get the same info.

You *can* call Win32 APIs from managed code! That is why a technology
called Platform Invoke or P/Invoke exists. See this page on MSDN for more
info:

http://msdn.microsoft.com/library/d...us/csref/html/vcwlkplatforminvoketutorial.asp

Now, according to www.pinvoke.net (a very useful web site for API-level
developers) you could call WTSGetActiveConsoleSessionId by copying the
following to your C# code:

[DllImport("kernel32.dll")]
static extern int WTSGetActiveConsoleSessionId();

I haven't tried this myself, but it ought to work.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top