2016-12-31 31 views
0

我想依次執行幾個exe文件。我需要執行第一個進程,並且只將另一個進程(第二個進程)加載到RAM中。當第一個過程完成時,第二個過程開始。由於第二個進程已經被加載,我們沒有延遲在它們之間切換。如何只在RAM中加載進程但不能運行(啓動)? (c#)

+0

如果你有機會到你想運行的可執行文件的源代碼,你可以修改它來拜訪一個互斥或另一個同步機制執行其邏輯之前。然後,您可以在正確的時間從您的呼叫應用程序發出Mutex信號。 –

回答

2

首先想到的是對Win32 API使用較低級別的調用。您可以創建一個暫停的進程,然後恢復調用ResumeThread的主線程。

也許下面的代碼可以幫到你嗎? 編輯:增加了一些意見可能有助於更好地理解代碼:

// Structures needed by CreateProcess API 
    public struct PROCESS_INFORMATION 
    { 
     public IntPtr hProcess; 
     public IntPtr hThread; 
     public uint dwProcessId; 
     public uint dwThreadId; 
    } 
    public struct STARTUPINFO 
    { 
     public uint cb; 
     public string lpReserved; 
     public string lpDesktop; 
     public string lpTitle; 
     public uint dwX; 
     public uint dwY; 
     public uint dwXSize; 
     public uint dwYSize; 
     public uint dwXCountChars; 
     public uint dwYCountChars; 
     public uint dwFillAttribute; 
     public uint dwFlags; 
     public short wShowWindow; 
     public short cbReserved2; 
     public IntPtr lpReserved2; 
     public IntPtr hStdInput; 
     public IntPtr hStdOutput; 
     public IntPtr hStdError; 
    } 

    public struct SECURITY_ATTRIBUTES 
    { 
     public int length; 
     public IntPtr lpSecurityDescriptor; 
     public bool bInheritHandle; 
    } 


    public class MyProcess : IDisposable 
    { 
     PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); 
     // Look at https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx for more information about process creation flags 
     private const int CREATE_SUSPENDED = 0x00000004; 
     public bool CreateProcess(string filename) 
     { 
      STARTUPINFO si = new STARTUPINFO(); 
      // Create a Win32 process started suspended. pi will hold the handles to the process and main thread 
      return CreateProcess(filename, null, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, null, ref si, out pi); 
     } 
     public void Start() 
     { 
      // Start the process which is currently suspended by resuming the main thread 
      ResumeThread(pi.hThread); 
     } 

     public void Dispose() 
     { 
      // Handles to the thread and process must be released when done so no memory leaks occur 
      CloseHandle(pi.hThread); 
      CloseHandle(pi.hProcess); 
     } 


     [DllImport("kernel32.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static private extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, 
           bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, 
           string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     static private extern uint ResumeThread(IntPtr hThread); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool CloseHandle(IntPtr hObject); 
    } 
+0

plz幫助並解釋更多thx – Mahdi

+0

並非所有的東西都可以直接用.net框架完成。幸運的是,.net可以讓您鎖定低級別的原生Win32 API函數。因此,您可以在本機C Win32程序中執行任何操作,理論上可以在您的C#代碼中執行。 – IProgrammer

+0

對不起。我誤按了帖子。在這種情況下,我們使用CreateProcess創建一個進程,但給它一個標誌來告訴Windows以暫停模式創建進程。然後,您可以調用ResumeThread來解凍流程的主線程並讓其啓動 – IProgrammer

相關問題