-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Process.cs
2874 lines (2587 loc) · 123 KB
/
Process.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="Process.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Diagnostics {
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Diagnostics;
using System;
using System.Collections;
using System.IO;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System.Collections.Specialized;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Runtime.Versioning;
/// <devdoc>
/// <para>
/// Provides access to local and remote
/// processes. Enables you to start and stop system processes.
/// </para>
/// </devdoc>
[
MonitoringDescription(SR.ProcessDesc),
DefaultEvent("Exited"),
DefaultProperty("StartInfo"),
Designer("System.Diagnostics.Design.ProcessDesigner, " + AssemblyRef.SystemDesign),
// Disabling partial trust scenarios
PermissionSet(SecurityAction.LinkDemand, Name="FullTrust"),
PermissionSet(SecurityAction.InheritanceDemand, Name="FullTrust"),
HostProtection(SharedState=true, Synchronization=true, ExternalProcessMgmt=true, SelfAffectingProcessMgmt=true)
]
public class Process : Component {
//
// FIELDS
//
bool haveProcessId;
int processId;
bool haveProcessHandle;
SafeProcessHandle m_processHandle;
bool isRemoteMachine;
string machineName;
ProcessInfo processInfo;
Int32 m_processAccess;
#if !FEATURE_PAL
ProcessThreadCollection threads;
ProcessModuleCollection modules;
#endif // !FEATURE_PAL
bool haveMainWindow;
IntPtr mainWindowHandle; // no need to use SafeHandle for window
string mainWindowTitle;
bool haveWorkingSetLimits;
IntPtr minWorkingSet;
IntPtr maxWorkingSet;
bool haveProcessorAffinity;
IntPtr processorAffinity;
bool havePriorityClass;
ProcessPriorityClass priorityClass;
ProcessStartInfo startInfo;
bool watchForExit;
bool watchingForExit;
EventHandler onExited;
bool exited;
int exitCode;
bool signaled;
DateTime exitTime;
bool haveExitTime;
bool responding;
bool haveResponding;
bool priorityBoostEnabled;
bool havePriorityBoostEnabled;
bool raisedOnExited;
RegisteredWaitHandle registeredWaitHandle;
WaitHandle waitHandle;
ISynchronizeInvoke synchronizingObject;
StreamReader standardOutput;
StreamWriter standardInput;
StreamReader standardError;
OperatingSystem operatingSystem;
bool disposed;
static object s_CreateProcessLock = new object();
// This enum defines the operation mode for redirected process stream.
// We don't support switching between synchronous mode and asynchronous mode.
private enum StreamReadMode
{
undefined,
syncMode,
asyncMode
}
StreamReadMode outputStreamReadMode;
StreamReadMode errorStreamReadMode;
// Support for asynchrously reading streams
[Browsable(true), MonitoringDescription(SR.ProcessAssociated)]
//[System.Runtime.InteropServices.ComVisible(false)]
public event DataReceivedEventHandler OutputDataReceived;
[Browsable(true), MonitoringDescription(SR.ProcessAssociated)]
//[System.Runtime.InteropServices.ComVisible(false)]
public event DataReceivedEventHandler ErrorDataReceived;
// Abstract the stream details
internal AsyncStreamReader output;
internal AsyncStreamReader error;
internal bool pendingOutputRead;
internal bool pendingErrorRead;
private static SafeFileHandle InvalidPipeHandle = new SafeFileHandle(IntPtr.Zero, false);
#if DEBUG
internal static TraceSwitch processTracing = new TraceSwitch("processTracing", "Controls debug output from Process component");
#else
internal static TraceSwitch processTracing = null;
#endif
//
// CONSTRUCTORS
//
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Diagnostics.Process'/> class.
/// </para>
/// </devdoc>
public Process() {
this.machineName = ".";
this.outputStreamReadMode = StreamReadMode.undefined;
this.errorStreamReadMode = StreamReadMode.undefined;
this.m_processAccess = NativeMethods.PROCESS_ALL_ACCESS;
}
[ResourceExposure(ResourceScope.Machine)]
Process(string machineName, bool isRemoteMachine, int processId, ProcessInfo processInfo) : base() {
Debug.Assert(SyntaxCheck.CheckMachineName(machineName), "The machine name should be valid!");
this.processInfo = processInfo;
this.machineName = machineName;
this.isRemoteMachine = isRemoteMachine;
this.processId = processId;
this.haveProcessId = true;
this.outputStreamReadMode = StreamReadMode.undefined;
this.errorStreamReadMode = StreamReadMode.undefined;
this.m_processAccess = NativeMethods.PROCESS_ALL_ACCESS;
}
//
// PROPERTIES
//
/// <devdoc>
/// Returns whether this process component is associated with a real process.
/// </devdoc>
/// <internalonly/>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessAssociated)]
bool Associated {
get {
return haveProcessId || haveProcessHandle;
}
}
#if !FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets the base priority of
/// the associated process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessBasePriority)]
public int BasePriority {
get {
EnsureState(State.HaveProcessInfo);
return processInfo.basePriority;
}
}
#endif // FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets
/// the
/// value that was specified by the associated process when it was terminated.
/// </para>
/// </devdoc>
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessExitCode)]
public int ExitCode {
get {
EnsureState(State.Exited);
return exitCode;
}
}
/// <devdoc>
/// <para>
/// Gets a
/// value indicating whether the associated process has been terminated.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessTerminated)]
public bool HasExited {
get {
if (!exited) {
EnsureState(State.Associated);
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_QUERY_INFORMATION | NativeMethods.SYNCHRONIZE, false);
if (handle.IsInvalid) {
exited = true;
}
else {
int exitCode;
// Although this is the wrong way to check whether the process has exited,
// it was historically the way we checked for it, and a lot of code then took a dependency on
// the fact that this would always be set before the pipes were closed, so they would read
// the exit code out after calling ReadToEnd() or standard output or standard error. In order
// to allow 259 to function as a valid exit code and to break as few people as possible that
// took the ReadToEnd dependency, we check for an exit code before doing the more correct
// check to see if we have been signalled.
if (NativeMethods.GetExitCodeProcess(handle, out exitCode) && exitCode != NativeMethods.STILL_ACTIVE) {
this.exited = true;
this.exitCode = exitCode;
}
else {
// The best check for exit is that the kernel process object handle is invalid,
// or that it is valid and signaled. Checking if the exit code != STILL_ACTIVE
// does not guarantee the process is closed,
// since some process could return an actual STILL_ACTIVE exit code (259).
if (!signaled) // if we just came from WaitForExit, don't repeat
{
ProcessWaitHandle wh = null;
try
{
wh = new ProcessWaitHandle(handle);
this.signaled = wh.WaitOne(0, false);
}
finally
{
if (wh != null)
wh.Close();
}
}
if (signaled)
{
if (!NativeMethods.GetExitCodeProcess(handle, out exitCode))
throw new Win32Exception();
this.exited = true;
this.exitCode = exitCode;
}
}
}
}
finally
{
ReleaseProcessHandle(handle);
}
if (exited) {
RaiseOnExited();
}
}
return exited;
}
}
private ProcessThreadTimes GetProcessTimes() {
ProcessThreadTimes processTimes = new ProcessThreadTimes();
SafeProcessHandle handle = null;
try {
int access = NativeMethods.PROCESS_QUERY_INFORMATION;
if (EnvironmentHelpers.IsWindowsVistaOrAbove())
access = NativeMethods.PROCESS_QUERY_LIMITED_INFORMATION;
handle = GetProcessHandle(access, false);
if( handle.IsInvalid) {
// On OS older than XP, we will not be able to get the handle for a process
// after it terminates.
// On Windows XP and newer OS, the information about a process will stay longer.
throw new InvalidOperationException(SR.GetString(SR.ProcessHasExited, processId.ToString(CultureInfo.CurrentCulture)));
}
if (!NativeMethods.GetProcessTimes(handle,
out processTimes.create,
out processTimes.exit,
out processTimes.kernel,
out processTimes.user)) {
throw new Win32Exception();
}
}
finally {
ReleaseProcessHandle(handle);
}
return processTimes;
}
#if !FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets the time that the associated process exited.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessExitTime)]
public DateTime ExitTime {
get {
if (!haveExitTime) {
EnsureState(State.IsNt | State.Exited);
exitTime = GetProcessTimes().ExitTime;
haveExitTime = true;
}
return exitTime;
}
}
#endif // !FEATURE_PAL
/// <devdoc>
/// <para>
/// Returns the native handle for the associated process. The handle is only available
/// if this component started the process.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessHandle)]
public IntPtr Handle {
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get {
EnsureState(State.Associated);
return OpenProcessHandle(this.m_processAccess).DangerousGetHandle();
}
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public SafeProcessHandle SafeHandle {
get {
EnsureState(State.Associated);
return OpenProcessHandle(this.m_processAccess);
}
}
#if !FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets the number of handles that are associated
/// with the process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessHandleCount)]
public int HandleCount {
get {
EnsureState(State.HaveProcessInfo);
return processInfo.handleCount;
}
}
#endif // !FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets
/// the unique identifier for the associated process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessId)]
public int Id {
get {
EnsureState(State.HaveId);
return processId;
}
}
/// <devdoc>
/// <para>
/// Gets
/// the name of the computer on which the associated process is running.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMachineName)]
public string MachineName {
get {
EnsureState(State.Associated);
return machineName;
}
}
#if !FEATURE_PAL
/// <devdoc>
/// <para>
/// Returns the window handle of the main window of the associated process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMainWindowHandle)]
public IntPtr MainWindowHandle {
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get {
if (!haveMainWindow) {
EnsureState(State.IsLocal | State.HaveId);
mainWindowHandle = ProcessManager.GetMainWindowHandle(processId);
if (mainWindowHandle != (IntPtr)0) {
haveMainWindow = true;
} else {
// We do the following only for the side-effect that it will throw when if the process no longer exists on the system. In Whidbey
// we always did this check but have now changed it to just require a ProcessId. In the case where someone has called Refresh()
// and the process has exited this call will throw an exception where as the above code would return 0 as the handle.
EnsureState(State.HaveProcessInfo);
}
}
return mainWindowHandle;
}
}
/// <devdoc>
/// <para>
/// Returns the caption of the <see cref='System.Diagnostics.Process.MainWindowHandle'/> of
/// the process. If the handle is zero (0), then an empty string is returned.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMainWindowTitle)]
public string MainWindowTitle {
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
get {
if (mainWindowTitle == null) {
IntPtr handle = MainWindowHandle;
if (handle == (IntPtr)0) {
mainWindowTitle = String.Empty;
}
else {
int length = NativeMethods.GetWindowTextLength(new HandleRef(this, handle)) * 2;
StringBuilder builder = new StringBuilder(length);
NativeMethods.GetWindowText(new HandleRef(this, handle), builder, builder.Capacity);
mainWindowTitle = builder.ToString();
}
}
return mainWindowTitle;
}
}
/// <devdoc>
/// <para>
/// Gets
/// the main module for the associated process.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMainModule)]
public ProcessModule MainModule {
[ResourceExposure(ResourceScope.Process)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
get {
// We only return null if we couldn't find a main module.
// This could be because
// 1. The process hasn't finished loading the main module (most likely)
// 2. There are no modules loaded (possible for certain OS processes)
// 3. Possibly other?
if (OperatingSystem.Platform == PlatformID.Win32NT) {
EnsureState(State.HaveId | State.IsLocal);
// on NT the first module is the main module
ModuleInfo module = NtProcessManager.GetFirstModuleInfo(processId);
return new ProcessModule(module);
}
else {
ProcessModuleCollection moduleCollection = Modules;
// on 9x we have to do a little more work
EnsureState(State.HaveProcessInfo);
foreach (ProcessModule pm in moduleCollection) {
if (pm.moduleInfo.Id == processInfo.mainModuleId) {
return pm;
}
}
return null;
}
}
}
/// <devdoc>
/// <para>
/// Gets or sets the maximum allowable working set for the associated
/// process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMaxWorkingSet)]
public IntPtr MaxWorkingSet {
get {
EnsureWorkingSetLimits();
return maxWorkingSet;
}
[ResourceExposure(ResourceScope.Process)]
[ResourceConsumption(ResourceScope.Process)]
set {
SetWorkingSetLimits(null, value);
}
}
/// <devdoc>
/// <para>
/// Gets or sets the minimum allowable working set for the associated
/// process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessMinWorkingSet)]
public IntPtr MinWorkingSet {
get {
EnsureWorkingSetLimits();
return minWorkingSet;
}
[ResourceExposure(ResourceScope.Process)]
[ResourceConsumption(ResourceScope.Process)]
set {
SetWorkingSetLimits(value, null);
}
}
/// <devdoc>
/// <para>
/// Gets
/// the modules that have been loaded by the associated process.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessModules)]
public ProcessModuleCollection Modules {
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
get {
if (modules == null) {
EnsureState(State.HaveId | State.IsLocal);
ModuleInfo[] moduleInfos = ProcessManager.GetModuleInfos(processId);
ProcessModule[] newModulesArray = new ProcessModule[moduleInfos.Length];
for (int i = 0; i < moduleInfos.Length; i++) {
newModulesArray[i] = new ProcessModule(moduleInfos[i]);
}
ProcessModuleCollection newModules = new ProcessModuleCollection(newModulesArray);
modules = newModules;
}
return modules;
}
}
/// <devdoc>
/// Returns the amount of memory that the system has allocated on behalf of the
/// associated process that can not be written to the virtual memory paging file.
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.NonpagedSystemMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessNonpagedSystemMemorySize)]
public int NonpagedSystemMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.poolNonpagedBytes);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessNonpagedSystemMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long NonpagedSystemMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.poolNonpagedBytes;
}
}
/// <devdoc>
/// Returns the amount of memory that the associated process has allocated
/// that can be written to the virtual memory paging file.
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PagedMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPagedMemorySize)]
public int PagedMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.pageFileBytes);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPagedMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PagedMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.pageFileBytes;
}
}
/// <devdoc>
/// Returns the amount of memory that the system has allocated on behalf of the
/// associated process that can be written to the virtual memory paging file.
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PagedSystemMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPagedSystemMemorySize)]
public int PagedSystemMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.poolPagedBytes);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPagedSystemMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PagedSystemMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.poolPagedBytes;
}
}
/// <devdoc>
/// <para>
/// Returns the maximum amount of memory that the associated process has
/// allocated that could be written to the virtual memory paging file.
/// </para>
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PeakPagedMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakPagedMemorySize)]
public int PeakPagedMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.pageFileBytesPeak);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakPagedMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PeakPagedMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.pageFileBytesPeak;
}
}
/// <devdoc>
/// <para>
/// Returns the maximum amount of physical memory that the associated
/// process required at once.
/// </para>
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PeakWorkingSet64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakWorkingSet)]
public int PeakWorkingSet {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.workingSetPeak);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakWorkingSet)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PeakWorkingSet64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.workingSetPeak;
}
}
/// <devdoc>
/// Returns the maximum amount of virtual memory that the associated
/// process has requested.
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PeakVirtualMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakVirtualMemorySize)]
public int PeakVirtualMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.virtualBytesPeak);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPeakVirtualMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PeakVirtualMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.virtualBytesPeak;
}
}
private OperatingSystem OperatingSystem {
get {
if (operatingSystem == null) {
operatingSystem = Environment.OSVersion;
}
return operatingSystem;
}
}
/// <devdoc>
/// <para>
/// Gets or sets a value indicating whether the associated process priority
/// should be temporarily boosted by the operating system when the main window
/// has focus.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPriorityBoostEnabled)]
public bool PriorityBoostEnabled {
get {
EnsureState(State.IsNt);
if (!havePriorityBoostEnabled) {
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_QUERY_INFORMATION);
bool disabled = false;
if (!NativeMethods.GetProcessPriorityBoost(handle, out disabled)) {
throw new Win32Exception();
}
priorityBoostEnabled = !disabled;
havePriorityBoostEnabled = true;
}
finally {
ReleaseProcessHandle(handle);
}
}
return priorityBoostEnabled;
}
set {
EnsureState(State.IsNt);
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_SET_INFORMATION);
if (!NativeMethods.SetProcessPriorityBoost(handle, !value))
throw new Win32Exception();
priorityBoostEnabled = value;
havePriorityBoostEnabled = true;
}
finally {
ReleaseProcessHandle(handle);
}
}
}
/// <devdoc>
/// <para>
/// Gets or sets the overall priority category for the
/// associated process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPriorityClass)]
public ProcessPriorityClass PriorityClass {
get {
if (!havePriorityClass) {
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_QUERY_INFORMATION);
int value = NativeMethods.GetPriorityClass(handle);
if (value == 0) {
throw new Win32Exception();
}
priorityClass = (ProcessPriorityClass)value;
havePriorityClass = true;
}
finally {
ReleaseProcessHandle(handle);
}
}
return priorityClass;
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
set {
if (!Enum.IsDefined(typeof(ProcessPriorityClass), value)) {
throw new InvalidEnumArgumentException("value", (int)value, typeof(ProcessPriorityClass));
}
// BelowNormal and AboveNormal are only available on Win2k and greater.
if (((value & (ProcessPriorityClass.BelowNormal | ProcessPriorityClass.AboveNormal)) != 0) &&
(OperatingSystem.Platform != PlatformID.Win32NT || OperatingSystem.Version.Major < 5)) {
throw new PlatformNotSupportedException(SR.GetString(SR.PriorityClassNotSupported), null);
}
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_SET_INFORMATION);
if (!NativeMethods.SetPriorityClass(handle, (int)value)) {
throw new Win32Exception();
}
priorityClass = value;
havePriorityClass = true;
}
finally {
ReleaseProcessHandle(handle);
}
}
}
/// <devdoc>
/// Returns the number of bytes that the associated process has allocated that cannot
/// be shared with other processes.
/// </devdoc>
[Obsolete("This property has been deprecated. Please use System.Diagnostics.Process.PrivateMemorySize64 instead. http://go.microsoft.com/fwlink/?linkid=14202")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPrivateMemorySize)]
public int PrivateMemorySize {
get {
EnsureState(State.HaveNtProcessInfo);
return unchecked((int)processInfo.privateBytes);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPrivateMemorySize)]
[System.Runtime.InteropServices.ComVisible(false)]
public long PrivateMemorySize64 {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.privateBytes;
}
}
/// <devdoc>
/// Returns the amount of time the process has spent running code inside the operating
/// system core.
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessPrivilegedProcessorTime)]
public TimeSpan PrivilegedProcessorTime {
get {
EnsureState(State.IsNt);
return GetProcessTimes().PrivilegedProcessorTime;
}
}
/// <devdoc>
/// <para>
/// Gets
/// the friendly name of the process.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessProcessName)]
public string ProcessName {
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
get {
EnsureState(State.HaveProcessInfo);
String processName = processInfo.processName;
//
// On some old NT-based OS like win2000, the process name from NTQuerySystemInformation is up to 15 characters.
// Processes executing notepad_1234567.exe and notepad_12345678.exe will have the same process name.
// GetProcessByNames will not be able find the process for notepad_12345678.exe.
// So we will try to replace the name of the process by its main module name if the name is 15 characters.
// However we can't always get the module name:
// (1) Normal user will not be able to get module information about processes.
// (2) We can't get module information about remoting process.
// We can't get module name for a remote process
//
if (processName.Length == 15 && ProcessManager.IsNt && ProcessManager.IsOSOlderThanXP && !isRemoteMachine) {
try {
String mainModuleName = MainModule.ModuleName;
if (mainModuleName != null) {
processInfo.processName = Path.ChangeExtension(Path.GetFileName(mainModuleName), null);
}
}
catch(Exception) {
// If we can't access the module information, we can still use the might-be-truncated name.
// We could fail for a few reasons:
// (1) We don't enough privilege to get module information.
// (2) The process could have terminated.
}
}
return processInfo.processName;
}
}
/// <devdoc>
/// <para>
/// Gets
/// or sets which processors the threads in this process can be scheduled to run on.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessProcessorAffinity)]
public IntPtr ProcessorAffinity {
get {
if (!haveProcessorAffinity) {
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_QUERY_INFORMATION);
IntPtr processAffinity;
IntPtr systemAffinity;
if (!NativeMethods.GetProcessAffinityMask(handle, out processAffinity, out systemAffinity))
throw new Win32Exception();
processorAffinity = processAffinity;
}
finally {
ReleaseProcessHandle(handle);
}
haveProcessorAffinity = true;
}
return processorAffinity;
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
set {
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_SET_INFORMATION);
if (!NativeMethods.SetProcessAffinityMask(handle, value))
throw new Win32Exception();
processorAffinity = value;
haveProcessorAffinity = true;
}
finally {
ReleaseProcessHandle(handle);
}
}
}
/// <devdoc>
/// <para>
/// Gets a value indicating whether or not the user
/// interface of the process is responding.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessResponding)]
public bool Responding {
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
get {
if (!haveResponding) {
IntPtr mainWindow = MainWindowHandle;
if (mainWindow == (IntPtr)0) {
responding = true;
}
else {
IntPtr result;
responding = NativeMethods.SendMessageTimeout(new HandleRef(this, mainWindow), NativeMethods.WM_NULL, IntPtr.Zero, IntPtr.Zero, NativeMethods.SMTO_ABORTIFHUNG, 5000, out result) != (IntPtr)0;
}
}
return responding;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessSessionId)]
public int SessionId {
get {
EnsureState(State.HaveNtProcessInfo);
return processInfo.sessionId;
}
}
#endif // !FEATURE_PAL
/// <devdoc>
/// <para>
/// Gets or sets the properties to pass into the <see cref='System.Diagnostics.Process.Start'/> method for the <see cref='System.Diagnostics.Process'/>
/// .
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), MonitoringDescription(SR.ProcessStartInfo)]
public ProcessStartInfo StartInfo {
get {
if (startInfo == null) {
startInfo = new ProcessStartInfo(this);
}
return startInfo;
}
[ResourceExposure(ResourceScope.Machine)]
set {
if (value == null) {
throw new ArgumentNullException("value");
}
startInfo = value;
}
}
#if !FEATURE_PAL
/// <devdoc>
/// Returns the time the associated process was started.
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MonitoringDescription(SR.ProcessStartTime)]
public DateTime StartTime {
get {
EnsureState(State.IsNt);