我在Windows Server 2016上有一个简单的登录脚本,该脚本在客户端连接时将数据写入db。
在此服务器上,我已经发布了一个应用程序,比如说Microsoft Paint。
客户端在控制面板->远程应用程序和桌面连接中建立连接后,便可以远程使用此应用程序。 远程应用程序(在我们的情况下为Microsoft Paint)然后显示在开始菜单中。 如果打开它,则将建立正常的远程桌面连接,并且Microsoft Paint会远程打开。
现在我想要的是获取应用程序的名称,客户端的名称,打开应用程序的时间和关闭应用程序的时间,然后将所有内容写入数据库。
wtsapi32应该提供检索应用程序名称所需的功能。在WTS_INFO_CLASS中有一个属性WTSApplicationName。
不幸的是,它总是返回一个空字符串。 其他属性(例如WTSClientAdress)返回预期结果。
还有其他检索应用名称的方法吗?
任何帮助将不胜感激! 预先感谢。
public static List<SessionInfo> GetServerActiveSessions(string serverName)
{
IntPtr server = IntPtr.Zero;
List<SessionInfo> sessionInfos = new List<SessionInfo>();
server = OpenServer(serverName);
if (server != IntPtr.Zero)
{
try
{
IntPtr ppSessionInfo = IntPtr.Zero;
Int32 count = 0;
if (WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count))
{
Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
Int32 current = (int)ppSessionInfo;
for (int i = 0; i < count; i++)
{
SessionInfo sessionInfo = new SessionInfo();
WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
current += dataSize;
sessionInfo.ApplicationName = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSApplicationName);
sessionInfo.ClientAddress = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientAddress);
sessionInfo.ClientBuildNumber = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientBuildNumber);
sessionInfo.ClientDirectory = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientDirectory);
sessionInfo.ClientDisplay = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientDisplay);
sessionInfo.ClientDirectory = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientDirectory);
sessionInfo.ClientHardwareId = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientHardwareId);
sessionInfo.ClientName = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientName);
sessionInfo.ClientProductId = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSClientProductId);
sessionInfo.ClientProtocolType = getProtocolType(QuerySessionInfo<short>(server, si.SessionID, WTS_INFO_CLASS.WTSClientProtocolType));
sessionInfo.ConnectState = QuerySessionInfo<WTS_CONNECTSTATE_CLASS>(server, si.SessionID, WTS_INFO_CLASS.WTSConnectState);
sessionInfo.DomainName = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSDomainName);
sessionInfo.InitialProgram = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSInitialProgram);
sessionInfo.OEMId = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSOEMId);
sessionInfo.SessionId = si.SessionID;
sessionInfo.UserName = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSUserName);
sessionInfo.WinStationName = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSWinStationName);
sessionInfo.WorkingDirectory = QuerySessionInfo(server, si.SessionID, WTS_INFO_CLASS.WTSWorkingDirectory);
sessionInfos.Add(sessionInfo);
}
WTSFreeMemory(ppSessionInfo);
}
}
finally
{
CloseServer(server);
}
}
return sessionInfos;
}