如何从vb6代码确定登录到Windows服务器的工作站的IP地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从vb6代码确定登录到Windows服务器的工作站的IP地址相关的知识,希望对你有一定的参考价值。
我有一个多用户的VB6程序,可以在网络上运行。某些用户使用的是Windows XP桌面,有些用户使用RDP登录Windows 2008R2服务器。我有一个新的要求,要求我知道局域网上用户的IP地址,远程用户的IP或远程用户的登录。我不确定这是否可行。任何帮助或信息将不胜感激。
答案
这将返回当前RDP会话的客户端IP地址或未在RDP会话下执行时的空字符串。
客户端IP地址是mstsc.exe
在建立连接时报告的客户端计算机的本地IP之一,可能与使用netstat
或类似网站观察到的真实IP不同。
Option Explicit
'--- for WTSQuerySessionInformation
Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1
Private Const WTSClientAddress As Long = 14
'--- for AddressFamily
Private Const AF_INET As Long = 2
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, ByVal SessionId As Long, ByVal WtsInfoClass As Long, ppBuffer As Long, pBytesReturned As Long) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32" (ByVal pMemory As Long)
Private Type WTS_CLIENT_ADDRESS
AddressFamily As Long
Address(0 To 19) As Byte
End Type
Private Function GetSessionClientIp() As String
Dim uAddress As WTS_CLIENT_ADDRESS
Dim lPtr As Long
Dim lSize As Long
Call WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, lPtr, lSize)
If lSize >= LenB(uAddress) Then
Call CopyMemory(uAddress, ByVal lPtr, LenB(uAddress))
End If
Call WTSFreeMemory(lPtr)
If uAddress.AddressFamily = AF_INET Then
GetSessionClientIp = uAddress.Address(2) & "." & uAddress.Address(3) & "." & uAddress.Address(4) & "." & uAddress.Address(5)
End If
End Function
Private Sub Form_Load()
MsgBox "GetSessionClientIp=" & GetSessionClientIp(), vbExclamation
End Sub
以上是关于如何从vb6代码确定登录到Windows服务器的工作站的IP地址的主要内容,如果未能解决你的问题,请参考以下文章
如何从 ATL activex 控件将错误字符串和错误代码返回到 VB6?