要求:
1. 用CodeBlocks+GCC, build 控制台程序procListConsole.cpp并运行。
该程序列出系统的所有进程的ID(16 进制)及模块Name
2. SDK开发改写procListConsole.cpp为一个Windows应用程序procListWin.cpp
提交:procListWin.cpp和procListWin.exe
其中,样例程序proListConsole.cpp源代码如下:
#include <windows.h> #include <stdio.h> #include <conio.h> // for getch #include <tlhelp32.h> // 快照函数的头文件 int main( ) { PROCESSENTRY32 pe32; // 给所有进程拍快照 HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 在使用这个结构之前,先设置它的大小 pe32.dwSize = sizeof(pe32); // 遍历进程快照,逐个显示进程信息 BOOL bMore = Process32First(hProcessSnap, &pe32); while (bMore) { printf(" ID: %05x Name: %S\n", (unsigned)pe32.th32ProcessID,pe32.szExeFile); // %S for UNICODE string bMore = Process32Next(hProcessSnap, &pe32); } CloseHandle(hProcessSnap); // 关闭snapshot对象 getch(); //Press any key to end program return 0; }
所以我们改写可以得到以下代码:
#include <windows.h> #include <tlhelp32.h> #include <stdio.h> #define LEN 1200 //*************************************************************************************** LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM); // Prototypes of functions called by WinMain BOOL InitApplication(HINSTANCE); BOOL InitInstance(HINSTANCE,int); int WINAPI WinMain(HINSTANCE hInstance, // 入口函数 HINSTANCE, LPSTR lpCmdLine, int nCmdShow ) { if (!InitApplication(hInstance)) // 应用初始化 return FALSE; if (!InitInstance(hInstance,nCmdShow)) // 实例初始化 return FALSE; MessageBox (NULL,TEXT("Thanks to check the homework :-)"),TEXT("P.S."),0); PROCESSENTRY32 pe32; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); pe32.dwSize = sizeof(pe32); BOOL bMore = Process32First(hProcessSnap,&pe32); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) // 消息循环 { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } //*************************************************************************************** BOOL InitApplication(HINSTANCE hInstance) // 应用初始化 { WNDCLASS wc; // Data structure of the window class wc.style = CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)MainWndProc; // Name of the Window Function wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("My1stWClass"); // Name of the window class return RegisterClass(&wc); } //*************************************************************************************** BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) // 实例初始化 { HWND hWnd = CreateWindow(TEXT("My1stWClass"), // Name of the window class TEXT("17APLab1:Windows进程列表 作者:1017510**** XXX"), // Title of the window WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } //*************************************************************************************** // 窗口过程函数 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PROCESSENTRY32 pe32; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); pe32.dwSize = sizeof(pe32); BOOL bMore = Process32First(hProcessSnap,&pe32); TCHAR id[40],content[40]; // 显示的内容 PAINTSTRUCT ps; HDC hdc; int len=0,cnt=-1; switch (message) { case WM_PAINT: // 窗口客户区得刷新 hdc = BeginPaint (hWnd, &ps); while(bMore) { if(len/LEN>cnt) { cnt++; TextOut(hdc,cnt*LEN/5,0,"ID",lstrlen("ID")); TextOut(hdc,cnt*LEN/5+70,0,"Name",lstrlen("Name")); len+=20; } strcpy(content,(char*)pe32.szExeFile); TextOut(hdc,(len/LEN)*LEN/5+0,len%LEN,id,wsprintf(id,TEXT("%05d"),pe32.th32ProcessID)); TextOut(hdc,(len/LEN)*LEN/5+70,len%LEN,content,lstrlen(content)); len+=20; bMore = Process32Next(hProcessSnap,&pe32); } EndPaint (hWnd, &ps); return 0; case WM_DESTROY: // 窗口关闭 PostQuitMessage(0); return 0; default: // 缺省消息的处理 return DefWindowProc(hWnd, message, wParam, lParam); } }
运行结果如图所示: