等待进程启动 C#
Wait For Process Start C#
嘿伙计们,我似乎无法让我的代码按我想要的方式工作。
我正在等待一个进程开始,也就是出现在我的任务管理器中。
虽然找不到该过程,但我一直在循环;如果找到进程,则中断 while 循环并执行以下 AKA 注入 DLL 的逻辑。我有断点,但我的代码一直在循环,所以它就像从未找到进程一样,尽管它显示在任务管理器中。
public static int inject(string dllPath, Process tProcess)
{
Process targetProcess = tProcess;
string dllName = dllPath;
const string PROCESSNAME = "BatteryLife.exe";
// Length == 0 = False?
while (Process.GetProcessesByName(PROCESSNAME).Length == 0)
{
var test3 = "";
Thread.Sleep(100);
// Length == 1 = True?
if (Process.GetProcessesByName(PROCESSNAME).Length == 1)
break;
var test = "";
}
var test2 = "";
// the target process
// geting the handle of the process - with required privileges
IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);
// searching for the address of LoadLibraryA and storing it in a pointer
IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
// name of the dll we want to inject
// alocating some memory on the target process - enough to store the name of the dll
// and storing its address in a pointer
IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
// writing the name of the dll there
UIntPtr bytesWritten;
WriteProcessMemory(procHandle, allocMemAddress, Encoding.Default.GetBytes(dllName), (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), out bytesWritten);
// creating a thread that will call LoadLibraryA with allocMemAddress as argument
CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero);
return 0;
}
我认为您需要从进程名称 string
中删除 .exe
。
Process[] pname = Process.GetProcessesByName("BatteryLife");
if (pname.Length == 0)
{
.....
}
嘿伙计们,我似乎无法让我的代码按我想要的方式工作。 我正在等待一个进程开始,也就是出现在我的任务管理器中。 虽然找不到该过程,但我一直在循环;如果找到进程,则中断 while 循环并执行以下 AKA 注入 DLL 的逻辑。我有断点,但我的代码一直在循环,所以它就像从未找到进程一样,尽管它显示在任务管理器中。
public static int inject(string dllPath, Process tProcess)
{
Process targetProcess = tProcess;
string dllName = dllPath;
const string PROCESSNAME = "BatteryLife.exe";
// Length == 0 = False?
while (Process.GetProcessesByName(PROCESSNAME).Length == 0)
{
var test3 = "";
Thread.Sleep(100);
// Length == 1 = True?
if (Process.GetProcessesByName(PROCESSNAME).Length == 1)
break;
var test = "";
}
var test2 = "";
// the target process
// geting the handle of the process - with required privileges
IntPtr procHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, targetProcess.Id);
// searching for the address of LoadLibraryA and storing it in a pointer
IntPtr loadLibraryAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
// name of the dll we want to inject
// alocating some memory on the target process - enough to store the name of the dll
// and storing its address in a pointer
IntPtr allocMemAddress = VirtualAllocEx(procHandle, IntPtr.Zero, (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
// writing the name of the dll there
UIntPtr bytesWritten;
WriteProcessMemory(procHandle, allocMemAddress, Encoding.Default.GetBytes(dllName), (uint)((dllName.Length + 1) * Marshal.SizeOf(typeof(char))), out bytesWritten);
// creating a thread that will call LoadLibraryA with allocMemAddress as argument
CreateRemoteThread(procHandle, IntPtr.Zero, 0, loadLibraryAddr, allocMemAddress, 0, IntPtr.Zero);
return 0;
}
我认为您需要从进程名称 string
中删除 .exe
。
Process[] pname = Process.GetProcessesByName("BatteryLife");
if (pname.Length == 0)
{
.....
}