|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +using NtApiDotNet.Win32; |
15 | 16 | using System; |
16 | 17 | using System.Collections.Generic; |
17 | 18 | using System.Runtime.InteropServices; |
@@ -140,6 +141,43 @@ public static NtWindowStation Open(string winsta_name) |
140 | 141 | return Open(winsta_name, null); |
141 | 142 | } |
142 | 143 |
|
| 144 | + [UnmanagedFunctionPointer(CallingConvention.StdCall)] |
| 145 | + private delegate IntPtr GetKbdLayout(); |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Create a Window Station by name. |
| 149 | + /// </summary> |
| 150 | + /// <param name="winsta_name">The name of the Window Station.</param> |
| 151 | + /// <returns>The Window Station.</returns> |
| 152 | + public static NtWindowStation Create(string winsta_name) |
| 153 | + { |
| 154 | + string dll_path; |
| 155 | + IntPtr layout_offset; |
| 156 | + using (var kbd_dll = SafeLoadLibraryHandle.LoadLibrary(@"kbdus.dll")) |
| 157 | + { |
| 158 | + dll_path = kbd_dll.FullPath; |
| 159 | + var proc = kbd_dll.GetProcAddress(new IntPtr(1)); |
| 160 | + GetKbdLayout kbdLayout = (GetKbdLayout)Marshal.GetDelegateForFunctionPointer(proc, typeof(GetKbdLayout)); |
| 161 | + var layout = kbdLayout(); |
| 162 | + layout_offset = new IntPtr(layout.ToInt64() - kbd_dll.DangerousGetHandle().ToInt64()); |
| 163 | + } |
| 164 | + |
| 165 | + using (var buffer = new SafeHGlobalBuffer(0x318)) |
| 166 | + { |
| 167 | + BufferUtils.FillBuffer(buffer, 0); |
| 168 | + using (var file = NtFile.Open(NtFileUtils.DosFileNameToNt(dll_path), null, |
| 169 | + FileAccessRights.GenericRead | FileAccessRights.Synchronize, FileShareMode.Read | FileShareMode.Delete, |
| 170 | + FileOpenOptions.NonDirectoryFile | FileOpenOptions.SynchronousIoNonAlert)) |
| 171 | + { |
| 172 | + using (var obja = new ObjectAttributes(winsta_name, AttributeFlags.CaseInsensitive)) |
| 173 | + { |
| 174 | + return new NtWindowStation(NtSystemCalls.NtUserCreateWindowStation(obja, WindowStationAccessRights.MaximumAllowed, file.Handle, |
| 175 | + layout_offset, IntPtr.Zero, buffer, new UnicodeString("00000409"), 0x04090409)); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
143 | 181 | /// <summary> |
144 | 182 | /// Get a list of desktops for this Window Station. |
145 | 183 | /// </summary> |
|
0 commit comments