进程列表

来源:互联网 发布:java系统开发实例 编辑:程序博客网 时间:2024/05/16 17:37

 进程列表

作者:Ackarlix

 

代码:

#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <fstream.h>


int main(int argc, char* argv[])
{

 ofstream outfile("ProcessList.txt");
 PROCESSENTRY32 pe32;
 pe32.dwSize = sizeof(pe32);

 HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (hProcessSnap == INVALID_HANDLE_VALUE)
 {

  outfile << "CreateToolhelp32Snapshot调用失败!" << endl;
  return -1;
 }

 BOOL bMore = ::Process32First(hProcessSnap, &pe32);

 while (bMore)
 {
  outfile << "进程名称:           " << pe32.szExeFile << endl;
  outfile << "进程ID号:           " << pe32.th32ProcessID << endl;
  outfile << "进程引用数:         " << pe32.cntUsage << endl;
  outfile << "进程默认堆ID号:     " << pe32.th32DefaultHeapID << endl;
  outfile << "进程模块ID号:       " << pe32.th32ModuleID << endl;
  outfile << "进程的线程数:       " << pe32.cntThreads << endl;
  outfile << "进程的父进程ID:     " << pe32.th32ParentProcessID << endl;
  outfile << endl;
  bMore = ::Process32Next(hProcessSnap, &pe32);
 }


 ::CloseHandle(hProcessSnap);

 STARTUPINFO si = {sizeof(si)};
 PROCESS_INFORMATION pi;
 char * szCommandLine = "notepad ProcessList.txt";
 ::CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);

 ::CloseHandle(pi.hThread);
 ::CloseHandle(pi.hProcess);

 return 0;
}

原创粉丝点击