如何在RAM中只加载进程而不运行(启动)?(c#)
我想按顺序执行几个exe文件。我需要执行第一个进程,并且只将另一个进程(第二个)加载到RAM中。第一个过程完成后,第二个过程开始。由于第二个进程已经加载,我们没有延迟在它们之间切换。首先想到的是使用对Win32 API的低级调用。您可以创建一个挂起的进程,然后继续主线程调用ResumeThread 也许下面的代码可以帮助您编辑:添加了一些可能有助于更好地理解代码的注释:如何在RAM中只加载进程而不运行(启动)?(c#),c#,optimization,process,ram,C#,Optimization,Process,Ram,我想按顺序执行几个exe文件。我需要执行第一个进程,并且只将另一个进程(第二个)加载到RAM中。第一个过程完成后,第二个过程开始。由于第二个进程已经加载,我们没有延迟在它们之间切换。首先想到的是使用对Win32 API的低级调用。您可以创建一个挂起的进程,然后继续主线程调用ResumeThread 也许下面的代码可以帮助您编辑:添加了一些可能有助于更好地理解代码的注释: // Structures needed by CreateProcess API public struct
// 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);
}
首先想到的是使用对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);
}
如果您有权访问正在尝试运行的可执行文件的源代码,则可以对其进行修改,使其在执行其逻辑之前等待互斥或其他同步机制。然后,您可以在正确的时间从调用应用程序发出该互斥体的信号。如果您有权访问正在尝试运行的可执行文件的源代码,您可以修改它以等待互斥体或其他同步机制,然后再执行其逻辑。然后,您可以在正确的时间从调用应用程序发出互斥信号。并非所有事情都可以通过.net framework直接完成。幸运的是,.net允许您使用pinvoke低级本机Win32 API函数。因此,在本机C Win32程序中可以执行的任何操作,理论上都可以在C#代码中执行。对不起。我误按了邮递。在本例中,我们使用CreateProcess创建一个进程,但给它一个标志,告诉windows在挂起模式下创建进程。然后,您可以调用ResumeThread来解冻进程的主线程,并让它启动。并非所有事情都可以通过.net framework直接完成。幸运的是,.net允许您使用pinvoke低级本机Win32 API函数。因此,在本机C Win32程序中可以执行的任何操作,理论上都可以在C#代码中执行。对不起。我误按了邮递。在本例中,我们使用CreateProcess创建一个进程,但给它一个标志,告诉windows在挂起模式下创建进程。然后您可以调用ResumeThread来解冻进程的主线程并让它启动