Re: Get current logged-in user
- From: Lattis <Lattis@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 Oct 2006 11:21:01 -0800
I am reffering to the person physically attached.
Its a windows 2000 terminal server with 3 users connected. 1 in front of the
server and another 2 with thinclients. Each user logs on with a different
login account. They all run an application that starts with an application
loader that launches that application as a different user (lets say
application_user). This application in turn starts another one where I need
to know the user name of the current session (The user that originally
launched the application)
I tried your suggestion but it is not easy for me to understand how to read
the buffer after I call the WTSUserName. I have searched everywhere and there
are no examples that use WTSUserName!!
The code I use is the following:
Imports System
Imports System.Runtime.InteropServices
Namespace TerminalServer
Friend Class Utilities
Public Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
WTSClientProtocolType
End Enum
<DllImport("Wtsapi32.dll")> _
Public Shared Function WTSQuerySessionInformation(ByVal hServer As
System.IntPtr, ByVal sessionId As Integer, ByVal wtsInfoClass As
WTS_INFO_CLASS, ByRef ppBuffer As System.IntPtr, ByRef pBytesReturned As
System.UInt32) As Boolean
End Function
<DllImport("Kernel32.dll")> _
Public Shared Function WTSGetActiveConsoleSessionId() As Integer
End Function
<DllImport("wtsapi32.dll")> _
Public Shared Sub WTSFreeMemory(ByVal memory As IntPtr)
End Sub
Public Const WTS_CURRENT_SERVER_HANDLE As Integer = -1
Friend Shared Function GetUserName() As String
Dim buffer As System.IntPtr = IntPtr.Zero
Dim bytesReturned As System.UInt32
Dim UserName As String
Dim sessionID As Integer = WTSGetActiveConsoleSessionId()
Try
Dim sessionInfo As Boolean =
WTSQuerySessionInformation(System.IntPtr.Zero, sessionID,
WTS_INFO_CLASS.WTSUserName, buffer, bytesReturned)
UserName = ' This is were I dont know what to do!
Catch
Return True
Finally
WTSFreeMemory(buffer)
buffer = IntPtr.Zero
End Try
Return UserName
End Function
End Class
End Namespace
Any help GREATLY appreciated.
Thanks
Lucas
"Jani Järvinen [MVP]" wrote:
Lucas,.
User A is logged in to a windows 2000 terminal. He runs an application
which
runs under the credentials of a different user. If I try to see the
current
user throught the application I can only see the user that started the
thread. How can I see the user that is logged in??
By "terminal", do you mean a physical PC, a user running cmd.exe (the "DOS
prompt"), or a server with Terminal Services and/or Citrix MetaFrame
running? Because Windows operating systems since NT4 can support multiple
users at the same time, there can be multiple explorer.exe's running at the
same time. Which of these users do you want? Also, does the application that
you refer to impersonate another user or not?
See this blog entry for discussion about the problem:
http://blogs.msdn.com/oldnewthing/archive/2006/08/22/712677.aspx
Now, my guess is that you are referring to the person physically attached to
the PC and his/her username, and in this case you could use the Terminal
Services (Win32) API functions WTSGetActiveConsoleSessionId and
WTSQuerySessionInformation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
First, you would call WTSGetActiveConsoleSessionId to get a session
identifier to the active console session (if any) and then given the session
ID, you would call WTSQuerySessionInformation with the WTSUserName
enumeration. This might be the information that you are looking for, though
I'm not exactly sure. Of course, you would need to use P/Invoke from .NET
code to get to these unmanaged API functions.
As for reference to others, to easily get the name of the user that is
running your application, you can use these properties in .NET:
- Environment.UserName
- System.Windows.Forms.SystemInformation.UserName
- or System.Security.Principal.WindowsIdentity.GetCurrent().Name
The last one returns the domain/computer name along with the username; the
two former ones only return the username.
Hope this helps!
--
Regards,
Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@xxxxxxxxxxxxxxxxxxxxxx
http://www.saunalahti.fi/janij/
- References:
- Re: Get current logged-in user
- From: Jani Järvinen [MVP]
- Re: Get current logged-in user
- Prev by Date: Re: incident tracking software
- Next by Date: PrintDocument speed is slow
- Previous by thread: Re: Get current logged-in user
- Next by thread: how much time do you spend filling in exam survey?
- Index(es):
Relevant Pages
|