Page 1 of 1

ID of the current session

PostPosted: Thu Jan 06, 2022 2:00 pm
by Natter
Hi,

I connect to the server via RDP. Is it possible to get the ID of the current session ?

Re: ID of the current session

PostPosted: Thu Jan 06, 2022 5:55 pm
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

Re: ID of the current session

PostPosted: Thu Jan 06, 2022 5:57 pm
by Antonio Linares

Re: ID of the current session

PostPosted: Thu Jan 06, 2022 6:02 pm
by Antonio Linares

Re: ID of the current session

PostPosted: Thu Jan 06, 2022 7:52 pm
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 :(

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 8:35 am
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

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 8:41 am
by Antonio Linares
Could you please explain how do you create wmi ?

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

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 9:55 am
by Natter
Code: Select all  Expand view
oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
    oWMI   := oLocator:ConnectServer()

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 2:15 pm
by Antonio Linares
Please try this:

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

XBrowse( oList )

and post here what you get, thanks

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 2:29 pm
by Antonio Linares
WaitRun( "query user > users.txt" )
MsgInfo( MemoRead( "users.txt" ) )

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 2:33 pm
by Antonio Linares
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/query-user

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

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 5:25 pm
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

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 6:46 pm
by Antonio Linares
Would you mind to share the code that you used to solve it ?

many thanks

Re: ID of the current session

PostPosted: Fri Jan 07, 2022 7:14 pm
by Natter
Code: Select all  Expand view
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

Re: ID of the current session

PostPosted: Sat Jan 08, 2022 8:50 am
by Antonio Linares
thank you