FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour ID of the current session
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
ID of the current session
Posted: Thu Jan 06, 2022 02:00 PM

Hi,

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Thu Jan 06, 2022 05: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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Thu Jan 06, 2022 05:57 PM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Thu Jan 06, 2022 06:02 PM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: ID of the current session
Posted: Thu Jan 06, 2022 07: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 :-)
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 08: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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 08: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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: ID of the current session
Posted: Fri Jan 07, 2022 09:55 AM
Code (fw): Select all Collapse
oLocator := CREATEOBJECT( "wbemScripting.SwbemLocator" )
    oWMI   := oLocator:ConnectServer()
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 02: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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 02:29 PM

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 02: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
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: ID of the current session
Posted: Fri Jan 07, 2022 05: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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Fri Jan 07, 2022 06:46 PM

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

many thanks

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: ID of the current session
Posted: Fri Jan 07, 2022 07:14 PM
Code (fw): Select all Collapse
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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: ID of the current session
Posted: Sat Jan 08, 2022 08:50 AM

thank you

regards, saludos

Antonio Linares
www.fivetechsoft.com