|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using Microsoft.Win32.SafeHandles; |
5 | 6 | using System.ComponentModel; |
6 | 7 | using System.Runtime.InteropServices; |
7 | 8 | using System.Security.Principal; |
8 | 9 |
|
9 | | -namespace System.Diagnostics.Tests |
| 10 | +internal static partial class Interop |
10 | 11 | { |
11 | | - internal static partial class Interop |
| 12 | + [StructLayout(LayoutKind.Sequential, Size = 40)] |
| 13 | + public struct PROCESS_MEMORY_COUNTERS |
12 | 14 | { |
13 | | - [StructLayout(LayoutKind.Sequential, Size = 40)] |
14 | | - public struct PROCESS_MEMORY_COUNTERS |
15 | | - { |
16 | | - public uint cb; |
17 | | - public uint PageFaultCount; |
18 | | - public uint PeakWorkingSetSize; |
19 | | - public uint WorkingSetSize; |
20 | | - public uint QuotaPeakPagedPoolUsage; |
21 | | - public uint QuotaPagedPoolUsage; |
22 | | - public uint QuotaPeakNonPagedPoolUsage; |
23 | | - public uint QuotaNonPagedPoolUsage; |
24 | | - public uint PagefileUsage; |
25 | | - public uint PeakPagefileUsage; |
26 | | - } |
| 15 | + public uint cb; |
| 16 | + public uint PageFaultCount; |
| 17 | + public uint PeakWorkingSetSize; |
| 18 | + public uint WorkingSetSize; |
| 19 | + public uint QuotaPeakPagedPoolUsage; |
| 20 | + public uint QuotaPagedPoolUsage; |
| 21 | + public uint QuotaPeakNonPagedPoolUsage; |
| 22 | + public uint QuotaNonPagedPoolUsage; |
| 23 | + public uint PagefileUsage; |
| 24 | + public uint PeakPagefileUsage; |
| 25 | + } |
27 | 26 |
|
28 | | - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
29 | | - internal struct USER_INFO_1 |
30 | | - { |
31 | | - public string usri1_name; |
32 | | - public string usri1_password; |
33 | | - public uint usri1_password_age; |
34 | | - public uint usri1_priv; |
35 | | - public string usri1_home_dir; |
36 | | - public string usri1_comment; |
37 | | - public uint usri1_flags; |
38 | | - public string usri1_script_path; |
39 | | - } |
| 27 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
| 28 | + internal struct USER_INFO_1 |
| 29 | + { |
| 30 | + public string usri1_name; |
| 31 | + public string usri1_password; |
| 32 | + public uint usri1_password_age; |
| 33 | + public uint usri1_priv; |
| 34 | + public string usri1_home_dir; |
| 35 | + public string usri1_comment; |
| 36 | + public uint usri1_flags; |
| 37 | + public string usri1_script_path; |
| 38 | + } |
40 | 39 |
|
41 | | - [StructLayout(LayoutKind.Sequential)] |
42 | | - public struct TOKEN_USER |
43 | | - { |
44 | | - public SID_AND_ATTRIBUTES User; |
45 | | - } |
| 40 | + [StructLayout(LayoutKind.Sequential)] |
| 41 | + public struct TOKEN_USER |
| 42 | + { |
| 43 | + public SID_AND_ATTRIBUTES User; |
| 44 | + } |
46 | 45 |
|
47 | | - [StructLayout(LayoutKind.Sequential)] |
48 | | - public struct SID_AND_ATTRIBUTES |
49 | | - { |
50 | | - public IntPtr Sid; |
51 | | - public int Attributes; |
52 | | - } |
| 46 | + [StructLayout(LayoutKind.Sequential)] |
| 47 | + public struct SID_AND_ATTRIBUTES |
| 48 | + { |
| 49 | + public IntPtr Sid; |
| 50 | + public int Attributes; |
| 51 | + } |
53 | 52 |
|
54 | | - [DllImport("kernel32.dll")] |
55 | | - public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags); |
| 53 | + [DllImport("kernel32.dll")] |
| 54 | + public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags); |
56 | 55 |
|
57 | | - [DllImport("kernel32.dll")] |
58 | | - internal static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId); |
| 56 | + [DllImport("kernel32.dll")] |
| 57 | + internal static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId); |
59 | 58 |
|
60 | | - [DllImport("kernel32.dll")] |
61 | | - public static extern int GetProcessId(SafeProcessHandle nativeHandle); |
| 59 | + [DllImport("kernel32.dll")] |
| 60 | + public static extern int GetProcessId(SafeProcessHandle nativeHandle); |
62 | 61 |
|
63 | | - [DllImport("kernel32.dll")] |
64 | | - internal static extern int GetConsoleCP(); |
| 62 | + [DllImport("kernel32.dll")] |
| 63 | + internal static extern int GetConsoleCP(); |
65 | 64 |
|
66 | | - [DllImport("kernel32.dll")] |
67 | | - internal static extern int GetConsoleOutputCP(); |
| 65 | + [DllImport("kernel32.dll")] |
| 66 | + internal static extern int GetConsoleOutputCP(); |
68 | 67 |
|
69 | | - [DllImport("kernel32.dll")] |
70 | | - internal static extern int SetConsoleCP(int codePage); |
| 68 | + [DllImport("kernel32.dll")] |
| 69 | + internal static extern int SetConsoleCP(int codePage); |
71 | 70 |
|
72 | | - [DllImport("kernel32.dll")] |
73 | | - internal static extern int SetConsoleOutputCP(int codePage); |
| 71 | + [DllImport("kernel32.dll")] |
| 72 | + internal static extern int SetConsoleOutputCP(int codePage); |
74 | 73 |
|
75 | | - [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
76 | | - internal static extern uint NetUserAdd(string servername, uint level, ref USER_INFO_1 buf, out uint parm_err); |
| 74 | + [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| 75 | + internal static extern uint NetUserAdd(string servername, uint level, ref USER_INFO_1 buf, out uint parm_err); |
77 | 76 |
|
78 | | - [DllImport("netapi32.dll", CharSet = CharSet.Unicode)] |
79 | | - internal static extern uint NetUserDel(string servername, string username); |
| 77 | + [DllImport("netapi32.dll", CharSet = CharSet.Unicode)] |
| 78 | + internal static extern uint NetUserDel(string servername, string username); |
80 | 79 |
|
81 | | - [DllImport("advapi32.dll")] |
82 | | - internal static extern bool OpenProcessToken(SafeProcessHandle ProcessHandle, uint DesiredAccess, out SafeProcessHandle TokenHandle); |
| 80 | + [DllImport("advapi32.dll")] |
| 81 | + internal static extern bool OpenProcessToken(SafeProcessHandle ProcessHandle, uint DesiredAccess, out SafeProcessHandle TokenHandle); |
83 | 82 |
|
84 | | - [DllImport("advapi32.dll")] |
85 | | - internal static extern bool GetTokenInformation(SafeProcessHandle TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, int TokenInformationLength, ref int ReturnLength); |
| 83 | + [DllImport("advapi32.dll")] |
| 84 | + internal static extern bool GetTokenInformation(SafeProcessHandle TokenHandle, uint TokenInformationClass, IntPtr TokenInformation, int TokenInformationLength, ref int ReturnLength); |
86 | 85 |
|
87 | | - [DllImport("shell32.dll")] |
88 | | - internal static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); |
| 86 | + [DllImport("shell32.dll")] |
| 87 | + internal static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); |
89 | 88 |
|
90 | | - internal static void NetUserAdd(string username, string password) |
91 | | - { |
92 | | - USER_INFO_1 userInfo = new USER_INFO_1(); |
93 | | - userInfo.usri1_name = username; |
94 | | - userInfo.usri1_password = password; |
95 | | - userInfo.usri1_priv = 1; |
| 89 | + internal static void NetUserAdd(string username, string password) |
| 90 | + { |
| 91 | + USER_INFO_1 userInfo = new USER_INFO_1(); |
| 92 | + userInfo.usri1_name = username; |
| 93 | + userInfo.usri1_password = password; |
| 94 | + userInfo.usri1_priv = 1; |
96 | 95 |
|
97 | | - uint parm_err; |
98 | | - uint result = NetUserAdd(null, 1, ref userInfo, out parm_err); |
| 96 | + uint parm_err; |
| 97 | + uint result = NetUserAdd(null, 1, ref userInfo, out parm_err); |
99 | 98 |
|
100 | | - if (result != ExitCodes.NERR_Success) |
101 | | - { |
102 | | - // most likely result == ERROR_ACCESS_DENIED |
103 | | - // due to running without elevated privileges |
104 | | - throw new Win32Exception((int)result); |
105 | | - } |
| 99 | + if (result != ExitCodes.NERR_Success) |
| 100 | + { |
| 101 | + // most likely result == ERROR_ACCESS_DENIED |
| 102 | + // due to running without elevated privileges |
| 103 | + throw new Win32Exception((int)result); |
106 | 104 | } |
| 105 | + } |
107 | 106 |
|
108 | | - internal static bool ProcessTokenToSid(SafeProcessHandle token, out SecurityIdentifier sid) |
| 107 | + internal static bool ProcessTokenToSid(SafeProcessHandle token, out SecurityIdentifier sid) |
| 108 | + { |
| 109 | + bool ret = false; |
| 110 | + sid = null; |
| 111 | + IntPtr tu = IntPtr.Zero; |
| 112 | + try |
109 | 113 | { |
110 | | - bool ret = false; |
111 | | - sid = null; |
112 | | - IntPtr tu = IntPtr.Zero; |
113 | | - try |
114 | | - { |
115 | | - TOKEN_USER tokUser; |
116 | | - const int bufLength = 256; |
117 | | - |
118 | | - tu = Marshal.AllocHGlobal(bufLength); |
119 | | - int cb = bufLength; |
120 | | - ret = GetTokenInformation(token, 1, tu, cb, ref cb); |
121 | | - if (ret) |
122 | | - { |
123 | | - tokUser = Marshal.PtrToStructure<TOKEN_USER>(tu); |
124 | | - sid = new SecurityIdentifier(tokUser.User.Sid); |
125 | | - } |
126 | | - return ret; |
127 | | - } |
128 | | - catch (Exception) |
129 | | - { |
130 | | - return false; |
131 | | - } |
132 | | - finally |
| 114 | + TOKEN_USER tokUser; |
| 115 | + const int bufLength = 256; |
| 116 | + |
| 117 | + tu = Marshal.AllocHGlobal(bufLength); |
| 118 | + int cb = bufLength; |
| 119 | + ret = GetTokenInformation(token, 1, tu, cb, ref cb); |
| 120 | + if (ret) |
133 | 121 | { |
134 | | - if (tu != IntPtr.Zero) |
135 | | - Marshal.FreeHGlobal(tu); |
| 122 | + tokUser = Marshal.PtrToStructure<TOKEN_USER>(tu); |
| 123 | + sid = new SecurityIdentifier(tokUser.User.Sid); |
136 | 124 | } |
| 125 | + return ret; |
137 | 126 | } |
138 | | - |
139 | | - internal static class ExitCodes |
| 127 | + catch (Exception) |
| 128 | + { |
| 129 | + return false; |
| 130 | + } |
| 131 | + finally |
140 | 132 | { |
141 | | - internal const uint NERR_Success = 0; |
142 | | - internal const uint NERR_UserNotFound = 2221; |
| 133 | + if (tu != IntPtr.Zero) |
| 134 | + Marshal.FreeHGlobal(tu); |
143 | 135 | } |
144 | 136 | } |
| 137 | + |
| 138 | + internal static class ExitCodes |
| 139 | + { |
| 140 | + internal const uint NERR_Success = 0; |
| 141 | + internal const uint NERR_UserNotFound = 2221; |
| 142 | + } |
145 | 143 | } |
0 commit comments