Loading... 简单来说,就是执行中的程序,也是计算机中正在运行的程序实例。进程是惰性的,一个进程至少包含 一个主线程-指向代码 进程内核对象【内核对进程的描述】 线程内核对象【执行代码】 一个虚拟空间【4GB】 低2GB有2w字节的空指针区 高2GB保存代码 进程内核对象句柄表,进程权限,唯一ID 什么是线程? 线程属于某一进程,同一个进程的所有线程共享进程内所有资源,包括虚拟空间。 什么是模块? 在内存空间中,有映射的.exe文件 .dll文件都被称为模块 时间片概念:在一个线程退出CPU时候,需要保存自己的执行环境,以便下次加载的时候找到上次执行的位置 Windows操作系统内核中没有进程的概念,只有线程的概念,进程是一组线程机相关资源的封装。 进程控制函数 psapi.h | WinExec | 可以创建进程 | | - | - | | ShellExecute | 创建进程,打印文件,浏览文件夹 | | System | 控制命令的方式打开一个进程 | | CreateProcess | 创建进程 | | OpenProcess | 打开进程 | | ExitProcess | 退出进程 | | TerminateProcess | 结束其他进程 | | GetWindowThreadProcessId | 获取当前句柄的ID | | GetCurrentProcessId | 调用进程当前的ID | | GetProcessImageFileName | 获取指定进程完整路径文件名 | | GetFileVersionInfoSize | 获取是32位还是64位 | | IsWow64Process | 返回值true =32 FALSE=64 | | IsWindowVisible | 判断窗口是否被隐藏 | | EnumProcessModules | 枚举出进程所以模块的句柄 | | GetMouleFileNameEx | 获取模块名称 | | EnumProcess | 用枚举出当前系统中所有进程ID,需要配合OpenProcess获得进程句柄使用 | | GetCurrentProcessId | 获取当前进程一个唯一的标识符 | | GetModuleHandle | 获取一个应用程序或动态链接库的模块句柄 | | GetPriorityClass | 获取特定进程的优先级别 | | GetExitCodeProces | 获取一个已结束进程的退出代码 | | GetExitCodeThread | 获取一个已结束线程的退出代码 | | GetModuleHandleA | 获取一个应用程序或动态链接库的模块句柄 | | TerminateProcess | 结束一个进程 | | LoadLibrary | 载入指定的动态链接库,并将它映射到当前进程使用的地址空间 | | LoadLibraryEx | 装载指定的动态链接库,并为当前进程把它映射到地址空间 | | LoadModule | 载入一个windows应用程序,并在指定的环境中运行 | | | | | - | - | | Process32First | 用来首次调用,获得第一个进程信息 | | Process32Next | 以后调用由它来完成,不断获取进程信息 | | Module32First | 首次调用,第一个模块信息 | | Module32Next | 以后调用由它来完成,不断获取模块信息 | | Thread32First | 用来首次调用,获得第一个线程信息 | | Thread32Next | 以后的调用由他来完成,不断获取线程信息 | | CreateToolhelp32Snapshot | 可以分别创建进程,线程,进程模块,进程堆块快照 | ## 进程创建 还有一种使用命令的方式执行 ``` ShellExecute(NULL, "open", "ReadMe.txt", NULL, NULL, SW_SHOWNORMAL); ``` ``` #include <iostream> #include <Windows.h> #define Path L"D:\\软件\\星愿浏览器\\软件\\Twinkstar Browser\\Twinkstar Browser\\twinkstar.exe" int main() { STARTUPINFO si = { 0 }; PROCESS_INFORMATION proinfo = { 0 }; CreateProcess( Path, //路径 NULL, //命令行参数 NULL, //进程安全属性 NULL, //线程安全属性 FALSE, //是否继承句柄 NULL, //创建方式 NULL, //环境 NULL, //当前运行目录 &si, //启动信息 &proinfo //进程信息 ); return 0; } ``` ## 结束进程 ``` //打开进程获取句柄 HANDLE hPro = OpenProcess( PROCESS_TERMINATE, //访问权限 FALSE, //是否继承句柄 29560 //进程ID ); //结束进程 TerminateProcess(hPro, 0); return 0; ``` ## 遍历进程 ``` //需要包含头文件 #include <tlhelp32.h> //获取快照句柄 HANDLE hToolhelp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //构建结构体储存信息 PROCESSENTRY32 pc = { sizeof(PROCESSENTRY32) }; //获取第一个节点 if (Process32First(hToolhelp, &pc)) { do { printf("[PID]:%d--[NAME]:%S\n",pc.th32ProcessID,pc.szExeFile); } while (Process32Next(hToolhelp, &pc)); } ``` # 线程 ## 进程创建 ```cpp HANDLE Thread1, Thread2; Thread1 = CreateThread(NULL, NULL, ThreadPro1, NULL, NULL, NULL); Thread2 = CreateThread(NULL, NULL, ThreadPro2, NULL, NULL, NULL); WaitForSingleObject(Thread1, -1); WaitForSingleObject(Thread2, -1); printf("%d", g_int); ``` ## 遍历线程 ``` HANDLE hToolhelp = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 10244); //构建结构体储存信息 THREADENTRY32 pc = { sizeof(THREADENTRY32) }; //获取第一个节点 if (Thread32First(hToolhelp, &pc)) { do { printf("[PID]:%d--[PPID]:%d\n",pc.th32OwnerProcessID,pc.th32ThreadID); } while (Thread32Next(hToolhelp, &pc)); } ``` # 遍历模块 ``` //------------------------------遍历模块 //****************注意这里只能遍历32位程序**************** //获取快照句柄 模块是某一进程下的所以得输入进程ID HANDLE hToolhelp = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 10244); //构建结构体储存信息 MODULEENTRY32 pc = { sizeof(MODULEENTRY32) }; //获取第一个节点 if (Module32First(hToolhelp, &pc)) { do { printf("[PID]:%d--[NAME]:%S--[基址]:0x%08X\n",pc.th32ProcessID,pc.szModule,pc.modBaseAddr); } while (Module32Next(hToolhelp, &pc)); } ``` 最后修改:2021 年 03 月 26 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏