1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include<Windows.h> #include <stdio.h> #include <tchar.h> #pragma warning(disable:4996)
void ChildProcess() { MessageBoxA(NULL, "子进程创建成功", "子进程创建成功", MB_OK); Sleep(3000); printf("This is child process"); ExitProcess(0); }
void main() { TCHAR szPath[MAX_PATH] = { 0, }; STARTUPINFO si = { sizeof(STARTUPINFO), }; PROCESS_INFORMATION pi = { 0, }; CONTEXT ctx = { 0, }; printf("This is Father Process!\n");
if (!GetModuleFileName(NULL, szPath, sizeof(TCHAR) * MAX_PATH)) { printf("GetModuleFileName() failed! [%d]\n", GetLastError()); return; } BOOL bRet = CreateProcessW((LPWSTR)szPath, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi); if (!bRet) { printf("CreateProcessW() failed! [%d]\n", GetLastError()); return; } ctx.ContextFlags = CONTEXT_FULL; GetThreadContext(pi.hThread, &ctx); ctx.Eip = (DWORD)ChildProcess; SetThreadContext(pi.hThread, &ctx); if( FALSE == ResumeThread(pi.hThread)) { printf("ResumeThread Failed! [%d]\n",GetLastError()); return; } WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread);
}
|