Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Win32.cs « Utility « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c1f4a331184d51009a20b321262ea3b107418a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Duplicati.Library.Utility
{
    //The signatures in this file are from http://pinvoke.net

    /// <summary>
    /// Various Windows specific calls 
    /// </summary>
    public static class Win32
    {

        #region Consts

        public const int ATTACH_PARENT_PROCESS = -1;

        #endregion

        #region Enums

        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_AWAYMODE_REQUIRED = 0x00000040,
            ES_CONTINUOUS = 0x80000000,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_SYSTEM_REQUIRED = 0x00000001
            // Legacy flag, should not be used.
            // ES_USER_PRESENT = 0x00000004
        }


        [Flags]
        public enum PROCESS_PRIORITY_CLASS : int
        {
            /// <summary>
            /// Process that has priority above <see cref="NORMAL_PRIORITY_CLASS" /> but below <see cref="HIGH_PRIORITY_CLASS" />.
            /// </summary>
            ABOVE_NORMAL_PRIORITY_CLASS = 0x00008000,
            /// <summary>
            /// Process that has priority above <see cref="IDLE_PRIORITY_CLASS" /> but below <see cref="NORMAL_PRIORITY_CLASS" />.
            /// </summary>
            BELOW_NORMAL_PRIORITY_CLASS = 0x00004000,
            /// <summary>
            /// Process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
            /// </summary>
            HIGH_PRIORITY_CLASS = 0x00000080,
            /// <summary>
            /// Process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
            /// </summary>
            IDLE_PRIORITY_CLASS = 0x00000040,
            /// <summary>
            /// Process with no special scheduling needs.
            /// </summary>
            NORMAL_PRIORITY_CLASS = 0x00000020,
            /// <summary>
            /// Begin background processing mode. The system lowers the resource scheduling priorities of the process (and its threads) so that it can perform background work without significantly affecting activity in the foreground.
            /// </summary>
            PROCESS_MODE_BACKGROUND_BEGIN = 0x00100000,
            /// <summary>
            /// End background processing mode. The system restores the resource scheduling priorities of the process (and its threads) as they were before the process entered background processing mode.
            /// </summary>
            PROCESS_MODE_BACKGROUND_END = 0x00200000,
            /// <summary>
            /// Process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.
            /// </summary>
            REALTIME_PRIORITY_CLASS = 0x00000100
        }


        /// <summary>
        /// The IO priority settings
        /// </summary>
        public enum IO_PRIORITY_HINT : int
        {
            /// <summary>
            /// Defragging, content indexing and other background I/Os.
            /// </summary>
            IoPriorityVeryLow = 0,
            /// <summary>
            /// Prefetching for applications.
            /// </summary>
            IoPriorityLow,
            /// <summary>
            /// Normal I/Os.
            /// </summary>
            IoPriorityNormal,
            /// <summary>
            /// Used by filesystems for checkpoint I/O.
            /// </summary>
            IoPriorityHigh,
            /// <summary>
            /// Used by memory manager. Not available for applications.
            /// </summary>
            IoPriorityCritical,
        }


        public enum PROCESS_INFORMATION_CLASS : int
        {
            ProcessBasicInformation = 0,
            ProcessQuotaLimits,
            ProcessIoCounters,
            ProcessVmCounters,
            ProcessTimes,
            ProcessBasePriority,
            ProcessRaisePriority,
            ProcessDebugPort,
            ProcessExceptionPort,
            ProcessAccessToken,
            ProcessLdtInformation,
            ProcessLdtSize,
            ProcessDefaultHardErrorMode,
            ProcessIoPortHandlers,
            ProcessPooledUsageAndLimits,
            ProcessWorkingSetWatch,
            ProcessUserModeIOPL,
            ProcessEnableAlignmentFaultFixup,
            ProcessPriorityClass,
            ProcessWx86Information,
            ProcessHandleCount,
            ProcessAffinityMask,
            ProcessPriorityBoost,
            ProcessDeviceMap,
            ProcessSessionInformation,
            ProcessForegroundInformation,
            ProcessWow64Information,
            ProcessImageFileName,
            ProcessLUIDDeviceMapsEnabled,
            ProcessBreakOnTermination,
            ProcessDebugObjectHandle,
            ProcessDebugFlags,
            ProcessHandleTracing,
            ProcessIoPriority,
            ProcessExecuteFlags,
            ProcessResourceManagement,
            ProcessCookie,
            ProcessImageInformation,
            ProcessCycleTime,
            ProcessPagePriority,
            ProcessInstrumentationCallback,
            ProcessThreadStackAllocation,
            ProcessWorkingSetWatchEx,
            ProcessImageFileNameWin32,
            ProcessImageFileMapping,
            ProcessAffinityUpdateMode,
            ProcessMemoryAllocationMode,
            MaxProcessInfoClass
        }

        #endregion

        #region Function calls

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

        /// <summary>
        /// Attaches the calling process to the console of the specified process.
        /// </summary>
        /// <param name="dwProcessId">[in] Identifier of the process, usually will be ATTACH_PARENT_PROCESS</param>
        /// <returns>If the function succeeds, the return value is nonzero.
        /// If the function fails, the return value is zero.
        /// To get extended error information, call Marshal.GetLastWin32Error.</returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool AttachConsole(int dwProcessId);

        /// <summary>
        /// Sets the process information parameters
        /// </summary>
        /// <returns>The set information process.</returns>
        /// <param name="hProcess">The process to use.</param>
        /// <param name="processInformationClass">The process information class.</param>
        /// <param name="processInformation">The process information.</param>
        /// <param name="processInformationLength">The process information length.</param>
        [DllImport("ntdll.dll", SetLastError = true)]
        public static extern int NtSetInformationProcess(IntPtr hProcess, PROCESS_INFORMATION_CLASS processInformationClass, ref IO_PRIORITY_HINT processInformation, int processInformationLength);


        /// <summary>
        /// Sets the process information parameters
        /// </summary>
        /// <returns>The query information.</returns>
        /// <param name="hProcess">The process to use.</param>
        /// <param name="processInformationClass">The process information class.</param>
        /// <param name="processInformation">The process information.</param>
        /// <param name="processInformationLength">Process information length.</param>
        /// <param name="returnLength">The size of the result.</param>
        [DllImport("ntdll.dll", SetLastError = true)]
        public static extern int NtQueryInformationProcess(IntPtr hProcess, PROCESS_INFORMATION_CLASS processInformationClass, ref IO_PRIORITY_HINT processInformation, int processInformationLength, IntPtr returnLength);

        /// <summary>
        /// Sets the priority class for the specified process. This value together with the priority value of each thread of the process determines each thread's base priority level.
        /// </summary>
        /// <returns><c>true</c>, if priority class was set, <c>false</c> otherwise.</returns>
        /// <param name="handle">A handle to the process. The handle must have the PROCESS_SET_INFORMATION access right.</param>
        /// <param name="priorityClass">The priority class for the process.</param>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool SetPriorityClass(IntPtr handle, PROCESS_PRIORITY_CLASS priorityClass);

        /// <summary>
        /// Gets the priority class for the specified process. This value together with the priority value of each thread of the process determines each thread's base priority level.
        /// </summary>
        /// <returns>The <see cref="PROCESS_PRIORITY_CLASS"/> value.</returns>
        /// <param name="handle">A handle to the process. The handle must have the PROCESS_SET_INFORMATION access right.</param>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern PROCESS_PRIORITY_CLASS GetPriorityClass(IntPtr handle);
        #endregion
    }
}