ID of the current session

Post Reply
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

ID of the current session

Post by Natter »

Hi,

I connect to the server via RDP. Is it possible to get the ID of the current session ?
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

Natter,

Here you have the code in VBA. Its easy to port it to Harbour and FWH:

https://visualbasic.happycodings.com/api-and-miscellaneous/code42.html
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Post by Natter »

Thank you, Antonio!
Do I understand correctly that in all these examples, processes are viewed and the SessionID of each of them is read?
I do the same thing through WMI.
oList:=wmi:Execquery("select * from Win32_Process")
But since I'm an oList, the processes of all sessions fall into, I can't distinguish my own processes from others'. I don't really understand how in these examples I can get MY session ID.
Could you explain a little :(
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

Dear Natter,

I have not tested them myself so I can't tell you how they will behave :-)

Anyhow, this seems to provide a hint:
WTSEnumerateProcess populates a WTS_PROCESS_INFO structure with information pertaining to running processes on the specified host. If the hServer parameter is set to WTS_CURRENT_SERVER_HANDLE, this will run against the host which is running the software.


So the better thing that we can do is build and run the tests and see what we get
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

Could you please explain how do you create wmi ?

oList := wmi:Execquery( "select * from Win32_Process" )
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Post by Natter »

Code: Select all | Expand

oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
    oWMI   := oLocator:ConnectServer()
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

Please try this:

oList := wmi:Execquery( "select * from Win32_Process" )

XBrowse( oList )

and post here what you get, thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

WaitRun( "query user > users.txt" )
MsgInfo( MemoRead( "users.txt" ) )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/query-user

query user [<username> | <sessionname> | <sessionID>] [/server:<servername>]
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Post by Natter »

Everything turned out to be simple.
When the application starts, the new process becomes the first in the list of processes. We look at this process .ProcessID and .SessionID So we can get the PID and SID of the current process. Usually the SID is 0. If RDP, then this is the session handle
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

Would you mind to share the code that you used to solve it ?

many thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1241
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Post by Natter »

Code: Select all | Expand

local PID, SID
local oList, oProc

  oList:= oWmi:ExecQuery( "select * from Win32_Process'" )
  for each oProc in oList
    PID:=oProc:ProcessId
    SID:=oProc:SessionId
    exit
  next


I didn't check under RDP. But there, it seems to me, is only a method.Getowner() does not work
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Re: ID of the current session

Post by Antonio Linares »

thank you
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply