ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/Wts.cs
Revision: 718
Committed: 2006-03-30T02:50:17-08:00 (19 years, 3 months ago) by douglas
File size: 3630 byte(s)
Log Message:
Getting somewhere!

File Contents

# User Rev Content
1 douglas 710 // Douglas Thrift
2     //
3     // CCS Computer Science
4     //
5     // Win32 Terminal Services Wrapper
6    
7     using System;
8     using System.Runtime.InteropServices;
9    
10     public static class Wts
11     {
12 douglas 718 public enum WTS_PROTOCOL_TYPE : ushort
13     {
14     WTS_PROTOCOL_TYPE_CONSOLE,
15     WTS_PROTOCOL_TYPE_ICA,
16     WTS_PROTOCOL_TYPE_RDP
17     }
18    
19 douglas 710 public enum WTS_CONNECTSTATE_CLASS
20     {
21     WTSActive,
22     WTSConnected,
23     WTSConnectQuery,
24     WTSShadow,
25     WTSDisconnected,
26     WTSIdle,
27     WTSListen,
28     WTSReset,
29     WTSDown,
30     WTSInit
31     }
32    
33 douglas 718 public enum WTS_INFO_CLASS : int
34 douglas 710 {
35     WTSInitialProgram,
36     WTSApplicationName,
37     WTSWorkingDirectory,
38     WTSOEMId,
39     WTSSessionId,
40     WTSUserName,
41     WTSWinStationName,
42     WTSDomainName,
43     WTSConnectState,
44     WTSClientBuildNumber,
45     WTSClientName,
46     WTSClientDirectory,
47     WTSClientProductId,
48     WTSClientHardwareId,
49     WTSClientAddress,
50     WTSClientDisplay,
51     WTSClientProtocolType
52     }
53    
54     [StructLayout(LayoutKind.Sequential)]
55     public class WTS_SESSION_INFO
56     {
57     public uint SessionId;
58     [MarshalAs(UnmanagedType.LPWStr)]
59     public string pWinStationName;
60     public WTS_CONNECTSTATE_CLASS State;
61     }
62    
63     [DllImport("Wtsapi32.dll", EntryPoint = "WTSEnumerateSessionsW")]
64 douglas 718 public static extern bool EnumerateSessions(IntPtr hServer, uint reserved, uint Version, out IntPtr ppSessionInfo, out uint pCount);
65 douglas 710
66 douglas 718 public static bool EnumerateSessions(IntPtr hServer, uint reserved, uint Version, out WTS_SESSION_INFO[] ppSessionInfo, out uint pCount)
67     {
68     IntPtr pointer;
69     bool value = EnumerateSessions(hServer, reserved, Version, out pointer, out pCount);
70 douglas 710
71 douglas 718 ppSessionInfo = new WTS_SESSION_INFO[pCount];
72    
73     for (long index = 0; index != pCount; ++index)
74     {
75     IntPtr pointer_ = new IntPtr(pointer.ToInt64() + Marshal.SizeOf(typeof(WTS_SESSION_INFO)) * index);
76    
77     ppSessionInfo[index] = (WTS_SESSION_INFO)Marshal.PtrToStructure(pointer_, typeof(WTS_SESSION_INFO));
78     }
79    
80     FreeMemory(pointer);
81    
82     return value;
83     }
84    
85 douglas 710 [DllImport("Wtsapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "WTSFreeMemory")]
86 douglas 718 public static extern void FreeMemory(IntPtr pMemory);
87 douglas 710
88     [DllImport("Wtsapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "WTSQuerySessionInformationW")]
89 douglas 718 public static extern bool QuerySessionInformation(IntPtr hServer, uint SessionId, WTS_INFO_CLASS WTSInfoClass, out IntPtr ppBuffer, out uint pBytesReturned);
90    
91     public static bool QuerySessionInformation(IntPtr hServer, uint SessionId, WTS_INFO_CLASS WTSInfoClass, out string ppBuffer, out uint pBytesRerturned)
92     {
93     IntPtr pointer;
94     bool value = QuerySessionInformation(hServer, SessionId, WTSInfoClass, out pointer, out pBytesRerturned);
95    
96     if (pointer != IntPtr.Zero)
97     ppBuffer = Marshal.PtrToStringUni(pointer);
98     else
99     ppBuffer = null;
100    
101     FreeMemory(pointer);
102    
103     return value;
104     }
105    
106     public static bool QuerySessionInformation(IntPtr hServer, uint SessionId, WTS_INFO_CLASS WTSInfoClass, out WTS_PROTOCOL_TYPE ppBuffer, out uint pBytesReturned)
107     {
108     IntPtr pointer;
109     bool value = QuerySessionInformation(hServer, SessionId, WTSInfoClass, out pointer, out pBytesReturned);
110    
111     ppBuffer = (WTS_PROTOCOL_TYPE)Marshal.ReadInt16(pointer);
112    
113     FreeMemory(pointer);
114    
115     return value;
116     }
117    
118     public static bool QuerySessionInformation(IntPtr hServer, uint SessionId, WTS_INFO_CLASS WTSInfoClass, out WTS_CONNECTSTATE_CLASS ppBuffer, out uint pBytesReturned)
119     {
120     IntPtr pointer;
121     bool value = QuerySessionInformation(hServer, SessionId, WTSInfoClass, out pointer, out pBytesReturned);
122    
123     ppBuffer = (WTS_CONNECTSTATE_CLASS)Marshal.ReadInt32(pointer);
124    
125     FreeMemory(pointer);
126    
127     return value;
128     }
129 douglas 710 }