C# winform系统下 远程桌面连接时取得客户端的IP
来源:互联网 发布:淘宝950格子代码 编辑:程序博客网 时间:2024/06/05 13:21
最近在遇到个系统,是移动终端A(可以把它看成普通电脑)通过远程桌面连接服务器B调用B上的程序,如何得到终端A的IP?
查了好多资料,也请教了高手。从以前一个VB程序中的方法改成C#的方法。
基本思路是API 调用wtsapi32.dll来实现。
废话不多说,上代码。。。
using System;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
namespace ArrivalInspection
{
/// <summary>
/// </summary>
public class csClientIP
{
[DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WTSEnumerateSessions(int hServer, int Reserved, int Version, ref long ppSessionInfo, ref int pCount);
[DllImport("wtsapi32.dll")]
public static extern void WTSFreeMemory(System.IntPtr pMemory);
[DllImport("wtsapi32.dll")]
public static extern bool WTSLogoffSession(int hServer, long SessionId, bool bWait);
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out StringBuilder ppBuffer, out int pBytesReturned);
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out byte[] ppBuffer, out int pBytesReturned);
public enum WTSInfoClass
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType
}
/**/
/// <summary>
/// The WTS_CONNECTSTATE_CLASS enumeration type contains INT values that indicate the connection state of a Terminal Services session.
/// </summary>
public enum WTS_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit,
}
/**/
/// <summary>
/// The WTS_SESSION_INFO structure contains information about a client session on a terminal server.
/// if the WTS_SESSION_INFO.SessionID==0, it means that the SESSION is the local logon user's session.
/// </summary>
public struct WTS_SESSION_INFO
{
public int SessionID;
[MarshalAs(UnmanagedType.LPTStr)]
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS state;
}
/**/
/// <summary>
/// The SessionEnumeration function retrieves a list of WTS_SESSION_INFO on a current terminal server.
/// </summary>
/// <returns>a list of WTS_SESSION_INFO on a current terminal server</returns>
public static WTS_SESSION_INFO[] SessionEnumeration()
{
//Set handle of terminal server as the current terminal server
int hServer = 0;
bool RetVal;
long lpBuffer = 0;
int Count = 0;
long p;
WTS_SESSION_INFO Session_Info = new WTS_SESSION_INFO();
WTS_SESSION_INFO[] arrSessionInfo;
RetVal = WTSEnumerateSessions(hServer, 0, 1, ref lpBuffer, ref Count);
arrSessionInfo = new WTS_SESSION_INFO[0];
if (RetVal)
{
arrSessionInfo = new WTS_SESSION_INFO[Count];
int i;
p = lpBuffer;
for (i = 0; i < Count; i++)
{
arrSessionInfo[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(new IntPtr(p), Session_Info.GetType());
p += Marshal.SizeOf(Session_Info.GetType());
}
WTSFreeMemory(new IntPtr(lpBuffer));
}
else
{
//Insert Error Reaction Here
}
return arrSessionInfo;
}
}
}
////上面是写成了一个类,下面是取得客户端IP的方法
private string getClientIP()
{
csClientIP.WTS_SESSION_INFO[] pSessionInfo = csClientIP.SessionEnumeration();
try
{
int count = 0;
IntPtr buffer = IntPtr.Zero;
StringBuilder sb = new StringBuilder();
bool bsuccess = csClientIP.WTSQuerySessionInformation(IntPtr.Zero, -1, csClientIP.WTSInfoClass.WTSClientName, out sb, out count);
string strIP = "";
if (bsuccess)
{
string strName = sb.ToString().Trim();
if(strName == "")
{
strIP = "";
}
else
{
IPHostEntry ipEntry = Dns.GetHostByName(strName);
IPAddress[] IpAddr = ipEntry.AddressList;
for (int i=0;i<IpAddr.Length;i++)
{
strIP = strIP + IpAddr[i].ToString();
}
}
}
return strIP;
}
catch (Exception ex)
{
throw ex;
}
}
- C# winform系统下 远程桌面连接时取得客户端的IP
- VC6中获取远程桌面连接的客户端ip地址
- 取得客户端的 IP 地址
- [转]取得客户端的IP
- 在winForm下,c#实现出勤系统的客户端(基础类的设计)
- 在winForm下,c#实现出勤系统的客户端(准备工作)
- 在动态ip下实现远程桌面连接
- 【系统】远程桌面的连接方法
- C# WinForm 下获取外网的IP地址
- Flex取得客户端的IP地址
- Flex取得客户端的IP地址
- 一个取得客户端IP的函数
- java取得客户端的IP地址
- 如何取得客户端的IP地址
- Windows远程桌面连接客户端过期的…
- 怎样删除远程桌面连接的ip地址
- java 取得 客户端IP
- apache取得客户端ip
- php linux sphinx 安装
- 并行计算部分总结
- 查看当前进程,或死锁进程,并能自动杀掉死进程
- PeerSim 代码初探和使用
- 用MS Word 2007往CSDN发表博客
- C# winform系统下 远程桌面连接时取得客户端的IP
- HTTP 错误 500.100 - 内部服务器错误 - ASP 错误 不能使用(未知的) 文件使用中
- php linux sphinx 命令和建立索引
- 使用ibatis2的jpetstore报错
- C#3.0新特性之隐式类型数组
- 我也说说CSDN
- phpexcel 导入操作 excel日期类型转换
- Linux_shell_grep
- dunit 测试应用