The Labs \ Source Viewer \ SSCLI \ Microsoft.Win32 \ PROCESS_INFORMATION

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SafeNativeMethods.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace Microsoft.Win32
  16. {
  17.     using System.Runtime.InteropServices;
  18.     using System;
  19.     using System.Diagnostics.CodeAnalysis;
  20.     using System.Security.Permissions;
  21.     using System.Collections;
  22.     using System.IO;
  23.     using System.Text;
  24.     using System.Threading;
  25.     using Microsoft.Win32.SafeHandles;
  26.     using System.Runtime.ConstrainedExecution;
  27.     using System.Runtime.Versioning;
  28.     using System.Diagnostics;
  29.    
  30.     [HostProtection(MayLeakOnAbort = true)]
  31.     [System.Security.SuppressUnmanagedCodeSecurity()]
  32.     static internal class SafeNativeMethods
  33.     {
  34.        
  35.         public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 256, FORMAT_MESSAGE_IGNORE_INSERTS = 512, FORMAT_MESSAGE_FROM_STRING = 1024, FORMAT_MESSAGE_FROM_SYSTEM = 4096, FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192;
  36.        
  37.         public const int MB_RIGHT = 524288, MB_RTLREADING = 1048576;
  38.        
  39.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true, BestFitMapping = true)]
  40.         [SuppressMessage("Microsoft.Security", "CA2101:SpecifyMarshalingForPInvokeStringArguments")]
  41.         [ResourceExposure(ResourceScope.None)]
  42.         unsafe public static extern int FormatMessage(int dwFlags, SafeHandle lpSource, uint dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] arguments);
  43.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true, BestFitMapping = true)]
  44.         [ResourceExposure(ResourceScope.None)]
  45.         public static extern int FormatMessage(int dwFlags, HandleRef lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr arguments);
  46.        
  47.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, BestFitMapping = true)]
  48.         [ResourceExposure(ResourceScope.None)]
  49.         public static extern void OutputDebugString(string message);
  50.        
  51.         [DllImport(ExternDll.User32, CharSet = System.Runtime.InteropServices.CharSet.Auto, BestFitMapping = true)]
  52.         public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);
  53.        
  54.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  55.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  56.         public static extern bool CloseHandle(IntPtr handle);
  57.        
  58.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  59.         public static extern bool CloseHandle(HandleRef handle);
  60.        
  61.         [DllImport(ExternDll.Kernel32)]
  62.         [ResourceExposure(ResourceScope.None)]
  63.         public static extern bool QueryPerformanceCounter(out long value);
  64.        
  65.         [DllImport(ExternDll.Kernel32)]
  66.         [ResourceExposure(ResourceScope.None)]
  67.         public static extern bool QueryPerformanceFrequency(out long value);
  68.        
  69.        
  70.         [DllImport(ExternDll.Kernel32, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
  71.         [ResourceExposure(ResourceScope.Machine)]
  72.         static internal extern SafeWaitHandle CreateSemaphore(NativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes, int initialCount, int maximumCount, string name);
  73.        
  74.         [DllImport(ExternDll.Kernel32, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
  75.         [ResourceExposure(ResourceScope.Machine)]
  76.             /* DWORD */        static internal extern SafeWaitHandle OpenSemaphore(int desiredAccess, bool inheritHandle, string name);
  77.        
  78.         [DllImport(ExternDll.Kernel32, SetLastError = true)]
  79.         [ResourceExposure(ResourceScope.Machine)]
  80.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  81.         static internal extern bool ReleaseSemaphore(SafeWaitHandle handle, int releaseCount, out int previousCount);
  82.        
  83.         [StructLayout(LayoutKind.Sequential)]
  84.         internal class PROCESS_INFORMATION
  85.         {
  86.             // The handles in PROCESS_INFORMATION are initialized in unmanaged functions.
  87.             // We can't use SafeHandle here because Interop doesn't support [out] SafeHandles in structures/classes yet.
  88.             public IntPtr hProcess = IntPtr.Zero;
  89.             public IntPtr hThread = IntPtr.Zero;
  90.             public int dwProcessId = 0;
  91.             public int dwThreadId = 0;
  92.            
  93.             // Note this class makes no attempt to free the handles
  94.             // Use InitialSetHandle to copy to handles into SafeHandles
  95.            
  96.         }
  97.        
  98.         /* The following code has been removed to prevent FXCOP violations.
  99.           The code is left here incase it needs to be resurrected.
  100.         // From file src\services\timers\system\timers\safenativemethods.cs
  101.         public delegate void TimerAPCProc(IntPtr argToCompletionRoutine, int timerLowValue, int timerHighValue);
  102.         */       
  103.     }
  104. }

Developer Fusion