예제 #1
0
 internal static extern bool CreateProcess( string lpApplicationName,
                                   string lpCommandLine,
                                   ref SecurityAttributes lpProcessAttributes,
                                   ref SecurityAttributes lpThreadAttributes,
                                   bool bInheritHandles,
                                   uint dwCreationFlags,
                                   IntPtr lpEnvironment,
                                   string lpCurrentDirectory,
                                   ref StartupInformation lpStartupInfo,
                                   out ProcessInformation lpProcessInformation );
예제 #2
0
        public static int Launch( string aFileName, string aCommandLineArguments, string aCurrentDirectory, bool aShowWindow, int aWaitTimeInMs )
        {
            // See http://msdn.microsoft.com/en-us/library/ms682425.aspx
            // See http://msdn2.microsoft.com/en-us/library/ms682425.aspx
            // See http://www.pinvoke.net/default.aspx/kernel32/CreateProcess.html
            ProcessInformation pi = new ProcessInformation();
            StartupInformation si = new StartupInformation();
            if ( !aShowWindow )
            {
                si.dwFlags = STARTF_USESHOWWINDOW;
                si.wShowWindow = SW_SHOWMINIMIZED;
            }

            si.cb = Marshal.SizeOf( si );
            SecurityAttributes pSec = new SecurityAttributes();
            pSec.nLength = Marshal.SizeOf( pSec );
            SecurityAttributes tSec = new SecurityAttributes();
            tSec.nLength = Marshal.SizeOf( tSec );
            //
            int error = 0;
            bool suceeded = CreateProcess( aFileName, aCommandLineArguments, ref pSec, ref tSec, false, 0, IntPtr.Zero, aCurrentDirectory, ref si, out pi ) != false;
            if ( !suceeded )
            {
                error = Marshal.GetLastWin32Error();
            }
            else
            {
                if ( aWaitTimeInMs != 0 )
                {
                    // Wait
                    WaitForSingleObject( pi.hProcess, aWaitTimeInMs );
                }

                // Tidy up
                CloseHandle( pi.hThread );
                CloseHandle( pi.hProcess );

                int hResult = GetExitCodeProcess( pi.hProcess, ref error );
                if ( error != 0 )
                {
                    Win32Exception exception = new Win32Exception( error );
                    throw exception;
                }
            }
            //
            return error;
        }