/// <summary> /// Executes code at a given address and returns the thread's exit code. /// </summary> /// <param name="dwStartAddress">Address to be executed.</param> /// <param name="dwParameter">Parameter to be passed to the code being executed.</param> /// <returns>Returns the exit code of the thread.</returns> public uint Execute(uint dwStartAddress, uint dwParameter) { IntPtr hThread; UIntPtr lpExitCode = UIntPtr.Zero; bool bSuccess = false; hThread = CreateRemoteThread(dwStartAddress, dwParameter); if (hThread == IntPtr.Zero) { throw new Exception("Thread could not be remotely created."); } bSuccess = (SThread.WaitForSingleObject(hThread, 10000) == WaitValues.WAIT_OBJECT_0); if (bSuccess) { bSuccess = Imports.GetExitCodeThread(hThread, out lpExitCode); } Imports.CloseHandle(hThread); if (!bSuccess) { throw new Exception("Error waiting for thread to exit or getting exit code."); } return((uint)lpExitCode); }
/// <summary> /// Gets the exit code of the specified thread. /// </summary> /// <param name="hThread">Handle to the thread whose exit code is wanted.</param> /// <returns>Returns 0 on failure, non-zero on success.</returns> public static uint GetExitCodeThread(IntPtr hThread) { UIntPtr dwExitCode; if (!Imports.GetExitCodeThread(hThread, out dwExitCode)) { throw new Exception("GetExitCodeThread failed."); } return((uint)dwExitCode); }