java调用user32的API_java如何调用wtsapi32.dll获取任务管理器里面的用户信息

这篇博客介绍了如何在Java中使用C#的DllImport特性来调用wtsapi32.dll,从而获取Windows任务管理器中的用户登录信息。通过封装WTSQuerySessionInformation等API,可以得到会话ID、用户名、客户端名和会话状态等数据。
摘要由CSDN通过智能技术生成

查到c#调用wtsapi32.dll的相关调用代码

using System;

//---引用

using System.Runtime.InteropServices;

using System.Text;

///

/// Windows 任务管理器登录用户信息

/// author:Stone_W

/// date:2011.1.14

///

public static class ComputerLoginUserInfo

{

#region 本机连接用户信息API封装

public class TSControl

{

[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);

public enum WTSInfoClass

{

WTSInitialProgram,

WTSApplicationName,

WTSWorkingDirectory,

WTSOEMId,

WTSSessionId,

WTSUserName,

WTSWinStationName,

WTSDomainName,

WTSConnectState,

WTSClientBuildNumber,

WTSClientName,

WTSClientDirectory,

WTSClientProductId,

WTSClientHardwareId,

WTSClientAddress,

WTSClientDisplay,

WTSClientProtocolType

}

public enum WTS_CONNECTSTATE_CLASS

{

WTSActive,

WTSConnected,

WTSConnectQuery,

WTSShadow,

WTSDisconnected,

WTSIdle,

WTSListen,

WTSReset,

WTSDown,

WTSInit,

}

public struct WTS_SESSION_INFO

{

public int SessionID;

[MarshalAs(UnmanagedType.LPTStr)]

public string pWinStationName;

public WTS_CONNECTSTATE_CLASS state;

}

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;

}

}

#endregion

public static System.Collections.Generic.List ComputerLoginUserInfoList;

public static void initComputerLoginUserInfo()

{

#region 查询代码

TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();

ComputerLoginUserInfoModel cum = null;

ComputerLoginUserInfoList = new System.Collections.Generic.List();

for (int i = 0; i < pSessionInfo.Length; i++)

{

if ("RDP-Tcp" != pSessionInfo[i].pWinStationName)

{

try

{

int count = 0;

IntPtr buffer = IntPtr.Zero;

StringBuilder userName = new StringBuilder();           // 用户名

StringBuilder clientUser = new StringBuilder();         // 客户端名

StringBuilder stateType = new StringBuilder();          // 会话类型

bool userNameBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,

pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName,

out userName, out count);

bool clientUserBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,

pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSClientName,

out clientUser, out count);

bool stateTypeBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero,

pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSWinStationName,

out stateType, out count);

if (userNameBool && clientUserBool && stateTypeBool)

{

cum = new ComputerLoginUserInfoModel();

cum.UserName = userName.ToString();

cum.ClientUserName = clientUser.ToString();

cum.SessionType = stateType.ToString();

}

if (!string.IsNullOrEmpty(cum.UserName))

ComputerLoginUserInfoList.Add(cum);

}

catch (Exception ex) { }

}

}

#endregion

}

static void Main(string[] args)

{

initComputerLoginUserInfo();

Console.Read();

}

}

public class ComputerLoginUserInfoModel

{

#region 用户信息字段

private string userName;

private string clientUserName;

private string sessionType;

///

/// 会话类型

///

public string SessionType

{

get { return sessionType; }

set { sessionType = value; }

}

///

/// 客户端名

///

public string ClientUserName

{

get { return clientUserName; }

set { clientUserName = value; }

}

///

/// 登录用户名

///

public string UserName

{

get { return userName; }

set { userName = value; }

}

}

#endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值