Win32 进程操作
创建线程
#include<iostream> #include<windows.h> using namespace std; DWORD WINAPI PROCESST1(LPVOID param); DWORD WINAPI PROCESST2(LPVOID param); DWORD WINAPI PROCESST3(LPVOID param); int main3() { HANDLE Thread1, Thread2, Thread3; Thread1 = CreateThread(NULL, 0, PROCESST1, NULL, 0, NULL); Thread2 = CreateThread(NULL, 0, PROCESST2, NULL, 0, NULL); Thread3 = CreateThread(NULL, 0, PROCESST3, NULL, 0, NULL); WaitForSingleObject(Thread1, INFINITE); WaitForSingleObject(Thread2, INFINITE); WaitForSingleObject(Thread3, INFINITE); CloseHandle(Thread1); CloseHandle(Thread2); CloseHandle(Thread3); return 0; } DWORD WINAPI PROCESST1(LPVOID param) { while (true) { cout << "THE Thread1 IS working." << endl; Sleep(500); } return 0; } DWORD WINAPI PROCESST2(LPVOID param) { while (true) { cout << "THE Thread2 IS working.." << endl; Sleep(500); } return 0; } DWORD WINAPI PROCESST3(LPVOID param) { while (true) { cout << "THE Thread3 IS working..." << endl; Sleep(500); } return 0; }
进程操作:
#include<iostream> #include<windows.h> #include<TlHelp32.h> using namespace std; //DWORD WINAPI PROCESST1(LPVOID param); //HANDLE mythred = ::CreateThread(NULL, 0, PROCESST1, NULL, 0, NULL); //建立进程 void Cpro() { STARTUPINFO sw = { 0 }; PROCESS_INFORMATION pinfo = { 0 }; CreateProcess( L"G:\\CPro\\Project1\\Debug\\Project1.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sw, &pinfo ); CloseHandle(pinfo.hThread); CloseHandle(pinfo.hProcess); }; //打开与关闭进程 void Opro() { HANDLE hproc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 8096); TerminateProcess(hproc, 0); }; //遍历进程 void Tpro() { HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 p32 = { sizeof(PROCESSENTRY32) }; BOOL flg = Process32First(hsnap,&p32); if (flg) { do { printf("id:%d name:%ls\n",p32.th32ProcessID,p32.szExeFile); }while (Process32Next(hsnap, &p32)); } }; //遍历DLL模块 void Spro() { HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 10808); MODULEENTRY32 p32 = { sizeof(MODULEENTRY32) }; BOOL flg = Module32First(hsnap, &p32); if (flg) { do { printf("Name:%ls\n", p32.szExePath); } while (Module32Next(hsnap, &p32)); } }; int main() { Spro(); return 0; }