查阅资料,了解PROCESSENTRY32的结构,对以上程序进行修改,显示进程其它相关信息。 对以上程序进行修改,查找系统中是否有指定进程在运行(如记事本,或者计算器),如果没有,则启动相应的进程。
时间: 2024-04-30 11:22:00 浏览: 119
PROCESSENTRY32结构体是Windows API中的一个结构体,用于枚举系统中的进程信息。它包含了进程的ID、父进程ID、优先级、线程数量、路径等详细信息。可以通过修改以上程序来显示更多的进程信息,比如进程的线程数量、路径等信息,示例如下:
```cpp
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <iostream>
void PrintProcessInfo(DWORD processID)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (hProcess == NULL)
return;
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
HMODULE hModule;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hModule, sizeof(hModule),
&cbNeeded))
{
GetModuleBaseName(hProcess, hModule, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
}
// Get process path
TCHAR szProcessPath[MAX_PATH];
if (GetModuleFileNameEx(hProcess, NULL, szProcessPath,
sizeof(szProcessPath) / sizeof(TCHAR)))
{
std::wcout << L"Process Path: " << szProcessPath << std::endl;
}
// Get process threads count
DWORD threadCount = 0;
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap != INVALID_HANDLE_VALUE)
{
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
if (Thread32First(hThreadSnap, &te32))
{
do
{
if (te32.th32OwnerProcessID == processID)
{
threadCount++;
}
} while (Thread32Next(hThreadSnap, &te32));
}
CloseHandle(hThreadSnap);
}
std::wcout << L"Process ID: " << processID << std::endl;
std::wcout << L"Process Name: " << szProcessName << std::endl;
std::wcout << L"Process Thread Count: " << threadCount << std::endl;
CloseHandle(hProcess);
}
int main()
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
std::cerr << "CreateToolhelp32Snapshot (of processes)"
<< " failed with error code: "
<< GetLastError() << std::endl;
return 1;
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
std::cerr << "Process32First failed with error code: "
<< GetLastError() << std::endl;
CloseHandle(hProcessSnap);
return 1;
}
// Walk the snapshot of processes and display information about each process in turn
do
{
PrintProcessInfo(pe32.th32ProcessID);
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
// Find and start Notepad process if it's not running
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
std::cerr << "CreateToolhelp32Snapshot (of processes)"
<< " failed with error code: "
<< GetLastError() << std::endl;
return 1;
}
bool foundNotepad = false;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32))
{
do
{
if (_tcsicmp(pe32.szExeFile, TEXT("notepad.exe")) == 0)
{
foundNotepad = true;
break;
}
} while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap);
if (!foundNotepad)
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcess(TEXT("notepad.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
std::wcout << L"Started Notepad process with process ID: " << pi.dwProcessId << std::endl;
}
else
{
std::cerr << "CreateProcess failed with error code: "
<< GetLastError() << std::endl;
}
}
return 0;
}
```
以上程序会输出每个进程的详细信息,包括进程ID、进程名、进程线程数量和进程路径。同时,它还会查找系统中是否有记事本进程在运行,如果没有,则启动记事本进程。
阅读全文