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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="NativeMethods.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;
  18.     using System.Text;
  19.     using System.Threading;
  20.     using System.Globalization;
  21.     using System.Runtime.Remoting;
  22.     using System.Runtime.InteropServices;
  23.     using System.Runtime.CompilerServices;
  24.     using System.Runtime.ConstrainedExecution;
  25.     using System.Diagnostics;
  26.     using System.Diagnostics.CodeAnalysis;
  27.     using System.ComponentModel;
  28.     using System.Security.Permissions;
  29.     using Microsoft.Win32.SafeHandles;
  30.    
  31.     // not public!
  32.     [HostProtection(MayLeakOnAbort = true)]
  33.     static internal class NativeMethods
  34.     {
  35.         public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  36.        
  37.         public const int GENERIC_READ = unchecked(((int)2147483648u));
  38.         public const int GENERIC_WRITE = (1073741824);
  39.        
  40.         public const int FILE_SHARE_READ = 1;
  41.         public const int FILE_SHARE_WRITE = 2;
  42.         public const int FILE_SHARE_DELETE = 4;
  43.        
  44.         public const int S_OK = 0;
  45.         public const int E_ABORT = unchecked((int)2147500036u);
  46.         public const int E_NOTIMPL = unchecked((int)2147500033u);
  47.        
  48.         public const int CREATE_ALWAYS = 2;
  49.        
  50.         public const int FILE_ATTRIBUTE_NORMAL = 128;
  51.        
  52.         public const int STARTF_USESTDHANDLES = 256;
  53.        
  54.         public const int STD_INPUT_HANDLE = -10;
  55.         public const int STD_OUTPUT_HANDLE = -11;
  56.         public const int STD_ERROR_HANDLE = -12;
  57.        
  58.         public const int STILL_ACTIVE = 259;
  59.         public const int SW_HIDE = 0;
  60.        
  61.         public const int WAIT_OBJECT_0 = 0;
  62.         public const int WAIT_FAILED = unchecked((int)4294967295u);
  63.         public const int WAIT_TIMEOUT = 258;
  64.         public const int WAIT_ABANDONED = 128;
  65.         public const int WAIT_ABANDONED_0 = WAIT_ABANDONED;
  66.        
  67.         // MoveFile Parameter
  68.         public const int MOVEFILE_REPLACE_EXISTING = 1;
  69.        
  70.         // copied from winerror.h
  71.         public const int ERROR_CLASS_ALREADY_EXISTS = 1410;
  72.         public const int ERROR_NONE_MAPPED = 1332;
  73.         public const int ERROR_INSUFFICIENT_BUFFER = 122;
  74.         public const int ERROR_PROC_NOT_FOUND = 127;
  75.         public const int ERROR_BAD_EXE_FORMAT = 193;
  76.         public const int ERROR_INVALID_NAME = 123;
  77.         public const int MAX_PATH = 260;
  78.        
  79.        
  80.         [StructLayout(LayoutKind.Sequential)]
  81.         internal class STARTUPINFO
  82.         {
  83.             public int cb;
  84.             public IntPtr lpReserved = IntPtr.Zero;
  85.             public IntPtr lpDesktop = IntPtr.Zero;
  86.             public IntPtr lpTitle = IntPtr.Zero;
  87.             public int dwX = 0;
  88.             public int dwY = 0;
  89.             public int dwXSize = 0;
  90.             public int dwYSize = 0;
  91.             public int dwXCountChars = 0;
  92.             public int dwYCountChars = 0;
  93.             public int dwFillAttribute = 0;
  94.             public int dwFlags = 0;
  95.             public short wShowWindow = 0;
  96.             public short cbReserved2 = 0;
  97.             public IntPtr lpReserved2 = IntPtr.Zero;
  98.             public SafeFileHandle hStdInput = new SafeFileHandle(IntPtr.Zero, false);
  99.             public SafeFileHandle hStdOutput = new SafeFileHandle(IntPtr.Zero, false);
  100.             public SafeFileHandle hStdError = new SafeFileHandle(IntPtr.Zero, false);
  101.            
  102.             public STARTUPINFO()
  103.             {
  104.                 cb = Marshal.SizeOf(this);
  105.             }
  106.            
  107.             public void Dispose()
  108.             {
  109.                 // close the handles created for child process
  110.                 if (hStdInput != null && !hStdInput.IsInvalid) {
  111.                     hStdInput.Close();
  112.                     hStdInput = null;
  113.                 }
  114.                
  115.                 if (hStdOutput != null && !hStdOutput.IsInvalid) {
  116.                     hStdOutput.Close();
  117.                     hStdOutput = null;
  118.                 }
  119.                
  120.                 if (hStdError != null && !hStdError.IsInvalid) {
  121.                     hStdError.Close();
  122.                     hStdError = null;
  123.                 }
  124.             }
  125.         }
  126.        
  127.         //
  128.         // DACL related stuff
  129.         //
  130.         [StructLayout(LayoutKind.Sequential)]
  131.         internal class SECURITY_ATTRIBUTES
  132.         {
  133.             public int nLength = 12;
  134.             public SafeLocalMemHandle lpSecurityDescriptor = new SafeLocalMemHandle(IntPtr.Zero, false);
  135.             public bool bInheritHandle = false;
  136.         }
  137.        
  138.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  139.         public static extern bool GetExitCodeProcess(SafeProcessHandle processHandle, out int exitCode);
  140.        
  141.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  142.         public static extern bool GetProcessTimes(SafeProcessHandle handle, out long creation, out long exit, out long kernel, out long user);
  143.        
  144.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
  145.         public static extern IntPtr GetStdHandle(int whichHandle);
  146.        
  147.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  148.         public static extern int GetConsoleCP();
  149.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  150.         public static extern int GetConsoleOutputCP();
  151.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  152.         public static extern int WaitForSingleObject(SafeProcessHandle handle, int timeout);
  153.        
  154.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  155.         public static extern bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
  156.        
  157.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true, BestFitMapping = false)]
  158.         public static extern bool CreateProcess(        [MarshalAs(UnmanagedType.LPTStr)]
  159.             // LPCTSTR
  160.             // LPTSTR - note: CreateProcess might insert a null somewhere in this string
  161.             // LPSECURITY_ATTRIBUTES
  162.             // LPSECURITY_ATTRIBUTES
  163.             // BOOL
  164.             // DWORD
  165.         // LPVOID
  166. string lpApplicationName, StringBuilder lpCommandLine, SECURITY_ATTRIBUTES lpProcessAttributes, SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, IntPtr lpEnvironment,         [MarshalAs(UnmanagedType.LPTStr)]
  167.             // LPCTSTR
  168.             // LPSTARTUPINFO
  169. string lpCurrentDirectory, STARTUPINFO lpStartupInfo, SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation
  170.             // LPPROCESS_INFORMATION
  171.         );
  172.        
  173.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  174.         public static extern bool TerminateProcess(SafeProcessHandle processHandle, int exitCode);
  175.        
  176.        
  177.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
  178.         public static extern int GetCurrentProcessId();
  179.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
  180.         public static extern IntPtr GetCurrentProcess();
  181.        
  182.         public static readonly HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  183.        
  184.        
  185.         // copied from winbase.h
  186.         public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 256;
  187.         public const int FORMAT_MESSAGE_IGNORE_INSERTS = 512;
  188.         public const int FORMAT_MESSAGE_FROM_STRING = 1024;
  189.         public const int FORMAT_MESSAGE_FROM_HMODULE = 2048;
  190.         public const int FORMAT_MESSAGE_FROM_SYSTEM = 4096;
  191.         public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192;
  192.         public const int FORMAT_MESSAGE_MAX_WIDTH_MASK = 255;
  193.         public const int LOAD_WITH_ALTERED_SEARCH_PATH = 8;
  194.         public const int LOAD_LIBRARY_AS_DATAFILE = 2;
  195.        
  196.         public const int SEEK_READ = 2;
  197.         public const int FORWARDS_READ = 4;
  198.         public const int BACKWARDS_READ = 8;
  199.         public const int ERROR_EVENTLOG_FILE_CHANGED = 1503;
  200.        
  201.        
  202.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true, BestFitMapping = false)]
  203.         public static extern bool DuplicateHandle(HandleRef hSourceProcessHandle, HandleRef hSourceHandle, HandleRef hTargetProcess, out SafeProcessHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions);
  204.        
  205.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true, BestFitMapping = false)]
  206.         public static extern bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess, out SafeFileHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions);
  207.        
  208.         [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true, BestFitMapping = false)]
  209.         public static extern bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess, out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions);
  210.        
  211.        
  212.        
  213.         public const int PROCESS_TERMINATE = 1;
  214.         public const int PROCESS_CREATE_THREAD = 2;
  215.         public const int PROCESS_SET_SESSIONID = 4;
  216.         public const int PROCESS_VM_OPERATION = 8;
  217.         public const int PROCESS_VM_READ = 16;
  218.         public const int PROCESS_VM_WRITE = 32;
  219.         public const int PROCESS_DUP_HANDLE = 64;
  220.         public const int PROCESS_CREATE_PROCESS = 128;
  221.         public const int PROCESS_SET_QUOTA = 256;
  222.         public const int PROCESS_SET_INFORMATION = 512;
  223.         public const int PROCESS_QUERY_INFORMATION = 1024;
  224.         public const int STANDARD_RIGHTS_REQUIRED = 983040;
  225.         public const int SYNCHRONIZE = 1048576;
  226.         public const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 4095;
  227.        
  228.         public const int SEMAPHORE_MODIFY_STATE = 2;
  229.         public const int EVENT_MODIFY_STATE = 2;
  230.         public const int MUTEX_MODIFY_STATE = 1;
  231.        
  232.        
  233.         public const int ERROR_BROKEN_PIPE = 109;
  234.         public const int ERROR_NO_DATA = 232;
  235.         public const int ERROR_HANDLE_EOF = 38;
  236.         public const int ERROR_IO_INCOMPLETE = 996;
  237.         public const int ERROR_IO_PENDING = 997;
  238.         public const int ERROR_FILE_EXISTS = 80;
  239.         public const int ERROR_FILENAME_EXCED_RANGE = 206;
  240.         // filename too long.
  241.         public const int ERROR_MORE_DATA = 234;
  242.         public const int ERROR_CANCELLED = 1223;
  243.         public const int ERROR_FILE_NOT_FOUND = 2;
  244.         public const int ERROR_PATH_NOT_FOUND = 3;
  245.         public const int ERROR_ACCESS_DENIED = 5;
  246.         public const int ERROR_INVALID_HANDLE = 6;
  247.         public const int ERROR_NOT_ENOUGH_MEMORY = 8;
  248.         public const int ERROR_SHARING_VIOLATION = 32;
  249.         public const int ERROR_OPERATION_ABORTED = 995;
  250.         public const int ERROR_NO_ASSOCIATION = 1155;
  251.         public const int ERROR_DLL_NOT_FOUND = 1157;
  252.         public const int ERROR_DDE_FAIL = 1156;
  253.         public const int ERROR_INVALID_PARAMETER = 87;
  254.         public const int ERROR_PARTIAL_COPY = 299;
  255.         public const int ERROR_SUCCESS = 0;
  256.         public const int ERROR_ALREADY_EXISTS = 183;
  257.         public const int ERROR_COUNTER_TIMEOUT = 1121;
  258.         public const int RPC_S_SERVER_UNAVAILABLE = 1722;
  259.         public const int RPC_S_CALL_FAILED = 1726;
  260.        
  261.        
  262.         public const int DUPLICATE_CLOSE_SOURCE = 1;
  263.         public const int DUPLICATE_SAME_ACCESS = 2;
  264.        
  265.        
  266.        
  267.     }
  268. }

Developer Fusion