//****************************************************************************************************** // WindowsApi.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 01/24/2006 - J. Ritchie Carroll // Initial version of source created. // 09/10/2008 - J. Ritchie Carroll // Converted to C#. // 08/05/2009 - Josh L. Patterson // Edited Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 02/04/2011 - J. Ritchie Carroll // Added WindowsAPI methods used to define automation of service failure actions. // 02/20/2011 - J. Ritchie Carroll // Added WindowsAPI methods used to help with UAC (User Account Control). // 09/21/2011 - J. Ritchie Carroll // Excluded class from Mono deployments due to P/Invoke requirements. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles; #pragma warning disable 1591 namespace GSF.Interop { /// /// Defines common Windows API constants, enumerations, structures and functions. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "lp")] [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible")] [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#")] public static class WindowsApi { /// /// Use the standard logon provider for the system. The default security provider is NTLM. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "LOGON")] [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int LOGON32_PROVIDER_DEFAULT = 0; /// /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on by a terminal server, /// remote shell, or similar process. This logon type has the additional expense of caching logon information for disconnected operations; /// therefore, it is inappropriate for some client/server applications, such as a mail server. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "LOGON")] [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int LOGON32_LOGON_INTERACTIVE = 2; /// /// This logon type is intended for high performance servers to authenticate plaintext passwords. The LogonUserEx function does not cache /// credentials for this logon type. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "LOGON")] [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int LOGON32_LOGON_NETWORK = 3; /// /// Impersonate a client at the impersonation level. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int SECURITY_IMPERSONATION = 2; /// /// Access is denied. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int ERROR_ACCESS_DENIED = 5; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int CRYPT_OID_INFO_OID_KEY = 1; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int CRYPT_OID_INFO_NAME_KEY = 2; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint CRYPT_OID_DISABLE_SEARCH_DS_FLAG = 0x80000000u; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const int CRYPT_INSTALL_OID_INFO_BEFORE_FLAG = 1; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SERVICE_LOGON_NAME = "SeServiceLogonRight"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_AUDIT_NAME = "SeAuditPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_BACKUP_NAME = "SeBackupPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CREATE_GLOBAL_NAME = "SeCreateGlobalPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CREATE_SYMBOLIC_LINK_NAME = "SeCreateSymbolicLinkPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_DEBUG_NAME = "SeDebugPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_ENABLE_DELEGATION_NAME = "SeEnableDelegationPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_IMPERSONATE_NAME = "SeImpersonatePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_INC_WORKING_SET_NAME = "SeIncreaseWorkingSetPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_RELABEL_NAME = "SeRelabelPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_RESTORE_NAME = "SeRestorePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SECURITY_NAME = "SeSecurityPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SYNC_AGENT_NAME = "SeSyncAgentPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_TCB_NAME = "SeTcbPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_TIME_ZONE_NAME = "SeTimeZonePrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_TRUSTED_CREDMAN_ACCESS_NAME = "SeTrustedCredManAccessPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_UNDOCK_NAME = "SeUndockPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const string SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint SE_PRIVILEGE_ENABLED = 0x00000002; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint SE_PRIVILEGE_REMOVED = 0x00000004; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000; // Use these for DesiredAccess [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint STANDARD_RIGHTS_READ = 0x00020000; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ASSIGN_PRIMARY = 0x0001; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_DUPLICATE = 0x0002; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_IMPERSONATE = 0x0004; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_QUERY = 0x0008; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_QUERY_SOURCE = 0x0010; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ADJUST_PRIVILEGES = 0x0020; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ADJUST_GROUPS = 0x0040; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ADJUST_DEFAULT = 0x0080; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ADJUST_SESSIONID = 0x0100; [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY); [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public const uint TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID); /// /// Win32 TOKEN_INFORMATION_CLASS enumeration. /// [SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public enum TOKEN_INFORMATION_CLASS { /// /// The buffer receives a TOKEN_USER structure that contains the user account of the token. /// TokenUser = 1, /// /// The buffer receives a TOKEN_GROUPS structure that contains the group accounts associated with the token. /// TokenGroups, /// /// The buffer receives a TOKEN_PRIVILEGES structure that contains the privileges of the token. /// TokenPrivileges, /// /// The buffer receives a TOKEN_OWNER structure that contains the default owner security identifier (SID) for newly created objects. /// TokenOwner, /// /// The buffer receives a TOKEN_PRIMARY_GROUP structure that contains the default primary group SID for newly created objects. /// TokenPrimaryGroup, /// /// The buffer receives a TOKEN_DEFAULT_DACL structure that contains the default DACL for newly created objects. /// TokenDefaultDacl, /// /// The buffer receives a TOKEN_SOURCE structure that contains the source of the token. TOKEN_QUERY_SOURCE access is needed to retrieve this information. /// TokenSource, /// /// The buffer receives a TOKEN_TYPE value that indicates whether the token is a primary or impersonation token. /// TokenType, /// /// The buffer receives a SECURITY_IMPERSONATION_LEVEL value that indicates the impersonation level of the token. If the access token is not an impersonation token, the function fails. /// TokenImpersonationLevel, /// /// The buffer receives a TOKEN_STATISTICS structure that contains various token statistics. /// TokenStatistics, /// /// The buffer receives a TOKEN_GROUPS structure that contains the list of restricting SIDs in a restricted token. /// TokenRestrictedSids, /// /// The buffer receives a DWORD value that indicates the Terminal Services session identifier that is associated with the token. /// TokenSessionId, /// /// The buffer receives a TOKEN_GROUPS_AND_PRIVILEGES structure that contains the user SID, the group accounts, the restricted SIDs, and the authentication ID associated with the token. /// TokenGroupsAndPrivileges, /// /// Reserved. /// TokenSessionReference, /// /// The buffer receives a DWORD value that is nonzero if the token includes the SANDBOX_INERT flag. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "SandBox")] TokenSandBoxInert, /// /// Reserved. /// TokenAuditPolicy, /// /// The buffer receives a TOKEN_ORIGIN value. /// TokenOrigin, /// /// The buffer receives a TOKEN_ELEVATION_TYPE value that specifies the elevation level of the token. /// TokenElevationType, /// /// The buffer receives a TOKEN_LINKED_TOKEN structure that contains a handle to another token that is linked to this token. /// TokenLinkedToken, /// /// The buffer receives a TOKEN_ELEVATION structure that specifies whether the token is elevated. /// TokenElevation, /// /// The buffer receives a DWORD value that is nonzero if the token has ever been filtered. /// TokenHasRestrictions, /// /// The buffer receives a TOKEN_ACCESS_INFORMATION structure that specifies security information contained in the token. /// TokenAccessInformation, /// /// The buffer receives a DWORD value that is nonzero if virtualization is allowed for the token. /// TokenVirtualizationAllowed, /// /// The buffer receives a DWORD value that is nonzero if virtualization is enabled for the token. /// TokenVirtualizationEnabled, /// /// The buffer receives a TOKEN_MANDATORY_LABEL structure that specifies the token's integrity level. /// TokenIntegrityLevel, /// /// The buffer receives a DWORD value that is nonzero if the token has the UIAccess flag set. /// TokenUIAccess, /// /// The buffer receives a TOKEN_MANDATORY_POLICY structure that specifies the token's mandatory integrity policy. /// TokenMandatoryPolicy, /// /// The buffer receives the token's logon security identifier (SID). /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Logon")] TokenLogonSid, /// /// The maximum value for this enumeration /// MaxTokenInfoClass } /// /// Win32 ProcessAccessTypes enumeration. /// [SuppressMessage("Microsoft.Usage", "CA2217:DoNotMarkEnumsWithFlags"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), Flags] public enum ProcessAccessTypes : uint { /// Enables usage of the process handle in the TerminateProcess function to terminate the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_TERMINATE = 0x00000001, /// /// Enables usage of the process handle in the CreateRemoteThread function to create a thread in the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_CREATE_THREAD = 0x00000002, /// /// Sets the session ID. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_SET_SESSIONID = 0x00000004, /// /// Enables usage of the process handle in the VirtualProtectEx and WriteProcessMemory functions to modify the virtual memory of the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_VM_OPERATION = 0x00000008, /// /// Enables usage of the process handle in the ReadProcessMemory function to read from the virtual memory of the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_VM_READ = 0x00000010, /// /// Enables usage of the process handle in the WriteProcessMemory function to write to the virtual memory of the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_VM_WRITE = 0x00000020, /// /// Enables usage of the process handle as either the source or target process in the DuplicateHandle function to duplicate a handle. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_DUP_HANDLE = 0x00000040, /// /// Creates a process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_CREATE_PROCESS = 0x00000080, /// /// Sets quota. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_SET_QUOTA = 0x00000100, /// /// Enables usage of the process handle in the SetPriorityClass function to set the priority class of the process. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_SET_INFORMATION = 0x00000200, /// /// Enables usage of the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_QUERY_INFORMATION = 0x00000400, /// /// Standard rights required. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] STANDARD_RIGHTS_REQUIRED = 0x000F0000, /// /// Enables usage of the process handle in any of the wait functions to wait for the process to terminate. /// SYNCHRONIZE = 0x00100000, /// /// Specifies all possible access flags for the process object. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] PROCESS_ALL_ACCESS = PROCESS_TERMINATE | PROCESS_CREATE_THREAD | PROCESS_SET_SESSIONID | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_DUP_HANDLE | PROCESS_CREATE_PROCESS | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION | PROCESS_QUERY_INFORMATION | STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE } /// /// Win32 ShutdownReason enumeration. /// [SuppressMessage("Microsoft.Usage", "CA2217:DoNotMarkEnumsWithFlags"), SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Naming", "CA1714:FlagsEnumsShouldHavePluralNames"), Flags] public enum ShutdownReason : uint { // Microsoft major reasons. [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_OTHER = 0x00000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_NONE = 0x00000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_HARDWARE = 0x00010000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_OPERATINGSYSTEM = 0x00020000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_SOFTWARE = 0x00030000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_APPLICATION = 0x00040000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_SYSTEM = 0x00050000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_POWER = 0x00060000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MAJOR_LEGACY_API = 0x00070000, // Microsoft minor reasons. [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_OTHER = 0x00000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_NONE = 0x000000ff, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_MAINTENANCE = 0x00000001, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_INSTALLATION = 0x00000002, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_UPGRADE = 0x00000003, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_RECONFIG = 0x00000004, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_HUNG = 0x00000005, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_UNSTABLE = 0x00000006, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_DISK = 0x00000007, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_PROCESSOR = 0x00000008, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_NETWORKCARD = 0x00000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_POWER_SUPPLY = 0x0000000a, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_CORDUNPLUGGED = 0x0000000b, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_ENVIRONMENT = 0x0000000c, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_HARDWARE_DRIVER = 0x0000000d, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_OTHERDRIVER = 0x0000000e, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_BLUESCREEN = 0x0000000F, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_SERVICEPACK = 0x00000010, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_HOTFIX = 0x00000011, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_SECURITYFIX = 0x00000012, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_SECURITY = 0x00000013, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_NETWORK_CONNECTIVITY = 0x00000014, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_WMI = 0x00000015, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_SERVICEPACK_UNINSTALL = 0x00000016, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_HOTFIX_UNINSTALL = 0x00000017, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_SECURITYFIX_UNINSTALL = 0x00000018, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_MMC = 0x00000019, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_MINOR_TERMSRV = 0x00000020, // Flags that end up in the event log code. [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_FLAG_USER_DEFINED = 0x40000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_FLAG_PLANNED = 0x80000000, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_UNKNOWN = SHTDN_REASON_MINOR_NONE, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_LEGACY_API = (SHTDN_REASON_MAJOR_LEGACY_API | SHTDN_REASON_FLAG_PLANNED), // This mask cuts out UI flags. [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SHTDN_REASON_VALID_BIT_MASK = 0xc0ffffff } /// /// Win32 SECURITY_IMPERSONATION_LEVEL enumeration. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public enum SECURITY_IMPERSONATION_LEVEL { SecurityAnonymous, SecurityIdentification, SecurityImpersonation, SecurityDelegation } /// /// Win32 TOKEN_TYPE enumeration. /// [SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] public enum TOKEN_TYPE { TokenPrimary = 1, TokenImpersonation } /// /// Win32 TOKEN_ELEVATION_TYPE enumeration. /// /// /// Enumeration indicates the elevation type of token being queried by the GetTokenInformation function or set by the SetTokenInformation function. /// [SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public enum TOKEN_ELEVATION_TYPE { /// /// The token does not have a linked token. /// TokenElevationTypeDefault = 1, /// /// The token is an elevated token. /// TokenElevationTypeFull, /// /// The token is a limited token. /// TokenElevationTypeLimited } /// /// Win32 SC_ACTION_TYPE enumeration. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public enum SC_ACTION_TYPE : uint { /// /// No action. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SC_ACTION_NONE = 0x00000000, /// /// Restart the service. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SC_ACTION_RESTART = 0x00000001, /// /// Reboot the computer. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SC_ACTION_REBOOT = 0x00000002, /// /// Run a command. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] SC_ACTION_RUN_COMMAND = 0x00000003 } /// /// Win32 CreationFlags enumeration. /// [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), Flags] public enum CreationFlags { [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_SUSPENDED = 0x00000004, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_NEW_CONSOLE = 0x00000010, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_NEW_PROCESS_GROUP = 0x00000200, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_UNICODE_ENVIRONMENT = 0x00000400, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_SEPARATE_WOW_VDM = 0x00000800, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] CREATE_DEFAULT_ERROR_MODE = 0x04000000, } /// /// Win32 LogonFlags enumeration. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Logon"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), Flags] public enum LogonFlags { /// /// Log on, then load the user's profile in the HKEY_USERS registry key. The function /// returns after the profile has been loaded. Loading the profile can be time-consuming, /// so it is best to use this value only if you must access the information in the /// HKEY_CURRENT_USER registry key. /// NOTE: Windows Server 2003: The profile is unloaded after the new process has been /// terminated, regardless of whether it has created child processes. /// /// See LOGON_WITH_PROFILE WithProfile = 1, /// /// Log on, but use the specified credentials on the network only. The new process uses the /// same token as the caller, but the system creates a new logon session within LSA, and /// the process uses the specified credentials as the default credentials. /// This value can be used to create a process that uses a different set of credentials /// locally than it does remotely. This is useful in inter-domain scenarios where there is /// no trust relationship. /// The system does not validate the specified credentials. Therefore, the process can start, /// but it may not have access to network resources. /// /// See LOGON_NETCREDENTIALS_ONLY NetCredentialsOnly } /// /// Win32 PROCESS_INFORMATION structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct PROCESS_INFORMATION { [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr hProcess; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr hThread; public int ProcessId; public int ThreadId; } /// /// Win32 STARTUPINFO structure. /// [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct STARTUPINFO { public int cb; public string lpReserved; public string lpDesktop; public string lpTitle; public uint dwX; public uint dwY; public uint dwXSize; public uint dwYSize; public uint dwXCountChars; public uint dwYCountChars; public uint dwFillAttribute; public uint dwFlags; public short wShowWindow; public short cbReserved2; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr lpReserved2; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr hStdInput; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr hStdOutput; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr hStdError; } /// /// Win32 SERVICE_DESCRIPTION structure. /// /// /// /// A description of NULL indicates no service description exists. The service description is NULL when the service is created. /// /// /// The description is simply a comment that explains the purpose of the service. For example, for the DHCP service, /// you could use the description "Provides internet addresses for computer on your network." /// /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct SERVICE_DESCRIPTION { /// /// The description of the service. /// /// /// If this member is NULL, the description remains unchanged. If this value is an empty string (""), the current description is deleted. /// public string lpDescription; } /// /// Win32 SC_ACTION structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct SC_ACTION { /// /// The to be performed. /// [MarshalAs(UnmanagedType.U4)] public SC_ACTION_TYPE Type; /// /// The time to wait before performing the specified action, in milliseconds. /// [MarshalAs(UnmanagedType.U4)] public int Delay; } /// /// Win32 SERVICE_FAILURE_ACTIONS structure. /// /// /// Represents the action the service controller should take on each failure of a service. A service is considered failed /// when it terminates without reporting a status of SERVICE_STOPPED to the service controller. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct SERVICE_FAILURE_ACTIONS { /// /// The time after which to reset the failure count to zero if there are no failures, in seconds. Specify INFINITE to indicate that this value should never be reset. /// [MarshalAs(UnmanagedType.U4)] public int dwResetPeriod; /// /// The message to be broadcast to server users before rebooting in response to the SC_ACTION_REBOOT service controller action. /// /// /// If this value is NULL, the reboot message is unchanged. If the value is an empty string (""), the reboot message is deleted and no message is broadcast. /// public string lpRebootMsg; /// /// The command line of the process for the CreateProcess function to execute in response to the SC_ACTION_RUN_COMMAND service controller action. This process runs under the same account as the service. /// /// /// If this value is NULL, the command is unchanged. If the value is an empty string (""), the command is deleted and no program is run when the service fails. /// public string lpCommand; /// /// The number of elements in the lpsaActions array. /// /// /// If this value is 0, but lpsaActions is not NULL, the reset period and array of failure actions are deleted. /// [MarshalAs(UnmanagedType.U4)] public int cActions; /// /// A pointer to an array of SC_ACTION structures. /// /// /// If this value is NULL, the cActions and dwResetPeriod members are ignored. /// [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr lpsaActions; } /// /// Win32 SERVICE_FAILURE_ACTIONS_FLAG structure. /// /// /// Contains the failure actions flag setting of a service. This setting determines when failure actions are to be executed. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct SERVICE_FAILURE_ACTIONS_FLAG { /// /// If this member is TRUE and the service has configured failure actions, the failure actions are queued if the service process /// terminates without reporting a status of SERVICE_STOPPED or if it enters the SERVICE_STOPPED state but the dwWin32ExitCode member /// of the SERVICE_STATUS structure is not ERROR_SUCCESS (0). If this member is FALSE and the service has configured failure actions, /// the failure actions are queued only if the service terminates without reporting a status of SERVICE_STOPPED. /// public bool bFailureAction; } /// /// LSA API policy access enumeration. /// [SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] public enum LsaAccess { [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] POLICY_READ = 0x20006, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] POLICY_ALL_ACCESS = 0x00F0FFF, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] POLICY_EXECUTE = 0X20801, [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")] POLICY_WRITE = 0X207F8 } /// /// Win32 LSA_OBJECT_ATTRIBUTES structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct LSA_OBJECT_ATTRIBUTES { public int Length; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr RootDirectory; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr ObjectName; public int Attributes; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr SecurityDescriptor; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr SecurityQualityOfService; } /// /// Win32 LSA_UNICODE_STRING structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct LSA_UNICODE_STRING { /// /// Specifies the length of the string. /// public ushort Length; /// /// Specifies the maximum length of the string. /// public ushort MaximumLength; /// /// Pointer to string data. /// [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr Buffer; } /// /// Win32 LUID_AND_ATTRIBUTES structure. /// /// /// An LUID_AND_ATTRIBUTES structure can represent an LUID whose attributes change frequently, /// such as when the LUID is used to represent privileges in the PRIVILEGE_SET structure. /// Privileges are represented by LUIDs and have attributes indicating whether they are currently enabled or disabled. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct LUID_AND_ATTRIBUTES { /// /// Specifies an LUID value. /// public long Luid; /// /// Specifies attributes of the LUID. This value contains up to 32 one-bit flags. Its meaning is dependent on the definition and use of the LUID. /// public uint Attributes; } /// /// Win32 TOKEN_PRIVILEGES structure. /// /// /// The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token. /// // The Pack attribute specified here is important. We are in essence cheating here because // the Privileges field is actually a variable size array of structs. We use the Pack=1 // to align the Privileges field exactly after the PrivilegeCount field when marshalling // this struct to Win32. [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential, Pack = 1)] public struct TOKEN_PRIVILEGES { /// /// This must be set to the number of entries in the Privileges array. /// public int PrivilegeCount; /// /// Specifies an array of LUID_AND_ATTRIBUTES structures. Each structure contains the LUID and attributes of a privilege. /// public LUID_AND_ATTRIBUTES Privileges; } /// /// Win32 SECURITY_ATTRIBUTES structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES { public int nLength; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr lpSecurityDescriptor; public int bInheritHandle; } /// /// Win32 CRYPTOAPI_BLOB structure. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), StructLayout(LayoutKind.Sequential)] public struct CRYPTOAPI_BLOB { public int cbData; [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")] public IntPtr pbData; } /// /// Win32 CRYPT_OID_INFO class. /// [SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores"), SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), StructLayout(LayoutKind.Sequential)] public class CRYPT_OID_INFO { public int cbSize; [MarshalAs(UnmanagedType.LPStr)] public string pszOID; [MarshalAs(UnmanagedType.LPWStr)] public string pwszName; public int dwGroupId; public int dwValueOrAlgidordwLength; public CRYPTOAPI_BLOB ExtraInfo; [MarshalAs(UnmanagedType.LPWStr)] public string pwszCNGAlgid; [MarshalAs(UnmanagedType.LPWStr)] public string pwszCNGExtraAlgid; } /// /// Win32 InitiateSystemShutdownEx function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InitiateSystemShutdownEx(string lpMachineName, string lpMessage, uint dwTimeout, bool bForceAppsClosed, bool bRebootAfterShutdown, ShutdownReason dwReason); /// /// Win32 GetShellWindow function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetShellWindow(); /// /// Win32 GetWindowThreadProcessId function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); /// /// Win32 OpenSCManager function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, int dwDesiredAccess); /// /// Win32 OpenService function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, int dwDesiredAccess); /// /// Win32 LockServiceDatabase function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern IntPtr LockServiceDatabase(IntPtr hSCManager); /// /// Win32 ChangeServiceConfig2 function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, IntPtr lpInfo); /// /// Win32 ChangeServiceConfig2 function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, [MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo); /// /// Win32 ChangeServiceConfig2 function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, [MarshalAs(UnmanagedType.Struct)] ref SERVICE_FAILURE_ACTIONS_FLAG lpInfo); /// /// Win32 CloseServiceHandle function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool CloseServiceHandle(IntPtr hSCObject); /// /// Win32 UnlockServiceDatabase function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool UnlockServiceDatabase(IntPtr hSCManager); /// /// Win32 LookupAccountName function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "3#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "5#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "6#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, PreserveSig = true)] public static extern bool LookupAccountName(string lpSystemName, string lpAccountName, IntPtr psid, ref int cbsid, StringBuilder domainName, ref int cbdomainLength, ref int use); /// /// Win32 LsaOpenPolicy function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "0#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "1#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", PreserveSig = true)] public static extern uint LsaOpenPolicy(ref LSA_UNICODE_STRING SystemName, ref LSA_OBJECT_ATTRIBUTES ObjectAttributes, int DesiredAccess, out IntPtr PolicyHandle); /// /// Win32 LsaAddAccountRights function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)] public static extern uint LsaAddAccountRights(IntPtr PolicyHandle, IntPtr AccountSid, LSA_UNICODE_STRING[] UserRights, uint CountOfRights); /// /// Win32 LsaClose function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32")] public static extern uint LsaClose(IntPtr PolicyHandle); /// /// Win32 FreeSid function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll")] public static extern IntPtr FreeSid(IntPtr pSid); /// /// Win32 GetLastError function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api"), DllImport("kernel32.dll")] public static extern int GetLastError(); /// /// Win32 GetTokenInformation function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, ref uint TokenInformation, uint TokenInformationLength, out uint ReturnLength); /// /// Win32 GetTokenInformation function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, ref TOKEN_ELEVATION_TYPE TokenInformation, uint TokenInformationLength, out uint ReturnLength); /// /// Win32 LogonUser function. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Username"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Logon"), DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken); /// /// Win32 CreateProcessWithTokenW function. /// [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "Logon"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "7#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)] public static extern bool CreateProcessWithTokenW(IntPtr hToken, LogonFlags dwLogonFlags, string lpApplicationName, string lpCommandLine, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); /// /// Win32 DuplicateToken function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool DuplicateToken(IntPtr existingTokenHandle, int securityImpersonationLevel, ref IntPtr duplicateTokenHandle); /// /// Win32 DuplicateTokenEx function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Struct)] ref SECURITY_ATTRIBUTES lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType, out IntPtr phNewToken); /// /// Win32 DuplicateTokenEx function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool DuplicateTokenEx(IntPtr hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType, out IntPtr phNewToken); /// /// Win32 AdjustTokenPrivileges function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "5#"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState, int BufferLength, IntPtr PreviousState, ref int ReturnLength); /// /// Win32 LookupPrivilegeValue function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref long lpLuid); /// /// Win32 OpenProcessToken function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "2#"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("advapi32.dll", SetLastError = true)] public static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, ref IntPtr TokenHandle); /// /// Win32 OpenProcess function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr OpenProcess(ProcessAccessTypes dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId); /// /// Win32 GetCurrentProcess function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetCurrentProcess(); /// /// Win32 CloseHandle function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr hndl); /// /// Win32 CopyMemory function. /// [DllImport("kernel32.dll", EntryPoint = "CopyMemory")] internal static extern void CopyMemory(IntPtr destination, IntPtr source, uint length); /// /// Win32 FormatMessage function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "1#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "4#"), SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "6#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("kernel32.dll")] public static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref string lpBuffer, int nSize, ref IntPtr Arguments); /// /// Win32 CryptFindOIDInfo function. /// [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("crypt32.dll", SetLastError = true)] public static extern IntPtr CryptFindOIDInfo(int dwKeyType, string pvKey, uint dwGroupId); /// /// Win32 CryptRegisterOIDInfo function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("crypt32.dll", SetLastError = true)] public static extern bool CryptRegisterOIDInfo(IntPtr pInfo, int dwFlags); /// /// Win32 CryptUnregisterOIDInfo function. /// [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("crypt32.dll", SetLastError = true)] public static extern bool CryptUnregisterOIDInfo(IntPtr pInfo); /// /// Win32 IsWow64Process function. /// [SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "1#"), SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs"), DllImport("kernel32.dll", SetLastError = true)] public static extern bool IsWow64Process(IntPtr hProcess, ref bool Wow64Process); /// /// Formats and returns a .NET string containing the Windows API level error message corresponding to the specified error code. /// /// An value corresponding to the specified error code. /// A formatted error message corresponding to the specified error code. public static string GetErrorMessage(int errorCode) { const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100; const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200; const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000; int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; int messageSize = 255; string lpMsgBuf = ""; IntPtr ptrlpSource = IntPtr.Zero; IntPtr prtArguments = IntPtr.Zero; if (FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, ref prtArguments) == 0) throw new InvalidOperationException("Failed to format message for error code " + errorCode); return lpMsgBuf; } /// /// Formats and returns a .NET string containing the Windows API level error message from the last Win32 call. /// /// A formatted error message corresponding to the last Win32 call error code. public static string GetLastErrorMessage() { return GetErrorMessage(Marshal.GetLastWin32Error()); } #pragma warning disable 169 #pragma warning disable 649 // ReSharper disable MemberCanBePrivate.Local // ReSharper disable NotAccessedField.Local [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class MEMORYSTATUSEX { public uint dwLength; public uint dwMemoryLoad; public ulong ullTotalPhys; public ulong ullAvailPhys; public ulong ullTotalPageFile; public ulong ullAvailPageFile; public ulong ullTotalVirtual; public ulong ullAvailVirtual; public ulong ullAvailExtendedVirtual; public MEMORYSTATUSEX() { dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); } } [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible")] [return: MarshalAs(UnmanagedType.Bool)] [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer); /// /// Flushes the buffers of a specified file and causes all buffered data to be written to a file. /// /// /// Since the method does not actually work, this finishes the flush to the disk file system. /// Which still could cache the results, but this is about the best we can do for a flush right now. [SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible")] [DllImport("KERNEL32", SetLastError = true)] public static extern void FlushFileBuffers(SafeFileHandle handle); } }