簡體   English   中英

等待流程啟動C#

[英]Wait For Process Start C#

嗨,我似乎無法讓我的代碼按照我想要的方式工作。 我正在等待一個過程開始,AKA顯示在我的任務管理器中。 雖然找不到該進程,但我不斷循環; 如果找到了進程,則中斷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)
{
  .....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM