/[AnywhereTS-MSSQL]/branches/TerminalServices/TSAdminTool/TerminalServices.cs
ViewVC logotype

Annotation of /branches/TerminalServices/TSAdminTool/TerminalServices.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 242 - (hide annotations) (download)
Sat Aug 25 16:49:29 2012 UTC (8 years, 6 months ago) by william
File size: 46289 byte(s)
+ minor code overhaul

1 william 230 #region TSMANAGER VERBOSE MESSAGE LOGGING
2 william 232 //#define ENABLE_TSMANAGER_VERBOSE_MESSAGES // when define will allow the below
3 william 230 #define ENABLE_TSMANAGER_VERBOSE_DEBUG_MESSAGES // when defined will allow calls to: LogDebugVerboseMessage(string format, params object[] args)
4     #define ENABLE_TSMANAGER_VERBOSE_ERROR_MESSAGES // when defined will allow calls to: LogErrorVerboseMessage(string format, params object[] args)
5     #define ENABLE_TSMANAGER_VERBOSE_FATAL_MESSAGES // when defined will allow calls to: LogFatalVerboseMessage(string format, params object[] args)
6     #define ENABLE_TSMANAGER_VERBOSE_INFO_MESSAGES // when defined will allow calls to: LogInfoVerboseMessage(string format, params object[] args)
7     #define ENABLE_TSMANAGER_VERBOSE_WARN_MESSAGES // when defined will allow calls to: LogWarnVerboseMessage(string format, params object[] args)
8     #endregion
9 william 4 using System;
10     using System.Collections.Generic;
11     using System.Text;
12     using System.Runtime.InteropServices;
13     using System.Net;
14     using System.Windows.Forms;
15     using System.Data;
16 william 47 using log4net;
17 william 230 using System.Diagnostics;
18 william 4 namespace AnywhereTS
19     {
20     public class TSManager
21     {
22 william 230 #region TSMANAGER VERBOSE MESSAGE LOGGING
23     [Conditional("ENABLE_TSMANAGER_VERBOSE_DEBUG_MESSAGES")]
24 william 236 private static void LogDebugVerboseMessage(string format, params object[] args)
25     {
26     #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
27     Logging.DefaultLog.DebugFormat(format, args);
28     #endif
29     }
30 william 230 [Conditional("ENABLE_TSMANAGER_VERBOSE_ERROR_MESSAGES")]
31 william 236 private static void LogErrorVerboseMessage(string format, params object[] args)
32     {
33     #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
34     Logging.DefaultLog.ErrorFormat(format, args);
35     #endif
36     }
37 william 230 [Conditional("ENABLE_TSMANAGER_VERBOSE_FATAL_MESSAGES")]
38 william 236 private static void LogFatalVerboseMessage(string format, params object[] args)
39     {
40     #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
41     Logging.DefaultLog.FatalFormat(format, args);
42     #endif
43     }
44 william 230 [Conditional("ENABLE_TSMANAGER_VERBOSE_INFO_MESSAGES")]
45 william 236 private static void LogInfoVerboseMessage(string format, params object[] args)
46     {
47     #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
48     Logging.DefaultLog.InfoFormat(format, args);
49     #endif
50     }
51 william 230 [Conditional("ENABLE_TSMANAGER_VERBOSE_WARN_MESSAGES")]
52 william 236 private static void LogWarnVerboseMessage(string format, params object[] args)
53     {
54     #if ENABLE_TSMANAGER_VERBOSE_MESSAGES
55     Logging.DefaultLog.WarnFormat(format, args);
56     #endif
57     }
58 william 230 #endregion
59 william 238 #region Win32 Support
60     #region Unmanaged Code
61 william 239 [DllImport("wtsapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
62     static extern IntPtr WTSOpenServer(string pServerName);
63 william 4
64     [DllImport("wtsapi32.dll")]
65     public static extern void WTSCloseServer(IntPtr hServer);
66    
67     [DllImport("wtsapi32.dll")]
68 william 240 static extern int WTSEnumerateSessions(
69     System.IntPtr hServer,
70     int Reserved,
71     int Version,
72     ref System.IntPtr ppSessionInfo,
73     ref int pCount);
74 william 4
75 william 188 /// <summary>
76     /// The WTSQuerySessionInformation function retrieves session information for the specified
77     /// session on the specified terminal server.
78     /// It can be used to query session information on local and remote terminal servers.
79     /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
80     /// </summary>
81     /// <param name="hServer">Handle to a terminal server. Specify a handle opened by the WTSOpenServer function,
82     /// or specify <see cref="WTS_CURRENT_SERVER_HANDLE"/> to indicate the terminal server on which your application is running.</param>
83     /// <param name="sessionId">A Terminal Services session identifier. To indicate the session in which the calling application is running
84     /// (or the current session) specify <see cref="WTS_CURRENT_SESSION"/>. Only specify <see cref="WTS_CURRENT_SESSION"/> when obtaining session information on the
85     /// local server. If it is specified when querying session information on a remote server, the returned session
86     /// information will be inconsistent. Do not use the returned data in this situation.</param>
87     /// <param name="wtsInfoClass">Specifies the type of information to retrieve. This parameter can be one of the values from the <see cref="WTSInfoClass"/> enumeration type. </param>
88     /// <param name="ppBuffer">Pointer to a variable that receives a pointer to the requested information. The format and contents of the data depend on the information class specified in the <see cref="WTSInfoClass"/> parameter.
89     /// To free the returned buffer, call the <see cref="WTSFreeMemory"/> function. </param>
90     /// <param name="pBytesReturned">Pointer to a variable that receives the size, in bytes, of the data returned in ppBuffer.</param>
91     /// <returns>If the function succeeds, the return value is a nonzero value.
92     /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
93     /// </returns>
94 william 240 [DllImport("Wtsapi32.dll")]
95 william 4 public static extern bool WTSQuerySessionInformation(
96 william 240 System.IntPtr hServer,
97     int sessionId,
98     WTSInfoClass wtsInfoClass,
99     out System.IntPtr ppBuffer,
100 william 4 out uint pBytesReturned);
101    
102     [DllImport("wtsapi32.dll")]
103     static extern void WTSFreeMemory(IntPtr pMemory);
104    
105     // For Lookup of MAC address
106     [DllImport("iphlpapi.dll", ExactSpelling = true)]
107     public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[]
108     pMacAddr, ref int PhyAddrLen);
109 william 238 #endregion
110     #region Constants
111 william 231 public const int WTS_CURRENT_SESSION = -1;
112 william 238 #endregion
113     #region Type Definitions
114     [StructLayout(LayoutKind.Sequential)]
115 william 4 private struct WTS_SESSION_INFO
116     {
117 william 231 public int SessionID;
118 william 4
119     [MarshalAs(UnmanagedType.LPStr)]
120     public String pWinStationName;
121    
122     public WTS_CONNECTSTATE_CLASS State;
123     }
124     public enum WTS_CONNECTSTATE_CLASS
125     {
126     WTSActive, // User logged on to WinStation
127     WTSConnected, // WinStation connected to client
128     WTSConnectQuery, // In the process of connecting to client
129     WTSShadow, // Shadowing another WinStation
130     WTSDisconnected, // WinStation logged on without client
131     WTSIdle, // Waiting for client to connect
132     WTSListen, // WinStation is listening for connection
133     WTSReset, // WinStation is being reset
134     WTSDown, // WinStation is down due to error
135     WTSInit, // WinStation in initialization
136 william 238 };
137 william 188 /// <summary>
138     /// Contains values that indicate the type of session information to retrieve in a call to the WTSQuerySessionInformation function.
139     /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa383861(v=vs.85).aspx
140     /// </summary>
141 william 4 public enum WTSInfoClass
142     {
143 william 188 /// <summary>
144     /// A null-terminated string that contains the name of the initial program that Remote Desktop Services runs when the user logs on.
145     /// </summary>
146 william 4 WTSInitialProgram,
147 william 188 /// <summary>
148     /// A null-terminated string that contains the published name of the application that the session is running.
149     /// </summary>
150 william 4 WTSApplicationName,
151 william 188 /// <summary>
152     /// A null-terminated string that contains the default directory used when launching the initial program.
153     /// </summary>
154 william 4 WTSWorkingDirectory,
155 william 188 /// <summary>
156     /// This value is not used.
157     /// </summary>
158 william 4 WTSOEMId,
159 william 188 /// <summary>
160     /// A ULONG value that contains the session identifier.
161     /// </summary>
162 william 4 WTSSessionId,
163 william 188 /// <summary>
164     /// A null-terminated string that contains the name of the user associated with the session.
165     /// </summary>
166 william 4 WTSUserName,
167 william 188 /// <summary>
168     /// A null-terminated string that contains the name of the Remote Desktop Services session.
169     /// Note Despite its name, specifying this type does not return the window station name. Rather, it returns the name of the Remote Desktop Services session. Each Remote Desktop Services session is associated with an interactive window station. Because the only supported window station name for an interactive window station is "WinSta0", each session is associated with its own "WinSta0" window station. For more information, see Window Stations.
170     /// </summary>
171 william 4 WTSWinStationName,
172 william 188 /// <summary>
173     /// A null-terminated string that contains the name of the domain to which the logged-on user belongs.
174     /// </summary>
175 william 4 WTSDomainName,
176 william 188 /// <summary>
177     /// The session's current connection state. For more information, see WTS_CONNECTSTATE_CLASS.
178     /// </summary>
179 william 4 WTSConnectState,
180 william 188 /// <summary>
181     /// A ULONG value that contains the build number of the client.
182     /// </summary>
183 william 4 WTSClientBuildNumber,
184 william 188 /// <summary>
185     /// A null-terminated string that contains the name of the client.
186     /// </summary>
187 william 4 WTSClientName,
188 william 188 /// <summary>
189     /// A null-terminated string that contains the directory in which the client is installed.
190     /// </summary>
191 william 4 WTSClientDirectory,
192 william 188 /// <summary>
193     /// A USHORT client-specific product identifier.
194     /// </summary>
195 william 4 WTSClientProductId,
196 william 188 /// <summary>
197     /// A ULONG value that contains a client-specific hardware identifier. This option is reserved for future use. WTSQuerySessionInformation will always return a value of 0.
198     /// </summary>
199 william 4 WTSClientHardwareId,
200 william 188 /// <summary>
201     /// The network type and network address of the client. For more information, see WTS_CLIENT_ADDRESS.
202     /// The IP address is offset by two bytes from the start of the Address member of the WTS_CLIENT_ADDRESS structure.
203     /// </summary>
204 william 4 WTSClientAddress,
205 william 188 /// <summary>
206     /// Information about the display resolution of the client. For more information, see WTS_CLIENT_DISPLAY.
207     /// </summary>
208 william 4 WTSClientDisplay,
209 william 188 /// <summary>
210     /// A USHORT value that specifies information about the protocol type for the session. This is one of the following values.
211     /// 0: The console session.
212     /// 1: This value is retained for legacy purposes.
213     /// 2: The RDP protocol.
214     /// </summary>
215 william 4 WTSClientProtocolType,
216 william 188 /// <summary>
217     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
218     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
219     /// </summary>
220     WTSIdleTime,
221     /// <summary>
222     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
223     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
224     /// </summary>
225     WTSLogonTime,
226     /// <summary>
227     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
228     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
229     /// </summary>
230     WTSIncomingBytes,
231     /// <summary>
232     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
233     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
234     /// </summary>
235     WTSOutgoingBytes,
236     /// <summary>
237     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
238     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
239     /// </summary>
240     WTSIncomingFrames,
241     /// <summary>
242     /// This value returns FALSE. If you call GetLastError to get extended error information, GetLastError returns ERROR_NOT_SUPPORTED.
243     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not used.
244     /// </summary>
245     WTSOutgoingFrames,
246     /// <summary>
247     /// Information about a Remote Desktop Connection (RDC) client. For more information, see WTSCLIENT.
248     /// Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
249     /// This value is supported beginning with Windows Server 2008 and Windows Vista with SP1.
250     /// </summary>
251     WTSClientInfo,
252     /// <summary>
253     /// Information about a client session on an RD Session Host server. For more information, see WTSINFO.
254     /// Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
255     /// This value is supported beginning with Windows Server 2008 and Windows Vista with SP1.
256     /// </summary>
257     WTSSessionInfo,
258     /// <summary>
259     /// Extended information about a session on an RD Session Host server. For more information, see WTSINFOEX.
260     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
261     /// </summary>
262     WTSSessionInfoEx,
263     /// <summary>
264     /// Information about the configuration of an RD Session Host server.
265     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
266     /// </summary>
267     WTSConfigInfo,
268     /// <summary>
269     /// This value is not supported.
270     /// </summary>
271     WTSValidationInfo,
272     /// <summary>
273     /// A WTS_SESSION_ADDRESS structure that contains the IPv4 address assigned to the session. If the session does not have a virtual IP address, the WTSQuerySessionInformation function returns ERROR_NOT_SUPPORTED.
274     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
275     /// </summary>
276     WTSSessionAddressV4,
277     /// <summary>
278     /// Determines whether the current session is a remote session. The WTSQuerySessionInformation function returns a value of TRUE to indicate that the current session is a remote session, and FALSE to indicate that the current session is a local session. This value can only be used for the local machine, so the hServer parameter of the WTSQuerySessionInformation function must contain WTS_CURRENT_SERVER_HANDLE.
279     /// Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, and Windows 2000: This value is not supported.
280     /// </summary>
281     WTSIsRemoteSession,
282 william 238 } ;
283 william 4 public struct WTS_CLIENT_ADDRESS
284     {
285     public uint AddressFamily; // AF_INET, AF_IPX, AF_NETBIOS, AF_UNSPEC
286     public Byte[] Address;
287     };
288     public struct WTS_CLIENT_DISPLAY
289     {
290 william 241 public int HorizontalResolution; // horizontal dimensions, in pixels
291     public int VerticalResolution; // vertical dimensions, in pixels
292     public int ColorDepth; // 1=16, 2=256, 4=64K, 8=16M
293 william 242 /// <summary>
294     /// Returns a WTS_CLIENT_DISPLAY instance initialized to default values
295     /// </summary>
296     public static WTS_CLIENT_DISPLAY Empty
297     {
298     get
299     {
300     WTS_CLIENT_DISPLAY t = new WTS_CLIENT_DISPLAY();
301     t.HorizontalResolution = 0;
302     t.VerticalResolution = 0;
303     t.ColorDepth = 0;
304     return t;
305     }
306     }
307 william 4 };
308     // Selected info for terminal server sessions
309     public struct TS_SESSION_INFO
310     {
311 william 231 public int sessionID;
312 william 4 public string ipAddress; // The IP address of the client
313     public string macAddress; // The MAC address of the client
314     public string name; // The (netbios) name of the client
315     public string username; // User logged in to the session, if any.
316     public WTS_CONNECTSTATE_CLASS state; // The state of the session
317     public int HorizontalResolution; // Vertical screen res for the session
318     public int VerticalResolution; // Horiz screen res for the session
319     public int ColorDepth; // Screen color depth for the session
320     };
321 william 238 #endregion
322     #endregion
323    
324 william 4 public static IntPtr OpenServer(String Name)
325     {
326 william 230 LogDebugVerboseMessage("OpenServer(String Name): Name={0}", Name);
327 william 4 IntPtr server = WTSOpenServer(Name);
328 william 230 LogDebugVerboseMessage("OpenServer(String Name): server={0}", server);
329 william 4 return server;
330     }
331     public static void CloseServer(IntPtr ServerHandle)
332     {
333 william 230 LogDebugVerboseMessage("CloseServer(IntPtr ServerHandle): Closing handle={0}", ServerHandle.ToString());
334 william 4 WTSCloseServer(ServerHandle);
335 william 230 LogDebugVerboseMessage("CloseServer(IntPtr ServerHandle): Closed Handle");
336 william 4 }
337    
338    
339     // Return address for a terminal server session.
340     // WTSClientAddress gives a pointer to a WTS_CLIENT_ADDRESS structure containing
341     // the network type and network address of the client. If the function is called
342     // from the Terminal Services console, ppBuffer returns a NULL pointer.
343     // Note that the first byte of the IP address returned in the ppBuffer
344     // parameter will be located at an offset of two bytes from the first location
345     // of the buffer.
346 william 231 private static string GetTSClientAddress(int sessionID, IntPtr server)
347 william 4 {
348     System.IntPtr ppBuffer = System.IntPtr.Zero;
349     uint pBytesReturned = 0;
350     StringBuilder builder = new StringBuilder();
351    
352     // Interface avec API
353     WTS_CLIENT_ADDRESS wtsAdr = new WTS_CLIENT_ADDRESS();
354    
355    
356     if (WTSQuerySessionInformation(
357     server,
358     sessionID,
359     WTSInfoClass.WTSClientAddress,
360     out ppBuffer,
361     out pBytesReturned))
362     {
363     wtsAdr.Address = new Byte[pBytesReturned - 1];
364     int run = (int)ppBuffer; // pointeur sur les donnĂ©es
365     Type t = typeof(Byte);
366     Type t1 = typeof(uint);
367    
368     int uintSize = Marshal.SizeOf(t1);
369     int byteSize = Marshal.SizeOf(t);
370    
371     wtsAdr.AddressFamily = (uint)Marshal.ReadInt32((System.IntPtr)run);
372    
373     // Address family can be only:
374     // AF_UNSPEC = 0 (unspecified)
375     // AF_INET = 2 (internetwork: UDP, TCP, etc.)
376     // AF_IPX = AF_NS = 6 (IPX protocols: IPX, SPX, etc.)
377     // AF_NETBIOS = 17 (NetBios-style addresses)
378    
379     //run+=uintSize;
380     //run+=dataSize;
381     /*switch(wtsAdr.AddressFamily)
382     {
383     case 0:builder.Append("AF_UNSPEC");
384     break;
385     case 1:builder.Append("AF_INET");
386     break;
387     case 2:builder.Append("AF_IPX");
388     break;
389     case 3:builder.Append("AF_NETBIOS");
390     break;
391     }*/
392     for (int i = 0; i < pBytesReturned - 1; i++)
393     {
394     wtsAdr.Address[i] = Marshal.ReadByte((System.IntPtr)run);
395     run += byteSize;
396     // TO GET and to SEE ALL the DATA
397     //builder.Append(wtsAdr.Address[i].ToString()+"-");
398     }
399     //builder.Append("-");
400    
401     // The IP address is located in bytes 2, 3, 4, and 5. The other bytes are not used.
402     // If AddressFamily returns AF_UNSPEC, the first byte in Address
403     // is initialized to zero.
404    
405     // Check if the returned address is an IP address
406     if (wtsAdr.AddressFamily == 2)
407     { // It is an IP address
408     builder.Append((wtsAdr.Address[4 + 2]).ToString());
409     builder.Append(".");
410     builder.Append((wtsAdr.Address[4 + 3]).ToString());
411     builder.Append(".");
412     builder.Append((wtsAdr.Address[4 + 4]).ToString());
413     builder.Append(".");
414     builder.Append((wtsAdr.Address[4 + 5]).ToString());
415     }
416     }
417     WTSFreeMemory(ppBuffer);
418     return builder.ToString();
419     }
420    
421     // Get display parameters for a user session on a terminal server
422     // In: SessionID = identifier for the session
423     // In: server = server handle for the server that the session resides on.
424 william 231 private static void GetTSClientDisplay(int sessionID, IntPtr server, out int horizontalResolution, out int verticalResolution, out int colorDepth)
425 william 242 {
426     WTS_CLIENT_DISPLAY clientDisplay = WTS_CLIENT_DISPLAY.Empty;
427     horizontalResolution = clientDisplay.HorizontalResolution;
428     verticalResolution = clientDisplay.VerticalResolution;
429     colorDepth = clientDisplay.ColorDepth;
430 william 4 System.IntPtr ppBuffer = System.IntPtr.Zero;
431     uint pBytesReturned = 0;
432 william 242 StringBuilder sDisplay = new StringBuilder();
433 william 4 if (WTSQuerySessionInformation(
434     server,
435     sessionID,
436     WTSInfoClass.WTSClientDisplay,
437     out ppBuffer,
438     out pBytesReturned))
439     {
440 william 242 clientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(ppBuffer, typeof(WTS_CLIENT_DISPLAY));
441     horizontalResolution = clientDisplay.HorizontalResolution;
442     verticalResolution = clientDisplay.VerticalResolution;
443     colorDepth = clientDisplay.ColorDepth;
444 william 4 }
445 william 242 LogDebugVerboseMessage("GetTSClientDisplay::server.ToInt32()={0}", server.ToInt32());
446     LogDebugVerboseMessage("GetTSClientDisplay::sessionID={0}", sessionID);
447 william 230 LogDebugVerboseMessage("GetTSClientDisplay::horizontalResolution={0}", horizontalResolution);
448     LogDebugVerboseMessage("GetTSClientDisplay::verticalResolution={0}", verticalResolution);
449     LogDebugVerboseMessage("GetTSClientDisplay::colorDepth={0}", colorDepth);
450 william 4 WTSFreeMemory(ppBuffer);
451     }
452    
453    
454     // Get Mac from IP, using ARP
455     private static string GetMACFromIP(string ipString)
456     {
457    
458     IPAddress ip = IPAddress.Parse(ipString); // Actual IP
459     int rv;
460     string macStr;
461     byte[] mac = new byte[6];
462     int maclen = mac.Length;
463    
464     rv = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, mac, ref maclen);
465     if (rv == 0) // If not 0, error
466     {
467     // macStr = BitConverter.ToString(mac, 0, 6);
468     macStr = BitConverter.ToString(mac, 0, 1)+BitConverter.ToString(mac, 1, 1)+BitConverter.ToString(mac, 2, 1)
469     +BitConverter.ToString(mac, 3, 1)+BitConverter.ToString(mac, 4, 1)+BitConverter.ToString(mac, 5, 1);
470     }
471     else
472     {
473     macStr = "";
474     }
475     return macStr;
476     }
477    
478    
479 william 231 private static string GetTSUserName(int sessionID, IntPtr server)
480 william 4 {
481     System.IntPtr ppBuffer = System.IntPtr.Zero;
482     uint pBytesReturned = 0;
483     string currentUserName = "";
484    
485     if (WTSQuerySessionInformation(
486     server,
487     sessionID,
488     WTSInfoClass.WTSUserName,
489     out ppBuffer,
490     out pBytesReturned))
491     {
492     currentUserName = Marshal.PtrToStringAnsi(ppBuffer);
493     }
494    
495     WTSFreeMemory(ppBuffer);
496 william 230 LogDebugVerboseMessage("GetTSUserName::currentUserName={0}", currentUserName);
497 william 4 return currentUserName;
498     }
499    
500 william 231 public static string GetTSClientName(int sessionID, IntPtr server)
501 william 4 {
502     string clientName = "";
503 william 189 if (!System.Windows.Forms.SystemInformation.TerminalServerSession)
504     {
505     System.IntPtr ppBuffer = System.IntPtr.Zero;
506     int adrBuffer;
507     uint pBytesReturned = 0;
508     if (WTSQuerySessionInformation(
509     server,
510     sessionID,
511     WTSInfoClass.WTSClientName,
512     out ppBuffer,
513     out pBytesReturned))
514     {
515     adrBuffer = (int)ppBuffer;
516     clientName = Marshal.PtrToStringAnsi((System.IntPtr)adrBuffer);
517     }
518 william 4
519 william 189 WTSFreeMemory(ppBuffer);
520     }
521     else
522 william 4 {
523 william 189 clientName = System.Environment.MachineName;
524 william 4 }
525 william 230 LogDebugVerboseMessage("GetTSClientName::clientName={0}", clientName);
526 william 4 return clientName;
527     }
528    
529     // List all sessions on all registered terminal servers and return in a list with selected data for each session.
530     public static List<TS_SESSION_INFO> ListSessions()
531     {
532     List<TS_SESSION_INFO> ret = new List<TS_SESSION_INFO>();
533     if (ATSGlobals.terminalServerConfig == 0) // This server is a terminal server
534     {
535     ListSessions(ret, "localhost"); // Add sessions from local server
536     }
537    
538     // Add sessions from other terminal servers
539     atsDataSet.TerminalServerDataTable datatableTerminalServer;
540     datatableTerminalServer = new atsDataSet.TerminalServerDataTable();
541     ProSupport.terminalServerTableAdapter.Fill(datatableTerminalServer);
542     foreach (DataRow row in datatableTerminalServer.Rows)
543     {
544     string strError = "";
545     try
546     {
547     strError = ListSessions(ret, row["Path"].ToString());
548     }
549     catch
550     {
551     //MessageBox.Show("Error: Could not retrieve session data from server '" + row["Path"].ToString() + "'. Error 39092.");
552     }
553     if (strError.Length != 0)
554     {
555     //MessageBox.Show("Warning: " + strError, "AnywhereTS");
556     }
557     }
558     return ret;
559     }
560    
561     // List all sessions on the terminal server and return in a list with selected data for each session.
562     public static List<TS_SESSION_INFO> ListSessions(String serverName)
563     {
564     IntPtr server = IntPtr.Zero;
565     List<TS_SESSION_INFO> ret = new List<TS_SESSION_INFO>();
566     server = OpenServer(serverName);
567    
568     try
569     {
570     IntPtr ppSessionInfo = IntPtr.Zero;
571    
572     Int32 count = 0;
573     Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
574     Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
575     Int32 current = (int)ppSessionInfo;
576     TS_SESSION_INFO CurrentClientInfo;
577    
578     if (retval != 0)
579     {
580     for (int i = 0; i < count; i++) // Iterate through all sessions on the server
581     {
582     WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
583     current += dataSize;
584     CurrentClientInfo.sessionID = si.SessionID;
585     CurrentClientInfo.ipAddress = GetTSClientAddress(si.SessionID, server);
586     CurrentClientInfo.name = GetTSClientName(si.SessionID, server);
587     CurrentClientInfo.username = GetTSUserName(si.SessionID, server);
588     CurrentClientInfo.state = si.State;
589     if (CurrentClientInfo.name != "")
590     {
591     CurrentClientInfo.macAddress = ProSupport.GetMacAddressFromNameAndDatabase(CurrentClientInfo.name);
592     }
593     else
594     {
595     CurrentClientInfo.macAddress = "";
596     }
597    
598     GetTSClientDisplay(
599     si.SessionID,
600     server,
601     out CurrentClientInfo.HorizontalResolution,
602     out CurrentClientInfo.VerticalResolution,
603     out CurrentClientInfo.ColorDepth);
604     ret.Add(CurrentClientInfo);
605     }
606    
607     WTSFreeMemory(ppSessionInfo);
608     }
609     }
610    
611     finally
612     {
613     try
614     {
615     CloseServer(server);
616     }
617 william 46 catch (Exception e)
618 william 4 {
619     MessageBox.Show("Error could not close terminal server connection (63210)");
620 william 46 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
621     {
622     Logging.ATSAdminLog.Error("Error could not close terminal server connection (63210)");
623     }
624 william 4 }
625     }
626     return ret;
627     }
628    
629     // List all sessions on the terminal server and return in a list with selected data for each session.
630     public static List<AtsSession> ListTSsessions(String serverName)
631     {
632     IntPtr server = IntPtr.Zero;
633     List<AtsSession> ret = new List<AtsSession>();
634     server = OpenServer(serverName);
635    
636     try
637     {
638     IntPtr ppSessionInfo = IntPtr.Zero;
639    
640     Int32 count = 0;
641     Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
642     Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
643     Int32 current = (int)ppSessionInfo;
644     AtsSession CurrentSession = new AtsSession();
645    
646     if (retval != 0)
647     {
648     for (int i = 0; i < count; i++) // Iterate through all sessions on the server
649     {
650     WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
651     current += dataSize;
652     CurrentSession.sessionID = si.SessionID;
653     CurrentSession.ipAddress = GetTSClientAddress(si.SessionID, server);
654     CurrentSession.name = GetTSClientName(si.SessionID, server);
655     CurrentSession.username = GetTSUserName(si.SessionID, server);
656     CurrentSession.state = si.State;
657     if (CurrentSession.name != "")
658     {
659     CurrentSession.macAddress = ProSupport.GetMacAddressFromNameAndDatabase(CurrentSession.name);
660     }
661     else
662     {
663     CurrentSession.macAddress = "";
664     }
665    
666     GetTSClientDisplay(
667     si.SessionID,
668     server,
669     out CurrentSession.HorizontalResolution,
670     out CurrentSession.VerticalResolution,
671     out CurrentSession.ColorDepth);
672     ret.Add(CurrentSession);
673     }
674    
675     WTSFreeMemory(ppSessionInfo);
676     }
677     }
678    
679     finally
680     {
681     try
682     {
683     CloseServer(server);
684     }
685 william 46 catch(Exception e)
686 william 4 {
687     MessageBox.Show("Error could not close terminal server connection (63210)");
688 william 46 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
689     {
690     Logging.ATSAdminLog.Error("Error could not close terminal server connection (63210)");
691     }
692 william 4 }
693     }
694     return ret;
695     }
696    
697    
698     // List all sessions on the terminal server and adds them to list with selected data for each session.
699     // Returns error message or "" if everything went ok.
700     public static string ListSessions(List<TS_SESSION_INFO> SessionInfo, String serverName)
701     {
702     IntPtr server = IntPtr.Zero;
703     server = OpenServer(serverName);
704     string strError = "Could not retrieve session data from server '" + serverName + "'"; // Assume something went wrong
705     if (server != IntPtr.Zero)
706     {
707     try
708     {
709     IntPtr ppSessionInfo = IntPtr.Zero;
710    
711     Int32 count = 0;
712     Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
713     Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
714     Int32 current = (int)ppSessionInfo;
715     TS_SESSION_INFO CurrentClientInfo;
716    
717     if (retval != 0)
718     {
719     for (int i = 0; i < count; i++)
720     {
721     WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
722     current += dataSize;
723     CurrentClientInfo.sessionID = si.SessionID;
724     CurrentClientInfo.ipAddress = GetTSClientAddress(si.SessionID, server);
725     CurrentClientInfo.name = GetTSClientName(si.SessionID, server);
726     CurrentClientInfo.username = GetTSUserName(si.SessionID, server);
727     CurrentClientInfo.state = si.State;
728     if (CurrentClientInfo.name != "")
729     {
730     CurrentClientInfo.macAddress = ProSupport.GetMacAddressFromNameAndDatabase(CurrentClientInfo.name);
731     }
732     else
733     {
734     CurrentClientInfo.macAddress = "";
735     }
736     GetTSClientDisplay(
737     si.SessionID,
738     server,
739     out CurrentClientInfo.HorizontalResolution,
740     out CurrentClientInfo.VerticalResolution,
741     out CurrentClientInfo.ColorDepth);
742    
743     SessionInfo.Add(CurrentClientInfo);
744     }
745    
746     WTSFreeMemory(ppSessionInfo);
747     }
748     }
749    
750     finally
751     {
752     try
753     {
754     CloseServer(server);
755     }
756     finally
757     {
758     // Catch exeception
759     }
760     }
761     strError = ""; // Everything went ok
762     } // end if (server != null)
763     return strError;
764     }
765    
766     // List all sessions on the terminal server and adds them to list with selected data for each session.
767     // Returns error message or "" if everything went ok.
768     public static string ListSessions(List<AtsSession> SessionInfo, String serverName)
769     {
770     IntPtr server = IntPtr.Zero;
771     server = OpenServer(serverName);
772     string strError = "Could not retrieve session data from server '" + serverName + "'"; // Assume something went wrong
773     if (server != IntPtr.Zero)
774     {
775     try
776     {
777     IntPtr ppSessionInfo = IntPtr.Zero;
778    
779     Int32 count = 0;
780     Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count);
781     Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
782     Int32 current = (int)ppSessionInfo;
783    
784     if (retval != 0)
785     {
786     for (int i = 0; i < count; i++)
787     {
788     AtsSession currentSession = new AtsSession();
789     WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)current, typeof(WTS_SESSION_INFO));
790     current += dataSize;
791     currentSession.TerminalServerName = serverName;
792     currentSession.sessionID = si.SessionID;
793     currentSession.ipAddress = GetTSClientAddress(si.SessionID, server);
794     currentSession.name = GetTSClientName(si.SessionID, server);
795     currentSession.username = GetTSUserName(si.SessionID, server);
796     currentSession.state = si.State;
797     if (currentSession.name != "")
798     {
799     currentSession.macAddress = ProSupport.GetMacAddressFromNameAndDatabase(currentSession.name);
800     }
801     else
802     {
803     currentSession.macAddress = "";
804     }
805     GetTSClientDisplay(
806     si.SessionID,
807     server,
808     out currentSession.HorizontalResolution,
809     out currentSession.VerticalResolution,
810     out currentSession.ColorDepth);
811    
812     SessionInfo.Add(currentSession);
813     }
814    
815     WTSFreeMemory(ppSessionInfo);
816     }
817     }
818     finally
819     {
820     try
821     {
822     CloseServer(server);
823     }
824     finally
825     {
826     // Catch exeception
827     }
828     }
829     strError = ""; // Everything went ok
830     } // end if (server != null)
831    
832     return strError;
833     }
834    
835     // Return matching session for a macAddress, or null, if the mac address is not used in any session.
836     // Called when a child node selected in the tree view
837     public static AtsSession GetSession(string macAddress)
838     {
839 william 230 LogDebugVerboseMessage("GetSession(string macAddress)::called");
840 william 4 foreach (AtsTerminalServer ts in AtsEnvironment.TerminalServers)
841     {
842 william 230 LogDebugVerboseMessage("GetSession(string macAddress)::ts.networkPath={0}", ts.networkPath == null ? string.Empty : ts.networkPath);
843     LogDebugVerboseMessage("GetSession(string macAddress)::ts.Online={0}", ts.Online);
844     LogDebugVerboseMessage("GetSession(string macAddress)::ts.Session.Count={0}", ts.Session == null ? 0 : ts.Session.Count);
845     LogDebugVerboseMessage("GetSession(string macAddress)::ts.strError={0}", ts.strError == null ? string.Empty : ts.strError);
846 william 4 foreach (AtsSession session in ts.Session)
847     {
848 william 225 int session_index = ts.Session.IndexOf(session);
849    
850 william 230 LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].TerminalServerName={1}", session_index, session.TerminalServerName == null ? string.Empty : session.TerminalServerName);
851     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].state={1}", session_index, StateToString(session.state));
852     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].sessionID={1}", session_index, session.sessionID);
853     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].name={1}", session_index, session.name == null ? string.Empty : session.name);
854     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].username={1}", session_index, session.username == null ? string.Empty : session.username);
855     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].ipAddress={1}", session_index, session.ipAddress == null ? string.Empty : session.ipAddress);
856     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].macAddress={1}", session_index, session.macAddress == null ? string.Empty : session.macAddress);
857     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].HorizontalResolution={1}", session_index, session.HorizontalResolution);
858     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].VerticalResolution={1}", session_index, session.VerticalResolution);
859     LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}].ColorDepth={1}", session_index, session.ColorDepth);
860 william 225
861 william 4 // Try to find the MAC adress for the selected client in any of the sessions.
862     if (session.macAddress == macAddress)
863     {
864 william 230 LogDebugVerboseMessage("GetSession(string macAddress)::session[{0}] found for macAddress={1}", session_index, macAddress);
865 william 4 return session;
866     }
867     }
868     }
869     // Session not found, return null.
870     return null;
871     }
872    
873     // Convert session state to string
874     public static string StateToString(WTS_CONNECTSTATE_CLASS state)
875     {
876     switch (state)
877     {
878     case WTS_CONNECTSTATE_CLASS.WTSActive:
879     return "User logged on";
880     case WTS_CONNECTSTATE_CLASS.WTSConnected:
881     return "Connected to client";
882     case WTS_CONNECTSTATE_CLASS.WTSConnectQuery:
883     return "Connecting to client";
884     case WTS_CONNECTSTATE_CLASS.WTSShadow:
885     return "Shadowing";
886     case WTS_CONNECTSTATE_CLASS.WTSDisconnected:
887     return "Logged on without client";
888     case WTS_CONNECTSTATE_CLASS.WTSIdle:
889     return "Waiting for client to connect";
890     case WTS_CONNECTSTATE_CLASS.WTSListen:
891     return "Listening for connection";
892     case WTS_CONNECTSTATE_CLASS.WTSReset:
893     return "Being reset";
894     case WTS_CONNECTSTATE_CLASS.WTSDown:
895     return "Down due to error";
896     case WTS_CONNECTSTATE_CLASS.WTSInit:
897     return "In initialization";
898     default:
899 william 230 LogDebugVerboseMessage("StateToString(WTS_CONNECTSTATE_CLASS state)::Unknown Session State={0}", state.ToString());
900 william 4 return "Unknown state (error!)";
901     }
902     }
903     public static string GetMyName()
904     {
905 william 225 string name = GetTSClientName(WTS_CURRENT_SESSION, System.IntPtr.Zero);
906 william 230 LogDebugVerboseMessage("GetMyName()::Name={0}", name);
907 william 225 return name;
908 william 4 }
909    
910     } // Class TSManager
911     } // Namespace TerminalServices

  ViewVC Help
Powered by ViewVC 1.1.22