Re: MAC Address
- From: Denys Rodrigues <DenysRodrigues@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 21 Apr 2007 08:20:02 -0700
Robert,
Thanks for the answer, but the code below will give me only the mac address
of the server, am I right?
What I need is to have a unique hardware identification of my clients. They
can be any kind of device (smartphone, pda, thin clients or desktop pcs)
Denys
"Robert Robinson" wrote:
My apologies to all for the length of the code snippets. I hope that.
this may be helpful.
The code shows how to find a number of terminal services parameters and
the MAC address. Note that the IP address of the client is not reliable
as is discussed in the comments. Also, note that it is easy to read the
MAC addresses on the computer on which the code resides, but the server
cannot read the MAC address of the client unless there is a program that
reads this information on the client and passes it to the server.
Also, many machines have more than one MAC address and you may need to
check each one of these.
Robbie
Private Enum WTS_CONNECTSTATE_CLASS
WTSActive
WTSConnected
WTSConnectQuery
WTSShadow
WTSDisconnected
WTSIdle
WTSListen
WTSReset
WTSDown
WTSInit
End Enum
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure WTS_SESSION_INFO
Dim SessionID As Int32 'DWORD integer
Dim pWinStationName As String ' integer LPTSTR - Pointer to a
null-terminated string containing the name of the WinStation for this
session
Dim State As WTS_CONNECTSTATE_CLASS
End Structure
Friend Structure strSessionsInfo
Dim SessionID As Integer
Dim StationName As String
Dim ConnectionState As String
End Structure
Private Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
WTSClientProtocolType
WTSIdleTime
WTSLogonTime
WTSIncomingBytes
WTSOutgoingBytes
WTSIncomingFrames
WTSOutgoingFrames
End Enum
'Structure for TS Client IP Address
<StructLayout(LayoutKind.Sequential)> _
Private Structure _WTS_CLIENT_ADDRESS
Public AddressFamily As Integer
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)> _
Public Address As Byte()
End Structure
'Structure for TS Client Information
Friend Structure WTS_CLIENT_INFO
Public WTSStatus As Boolean
Public WTSUserName As String
Public WTSStationName As String
Public WTSDomainName As String
Public WTSClientName As String
Public AddressFamily As Integer
Public Address As Byte()
End Structure
'Function for TS Session Information excluding Client IP address
Private Declare Function WTSQuerySessionInformation Lib
"WtsApi32.dll" Alias "WTSQuerySessionInformationW" (ByVal hServer As
Int32, _
ByVal SessionId As Int32, ByVal WTSInfoClass As Int32,
<MarshalAs(UnmanagedType.LPWStr)> ByRef ppBuffer As String, ByRef pCount
As Int32) As Boolean
'Function for TS Client IP Address
Private Declare Function WTSQuerySessionInformation2 Lib
"WtsApi32.dll" Alias "WTSQuerySessionInformationW" (ByVal hServer As
Int32, _
ByVal SessionId As Int32, ByVal WTSInfoClass As Int32, ByRef
ppBuffer As IntPtr, ByRef pCount As Int32) As Boolean
Private Declare Function GetCurrentProcessId Lib "Kernel32.dll"
Alias "GetCurrentProcessId" () As Int32
Private Declare Function ProcessIdToSessionId Lib "Kernel32.dll"
Alias "ProcessIdToSessionId" (ByVal processID As Int32, ByRef sessionID
As Int32) As Boolean
Private Declare Function WTSGetActiveConsoleSessionId Lib
"Kernel32.dll" Alias "WTSGetActiveConsoleSessionId" () As Int32
<DllImport("wtsapi32.dll", _
bestfitmapping:=True, _
CallingConvention:=CallingConvention.StdCall, _
CharSet:=CharSet.Auto, _
EntryPoint:="WTSEnumerateSessions", _
setlasterror:=True, _
ThrowOnUnmappableChar:=True)> _
Private Shared Function WTSEnumerateSessions( _
ByVal hServer As IntPtr, _
<MarshalAs(UnmanagedType.U4)> _
ByVal Reserved As Int32, _
<MarshalAs(UnmanagedType.U4)> _
ByVal Vesrion As Int32, _
ByRef ppSessionInfo As IntPtr, _
<MarshalAs(UnmanagedType.U4)> _
ByRef pCount As Int32) As Int32
End Function
<DllImport("wtsapi32.dll")> _
Private Shared Sub WTSFreeMemory(ByVal pMemory As IntPtr)
End Sub
<DllImport("wtsapi32.dll", CharSet:=CharSet.Auto,
SetLastError:=True)> _
Private Shared Function WTSOpenServer(ByVal pServerName As String)
As IntPtr
End Function
<DllImport("wtsapi32.dll", CharSet:=CharSet.Auto,
SetLastError:=True)> _
Private Shared Sub WTSCloseServer(ByVal hServer As IntPtr)
End Sub
Friend Function GetSessions(ByVal ServerName As String, ByRef
ClientInfo As WTS_CLIENT_INFO) As Boolean
'This is a test Visual Basic NET program that accesses Terminal
Services Session information.
'It also accesses the Client Information for the client which
executes this program on a TS Server.
'The program has been compiled with VisualStudio 2005 and
executed on Windows Server 2003
'It has also been compiled with VisualStudio Orcas and executed
on Windows Server 2007 (Longhorn Server).
'The program can be tested by creating a Windows console VB
application with form1 and a button1 button.
'
'This program is based in part on code that is available from
various Internet forums.
'Much credit and thanks are due to Eric Guerra, Microsoft
Developer Support, who provides the
'solutions to a number of difficult programming issues.
'
'Note that there are specific issues with identifying a client
IP address.
'Quoted from Microsoft at:
http://msdn2.microsoft.com/en-us/library/aa383857.aspx
'The client network address is reported by the RDP client
itself when it connects to the server.
'This could be different than the address that actually
connected to the server.
'For example, suppose there is a NAT between the client and the
server. The client
'can report its own IP address, but the IP address that
actually connects to the server is
'the NAT address. For VPN connections, the IP address might not
be discoverable by the client.
'If it cannot be discovered, the client can report the only IP
address it has, which may be the
'ISP assigned address. Because the address may not be the
actual network address, it should not
'be used as a form of client authentication.
'End Quote
Dim ptrOpenedServer As IntPtr
Try
ptrOpenedServer = WTSOpenServer(ServerName)
If ptrOpenedServer = vbNull Then
MessageBox.Show("Terminal Services not running on : " &
ServerName)
ClientInfo.WTSStationName = "Console"
GetSessions = False
Exit Function
End If
Dim FRetVal As Int32
Dim ppSessionInfo As IntPtr = IntPtr.Zero
Dim Count As Int32 = 0
Try
FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1,
ppSessionInfo, Count)
If FRetVal <> 0 Then
Dim sessionInfo() As WTS_SESSION_INFO = New
WTS_SESSION_INFO(Count) {}
Dim i As Integer
Dim session_ptr As System.IntPtr
For i = 0 To Count - 1
session_ptr = ppSessionInfo.ToInt32() + (i *
Marshal.SizeOf(sessionInfo(i)))
sessionInfo(i) =
CType(Marshal.PtrToStructure(session_ptr, GetType(WTS_SESSION_INFO)),
WTS_SESSION_INFO)
Next
WTSFreeMemory(ppSessionInfo)
Dim tmpArr(sessionInfo.GetUpperBound(0)) As
strSessionsInfo
For i = 0 To tmpArr.GetUpperBound(0)
tmpArr(i).SessionID = sessionInfo(i).SessionID
tmpArr(i).StationName =
sessionInfo(i).pWinStationName
tmpArr(i).ConnectionState =
GetConnectionState(sessionInfo(i).State)
'MessageBox.Show(tmpArr(i).StationName & " " &
tmpArr(i).SessionID & " " & tmpArr(i).ConnectionState)
Next
ReDim sessionInfo(-1)
Else
Throw New ApplicationException("No data retruned")
End If
Catch ex As Exception
Throw New Exception(ex.Message & vbCrLf &
System.Runtime.InteropServices.Marshal.GetLastWin32Error)
End Try
Catch ex As Exception
Throw New Exception(ex.Message)
Exit Function
Finally
End Try
'Get ProcessID of TS Session that executed this TS Session
Dim active_process As Int32 = GetCurrentProcessId()
Dim active_session As Int32 = 0
Dim success1 As Boolean = ProcessIdToSessionId(active_process,
active_session)
If success1 = False Then
MessageBox.Show("Error: ProcessIdToSessionId")
End If
Dim returned As Integer
Dim str As String = ""
Dim success As Boolean = False
ClientInfo.WTSStationName = ""
ClientInfo.WTSClientName = ""
ClientInfo.Address(2) = 0
ClientInfo.Address(3) = 0
ClientInfo.Address(4) = 0
ClientInfo.Address(5) = 0
'Get User Name of this TS session
If WTSQuerySessionInformation(ptrOpenedServer, active_session,
WTS_INFO_CLASS.WTSUserName, str, returned) = True Then
ClientInfo.WTSUserName = str
End If
'Get StationName of this TS session
If WTSQuerySessionInformation(ptrOpenedServer, active_session,
WTS_INFO_CLASS.WTSWinStationName, str, returned) = True Then
ClientInfo.WTSStationName = str
End If
'Get Domain Name of this TS session
If WTSQuerySessionInformation(ptrOpenedServer, active_session,
WTS_INFO_CLASS.WTSDomainName, str, returned) = True Then
ClientInfo.WTSDomainName = str
End If
'Skip client name and client address if this is a console session
If ClientInfo.WTSStationName <> "Console" Then
If WTSQuerySessionInformation(ptrOpenedServer,
active_session, WTS_INFO_CLASS.WTSClientName, str, returned) = True Then
ClientInfo.WTSClientName = str
End If
'Get client IP address
Dim addr As IntPtr
If WTSQuerySessionInformation2(ptrOpenedServer,
active_session, WTS_INFO_CLASS.WTSClientAddress, addr, returned) = True Then
Dim obj As New _WTS_CLIENT_ADDRESS()
obj = CType(Marshal.PtrToStructure(addr,
obj.GetType()), _WTS_CLIENT_ADDRESS)
ClientInfo.Address(2) = obj.Address(2)
ClientInfo.Address(3) = obj.Address(3)
ClientInfo.Address(4) = obj.Address(4)
ClientInfo.Address(5) = obj.Address(5)
End If
End If
WTSCloseServer(ptrOpenedServer)
Return True
Dim clientMAC(10) As String
Dim iCount As Integer
stationName = clientInfo.WTSStationName
If clientInfo.WTSStationName <> "Console" Then
clientUserName = clientInfo.WTSUserName
clientMachineName = clientInfo.WTSClientName
Else
clientUserName = System.Environment.UserName
- Follow-Ups:
- Re: MAC Address
- From: Robert Robinson
- Re: MAC Address
- References:
- Re: MAC Address
- From: JP
- Re: MAC Address
- From: Robert Robinson
- Re: MAC Address
- Prev by Date: Re: Terminal Server quits
- Next by Date: Printer Spooler always hang, but no error message in event viewer
- Previous by thread: Re: MAC Address
- Next by thread: Re: MAC Address
- Index(es):