ID of the current session

ID of the current session

Postby Natter » Thu Jan 06, 2022 2:00 pm

Hi,

I connect to the server via RDP. Is it possible to get the ID of the current session ?
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Postby Antonio Linares » Thu Jan 06, 2022 5:55 pm

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: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Antonio Linares » Thu Jan 06, 2022 5:57 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Antonio Linares » Thu Jan 06, 2022 6:02 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Natter » Thu Jan 06, 2022 7:52 pm

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 :(
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 8:35 am

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: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 8:41 am

Could you please explain how do you create wmi ?

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Natter » Fri Jan 07, 2022 9:55 am

Code: Select all  Expand view
oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
    oWMI   := oLocator:ConnectServer()
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 2:15 pm

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: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 2:29 pm

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 2:33 pm

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
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Natter » Fri Jan 07, 2022 5:25 pm

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
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Postby Antonio Linares » Fri Jan 07, 2022 6:46 pm

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

many thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ID of the current session

Postby Natter » Fri Jan 07, 2022 7:14 pm

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
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: ID of the current session

Postby Antonio Linares » Sat Jan 08, 2022 8:50 am

thank you
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42065
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], nageswaragunupudi and 53 guests