如何只在RAM中加载进程但不能运行(启动)? (c#)

问题描述:

我想依次执行几个exe文件。我需要执行第一个进程,并且只将另一个进程(第二个进程)加载到RAM中。当第一个过程完成时,第二个过程开始。由于第二个进程已经被加载,我们没有延迟在它们之间切换。如何只在RAM中加载进程但不能运行(启动)? (c#)

+0

如果你有机会到你想运行的可执行文件的源代码,你可以修改它来拜访一个互斥或另一个同步机制执行其逻辑之前。然后,您可以在正确的时间从您的呼叫应用程序发出Mutex信号。 –

首先想到的是对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