第一个SDK程序:显示系统当前进程

来源:互联网 发布:淘宝卖家查询工具 编辑:程序博客网 时间:2024/06/06 09:39
#include <windows.h>#include <stdio.h>#include <conio.h>#include <string>#include <tlhelp32.h> // 声明快照函数的头文件#include <hello.h>     // 自定义头文件//***************************************************************************************using namespace std;int WINAPI WinMain(HINSTANCE hInstance,                  // 入口函数                   HINSTANCE,                   LPSTR     lpCmdLine,                   int       nCmdShow  ){    if (!InitApplication(hInstance))       // 应用初始化        return FALSE;    if (!InitInstance(hInstance,nCmdShow)) // 实例初始化        return FALSE;    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("My First Window"), // 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){    PAINTSTRUCT ps;    HDC hdc;    PROCESSENTRY32 pe32;    // 给系统内的所有进程拍快照    HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);    // 在使用这个结构之前,先设置它的大小    pe32.dwSize = sizeof(pe32);    if(hProcessSnap == INVALID_HANDLE_VALUE)    {        printf("CreateToolhelp32Snapshot调用失败!\n");        return -1;    }    // 遍历进程快照,轮流显示每个进程的信息    BOOL bMore = Process32First(hProcessSnap, &pe32);    string process[1000];    int k = 0;    while(bMore)    {        char temp[100];        sprintf(temp, "%05x       %s", (unsigned)pe32.th32ProcessID, pe32.szExeFile);        process[k++] = temp;        bMore = Process32Next(hProcessSnap, &pe32);    }    CloseHandle(hProcessSnap); // 清除snapshot对象    switch (message)    {        case WM_PAINT:  // 窗口客户区得刷新        {            char title[100] = "进程ID:     模块名:                   ";            hdc = BeginPaint (hWnd, &ps);            //按照每行33个进程的格式输出            int over = 0, kk = 0;            for (int j = 0;; j++)            {                for (int i = 0; i <= 33; i++)                {                    if (!i)                        TextOut(hdc, 300 * j + 50, 20 * i, title, lstrlen(title));                    else                    {                        TextOut(hdc, 300 * j + 50, 20 * i, process[kk].c_str(), lstrlen(process[kk].c_str()));                        kk++;                        if (kk == k)                        {                            over = 1;                            break;                        }                    }                }                if (over)                    break;            }            EndPaint (hWnd, &ps);            return 0;        }        case WM_DESTROY: // 窗口关闭        {            PostQuitMessage(0);            return 0;        }        default:  // 缺省消息的处理            return DefWindowProc(hWnd, message, wParam, lParam);   }}

下面是hello.h的内容

//*************************************************************************************** // Prototype for the Window Function LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);// Prototypes of functions called by WinMain BOOL InitApplication(HINSTANCE);BOOL InitInstance(HINSTANCE,int);//***************************************************************************************
虽然我也感觉这头文件实在是没必要,不过老师要求也zhi

0 0
原创粉丝点击