Google Answers Logo
View Question
 
Q: Win32: Determine if current user logged in from console or remote desktop? ( No Answer,   2 Comments )
Question  
Subject: Win32: Determine if current user logged in from console or remote desktop?
Category: Computers > Programming
Asked by: vichoty-ga
List Price: $2.00
Posted: 01 Jul 2005 15:46 PDT
Expires: 31 Jul 2005 15:46 PDT
Question ID: 539229
Hi, 

What is the way with win32 to determine whether if a current user
being logged in has logged in through console user or through remote
desktop?


I am using the following mechanism to find out the current logged in user SID:
. get the desktop window (e.g. explorer.exe)
. get its process ID
. use OpenProcessToken and GetTokenInformation to retreive the SID of
the desktop window's owner

The above would work with both cases, which I am looking to tell the difference.

Thanks 
Victor
Answer  
There is no answer at this time.

Comments  
Subject: Re: Win32: Determine if current user logged in from console or remote desktop?
From: dkorber-ga on 07 Jul 2005 04:19 PDT
 
This script tells you if you are running under a TS Session or not:

Dim Sh, Clientname, Computername
Set Sh = WScript.CreateObject("WScript.Shell")
Clientname =  UCase(Sh.ExpandEnvironmentStrings("%Clientname%"))
Computername =  UCase(Sh.ExpandEnvironmentStrings("%Computername%"))

If (Clientname <> "%CLIENTNAME%") And (Clientname <> Computername) Then
	IsTSSession = True
Else
	IsTSSession = False
End If

If IsTSSession Then
	WScript.Echo "This is a Terminal Services session"
Else
	WScript.Echo "This is NOT a Terminal Services session"
End If
Subject: Re: Win32: Determine if current user logged in from console or remote desktop?
From: coolchap_here-ga on 19 Jul 2005 06:46 PDT
 
Hi,

You can use WMI to get the logged in user's SID as follows:
{save the following code as test.vbs}
This script will return the logged in user name and his SID.

Dim strComputer    : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:" & _
                           "{impersonationLevel=impersonate}!\\" & _
                           strComputer & "\Root\CIMV2")
msgbox "Currently logged in user is: " & strGetLoggedOnUserAccount()
msgbox "SID of logged in user is : " & getSIDOfLoogedInUser()
Function strGetLoggedOnUserAccount()
    Dim colProcesses
    Dim objProcess
    Dim strUser
    Dim strDomain
    Dim strAccount
    Dim lReturn
    On Error Resume Next
    Set colProcesses = objWMIService.ExecQuery _
         ("Select name from Win32_Process Where Name = 'explorer.exe'
AND SessionId = 0")
    strUser = ""
    strDomain = ""
    For Each objProcess In colProcesses
        lReturn = objProcess.GetOwner(strUser, strDomain)
        If lReturn = 0 Then
        strAccount = strDomain & "\" & strUser
        End If
    Next
    If strAccount <> "" Then 
        strGetLoggedOnUserAccount = strAccount
    Else
        MsgBox ("Unexpected error")
        Call WScript.Quit(1)
    End If
    On Error Goto 0
End Function

Function getSIDOfLoogedInUser()
	On Error Resume Next
	
	Dim strUserName
	Dim objSID
	Dim objRegistry
	Dim strComputer
	Dim lngRtn
	Dim strSID
	Dim arrRegKeys
	Const HKLM = &H80000002  
	strComputer = "."
	getSIDOfLoogedInUser = ""
	
	'get the current logged in user name with domain
    strUserName = strGetLoggedOnUserAccount()
	
	
	'obtain the SID for the current logged in user
	Set objRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\default:StdRegProv")
    lngRtn = objRegistry.EnumKey(HKLM, "SOFTWARE\Microsoft\Windows
NT\CurrentVersion\ProfileList", arrRegKeys)
	If lngRtn = 0 Then
		For Each strSID In arrRegKeys
				Set objSID = objWMIService.Get("Win32_SID='" & strSID & "'")
				If UCase(objSID.ReferencedDomainName & "\" & objSID.AccountName) =
UCase(strUserName) Then
					getSIDOfLoogedInUser = strSID					
				End If 
		Next
	End If
	On Error Goto 0
End Function

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy