Detect Remote RDP Session??

T

Tim

Hi Guys,

I was wondering if there is a way to detect from with a C# app if it is
being started in a remote session? i.e. someone has connected via remote
desktop or terminal services...

Thanks

Tim
 
S

Siva M

I have not tried, but check it: P/Invoke call on GetSystemMetrics(0x1000).
Should return non-zero under Term. Services. 0, otherwise.

Hi Guys,

I was wondering if there is a way to detect from with a C# app if it is
being started in a remote session? i.e. someone has connected via remote
desktop or terminal services...

Thanks

Tim
 
W

Willy Denoyette [MVP]

| Hi Guys,
|
| I was wondering if there is a way to detect from with a C# app if it is
| being started in a remote session? i.e. someone has connected via remote
| desktop or terminal services...
|
| Thanks
|
| Tim
|
|

Check the Process.SessionId property, if it's non zero it means you are
running in a TS session.

Willy.
 
T

Tim

Thanks for the reply Siva, I don't suppose you could give me some more
information. I am not sure how to proceed. I would appreciate some actual
code if you can.

Thanks

Tim
 
W

Willy Denoyette [MVP]

That's right, on v1.1 you'll have to PInvoke ProcessIdToSessionId, something
like this will do:


[DllImport("Kernel32")
extern static int ProcessIdToSessionId(int procId, out int sessId);

int procId = Process.getCurrentProcess().Id;
int sessId = 0;

if(ProcessIdToSessionId(procId, out sessId) != 0)
{
if (sessId == 0)
// no TS
else
// in TS
}
else
// API call failed

Willy.



|I think this only works for .Net 2.0. I am using 1.1 still...
|
| | >
| > | > | Hi Guys,
| > |
| > | I was wondering if there is a way to detect from with a C# app if it
is
| > | being started in a remote session? i.e. someone has connected via
remote
| > | desktop or terminal services...
| > |
| > | Thanks
| > |
| > | Tim
| > |
| > |
| >
| > Check the Process.SessionId property, if it's non zero it means you are
| > running in a TS session.
| >
| > Willy.
| >
| >
|
|
 
T

Tim

Hi Wily,

I tried this

[DllImport("kernel32.dll")]
static extern bool ProcessIdToSessionId(uint dwProcessId, out uint
pSessionId);
uint procId = System.Convert.ToUInt32(Process.GetCurrentProcess().Id);
uint sessId = 0;
if(ProcessIdToSessionId(procId, out sessId))
{
if (sessId == 0)
{
// no TS
}
else
{
// in TS
}
}

but it is 0 for remote logins... Any other ideas??

Tim
 
T

Tim

I think I may have found the problem; the 'server' I am using is a WinXP Pro
machine... does this mean it will not work? MSDN documentation;
http://msdn.microsoft.com/library/d...us/termserv/termserv/processidtosessionid.asp

Tim

Tim said:
Hi Wily,

I tried this

[DllImport("kernel32.dll")]
static extern bool ProcessIdToSessionId(uint dwProcessId, out uint
pSessionId);
uint procId = System.Convert.ToUInt32(Process.GetCurrentProcess().Id);
uint sessId = 0;
if(ProcessIdToSessionId(procId, out sessId))
{
if (sessId == 0)
{
// no TS
}
else
{
// in TS
}
}

but it is 0 for remote logins... Any other ideas??

Tim

Willy Denoyette said:
That's right, on v1.1 you'll have to PInvoke ProcessIdToSessionId,
something
like this will do:


[DllImport("Kernel32")
extern static int ProcessIdToSessionId(int procId, out int sessId);

int procId = Process.getCurrentProcess().Id;
int sessId = 0;

if(ProcessIdToSessionId(procId, out sessId) != 0)
{
if (sessId == 0)
// no TS
else
// in TS
}
else
// API call failed

Willy.



|I think this only works for .Net 2.0. I am using 1.1 still...
|
| | >
| > | > | Hi Guys,
| > |
| > | I was wondering if there is a way to detect from with a C# app if
it
is
| > | being started in a remote session? i.e. someone has connected via
remote
| > | desktop or terminal services...
| > |
| > | Thanks
| > |
| > | Tim
| > |
| > |
| >
| > Check the Process.SessionId property, if it's non zero it means you
are
| > running in a TS session.
| >
| > Willy.
| >
| >
|
|
 
W

Willy Denoyette [MVP]

Windows client OS's don't run real TS sessions, they use TS technology to
present a remote desktop session (single session only), guess that's the
reason why 0 is returned.

Willy.

|I think I may have found the problem; the 'server' I am using is a WinXP
Pro
| machine... does this mean it will not work? MSDN documentation;
|
http://msdn.microsoft.com/library/d...us/termserv/termserv/processidtosessionid.asp
|
| Tim
|
| | > Hi Wily,
| >
| > I tried this
| >
| > [DllImport("kernel32.dll")]
| > static extern bool ProcessIdToSessionId(uint dwProcessId, out uint
| > pSessionId);
| > uint procId = System.Convert.ToUInt32(Process.GetCurrentProcess().Id);
| > uint sessId = 0;
| > if(ProcessIdToSessionId(procId, out sessId))
| > {
| > if (sessId == 0)
| > {
| > // no TS
| > }
| > else
| > {
| > // in TS
| > }
| > }
| >
| > but it is 0 for remote logins... Any other ideas??
| >
| > Tim
| >
| > | >> That's right, on v1.1 you'll have to PInvoke ProcessIdToSessionId,
| >> something
| >> like this will do:
| >>
| >>
| >> [DllImport("Kernel32")
| >> extern static int ProcessIdToSessionId(int procId, out int sessId);
| >>
| >> int procId = Process.getCurrentProcess().Id;
| >> int sessId = 0;
| >>
| >> if(ProcessIdToSessionId(procId, out sessId) != 0)
| >> {
| >> if (sessId == 0)
| >> // no TS
| >> else
| >> // in TS
| >> }
| >> else
| >> // API call failed
| >>
| >> Willy.
| >>
| >>
| >>
| >> | >> |I think this only works for .Net 2.0. I am using 1.1 still...
| >> |
| >> | | >> | >
| >> | > | >> | > | Hi Guys,
| >> | > |
| >> | > | I was wondering if there is a way to detect from with a C# app if
| >> it
| >> is
| >> | > | being started in a remote session? i.e. someone has connected via
| >> remote
| >> | > | desktop or terminal services...
| >> | > |
| >> | > | Thanks
| >> | > |
| >> | > | Tim
| >> | > |
| >> | > |
| >> | >
| >> | > Check the Process.SessionId property, if it's non zero it means you
| >> are
| >> | > running in a TS session.
| >> | >
| >> | > Willy.
| >> | >
| >> | >
| >> |
| >> |
| >>
| >>
| >
| >
|
|
 
T

Tim

Hi Willy (sorry for mis-spelling your name before),

Is there any way to tell if it is a remote desktop session that is running
on an XP machine or something that is not a server with a proper terminal
services session?

Tim


Willy Denoyette said:
Windows client OS's don't run real TS sessions, they use TS technology to
present a remote desktop session (single session only), guess that's the
reason why 0 is returned.

Willy.

|I think I may have found the problem; the 'server' I am using is a WinXP
Pro
| machine... does this mean it will not work? MSDN documentation;
|
http://msdn.microsoft.com/library/d...us/termserv/termserv/processidtosessionid.asp
|
| Tim
|
| | > Hi Wily,
| >
| > I tried this
| >
| > [DllImport("kernel32.dll")]
| > static extern bool ProcessIdToSessionId(uint dwProcessId, out uint
| > pSessionId);
| > uint procId = System.Convert.ToUInt32(Process.GetCurrentProcess().Id);
| > uint sessId = 0;
| > if(ProcessIdToSessionId(procId, out sessId))
| > {
| > if (sessId == 0)
| > {
| > // no TS
| > }
| > else
| > {
| > // in TS
| > }
| > }
| >
| > but it is 0 for remote logins... Any other ideas??
| >
| > Tim
| >
| > | >> That's right, on v1.1 you'll have to PInvoke ProcessIdToSessionId,
| >> something
| >> like this will do:
| >>
| >>
| >> [DllImport("Kernel32")
| >> extern static int ProcessIdToSessionId(int procId, out int sessId);
| >>
| >> int procId = Process.getCurrentProcess().Id;
| >> int sessId = 0;
| >>
| >> if(ProcessIdToSessionId(procId, out sessId) != 0)
| >> {
| >> if (sessId == 0)
| >> // no TS
| >> else
| >> // in TS
| >> }
| >> else
| >> // API call failed
| >>
| >> Willy.
| >>
| >>
| >>
| >> | >> |I think this only works for .Net 2.0. I am using 1.1 still...
| >> |
message
| >> | | >> | >
| >> | > | >> | > | Hi Guys,
| >> | > |
| >> | > | I was wondering if there is a way to detect from with a C# app
if
| >> it
| >> is
| >> | > | being started in a remote session? i.e. someone has connected
via
| >> remote
| >> | > | desktop or terminal services...
| >> | > |
| >> | > | Thanks
| >> | > |
| >> | > | Tim
| >> | > |
| >> | > |
| >> | >
| >> | > Check the Process.SessionId property, if it's non zero it means
you
| >> are
| >> | > running in a TS session.
| >> | >
| >> | > Willy.
| >> | >
| >> | >
| >> |
| >> |
| >>
| >>
| >
| >
|
|
 

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