⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************\ 
*       This is a part of the Microsoft Source Code Samples.  
*       Copyright (C) 1994-1998 Microsoft Corporation. 
*       All rights reserved. 
*       This source code is only intended as a supplement to  
*       Microsoft Development Tools and/or WinHelp documentation. 
*       See these sources for detailed information regarding the  
*       Microsoft samples programs. 
\******************************************************************************/ 
 
 
/*++ 
 
Module Name: 
 
    common.c 
 
Abstract: 
 
    This module contains common apis used by tlist & kill. 
 
--*/ 

#include <windows.h> 
#include <winperf.h>   // for Windows NT 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <tlhelp32.h>  // for Windows 95 
 
#include "common.h" 
 
 
// 
// manafest constants 
// 
#define INITIAL_SIZE        51200 
#define EXTEND_SIZE         25600 
#define REGKEY_PERF         _T("software\\microsoft\\windows nt\\currentversion\\perflib") 
#define REGSUBKEY_COUNTERS  _T("Counters")
#define PROCESS_COUNTER     _T("process")
#define PROCESSID_COUNTER   _T("id process")
#define UNKNOWN_TASK        _T("unknown")
 
 
// 
// Function pointer types for accessing Toolhelp32 functions dynamically. 
// By dynamically accessing these functions, we can do so only on Windows 
// 95 and still run on Windows NT, which does not have these functions. 
// 
typedef BOOL (WINAPI *PROCESSWALK)(HANDLE hSnapshot, LPPROCESSENTRY32 lppe); 
typedef HANDLE (WINAPI *CREATESNAPSHOT)(DWORD dwFlags, DWORD th32ProcessID); 
 
 
// 
// prototypes 
// 
BOOL CALLBACK 
EnumWindowsProc( 
    HWND    hwnd, 
    DWORD   lParam 
    ); 
 
 
 
DWORD 
GetTaskList95( 
    PTASK_LIST  pTask, 
    DWORD       dwNumTasks 
    ) 
 
/*++ 
 
Routine Description: 
 
    Provides an API for getting a list of tasks running at the time of the 
    API call.  This function uses Toolhelp32to get the task list and is  
    therefore straight WIN32 calls that anyone can call. 
 
Arguments: 
 
    dwNumTasks       - maximum number of tasks that the pTask array can hold 
 
Return Value: 
 
    Number of tasks placed into the pTask array. 
 
--*/ 
 
{ 
   CREATESNAPSHOT pCreateToolhelp32Snapshot = NULL; 
   PROCESSWALK    pProcess32First           = NULL; 
   PROCESSWALK    pProcess32Next            = NULL; 
 
   HANDLE         hKernel        = NULL; 
   HANDLE         hProcessSnap   = NULL; 
   PROCESSENTRY32 pe32           = {0}; 
   DWORD          dwTaskCount    = 0; 
 
 
   // Guarantee to the code later on that we'll enum at least one task. 
   if (dwNumTasks == 0) 
      return 0; 
 
    // Obtain a module handle to KERNEL so that we can get the addresses of 
    // the 32-bit Toolhelp functions we need. 
    hKernel = GetModuleHandle(_T("KERNEL32.DLL")); 
 
    if (hKernel) 
    { 
        pCreateToolhelp32Snapshot = 
          (CREATESNAPSHOT)GetProcAddress((HMODULE) hKernel, "CreateToolhelp32Snapshot"); 
 
        pProcess32First = (PROCESSWALK)GetProcAddress((HMODULE)hKernel, 
                                                      "Process32First"); 
        pProcess32Next  = (PROCESSWALK)GetProcAddress((HMODULE)hKernel, 
                                                      "Process32Next"); 
    } 
     
    // make sure we got addresses of all needed Toolhelp functions. 
    if (!(pProcess32First && pProcess32Next && pCreateToolhelp32Snapshot)) 
       return 0; 
        
 
    // Take a snapshot of all processes currently in the system. 
    hProcessSnap = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    if (hProcessSnap == (HANDLE)-1) 
        return 0; 
 
    // Walk the snapshot of processes and for each process, get information 
    // to display. 
    dwTaskCount = 0; 
    pe32.dwSize = sizeof(PROCESSENTRY32);   // must be filled out before use 
    if (pProcess32First(hProcessSnap, &pe32)) 
    { 
        do 
        { 
            LPTSTR pCurChar; 
 
            // strip path and leave executabe filename splitpath 
            for (pCurChar = (pe32.szExeFile + lstrlen (pe32.szExeFile)); 
                 *pCurChar != '\\' && pCurChar != pe32.szExeFile;  
                 --pCurChar) 
 
            lstrcpy(pTask -> ProcessName, pCurChar); 
            pTask -> flags = 0; 
            pTask -> dwProcessId = pe32.th32ProcessID; 
 
            ++dwTaskCount;   // keep track of how many tasks we've got so far 
            ++pTask;         // get to next task info block. 
        } 
        while (dwTaskCount < dwNumTasks && pProcess32Next(hProcessSnap, &pe32)); 
    } 
    else 
        dwTaskCount = 0;    // Couldn't walk the list of processes. 
 
    // Don't forget to clean up the snapshot object... 
    CloseHandle (hProcessSnap); 
 
    return dwTaskCount; 
} 
 
 
DWORD 
GetTaskListNT( 
    PTASK_LIST  pTask, 
    DWORD       dwNumTasks 
    ) 
 
/*++ 
 
Routine Description: 
 
    Provides an API for getting a list of tasks running at the time of the 
    API call.  This function uses the registry performance data to get the 
    task list and is therefore straight WIN32 calls that anyone can call. 
 
Arguments: 
 
    dwNumTasks       - maximum number of tasks that the pTask array can hold 
 
Return Value: 
 
    Number of tasks placed into the pTask array. 
 
--*/ 
 
{ 
    DWORD                        rc; 
    HKEY                         hKeyNames; 
    DWORD                        dwType; 
    DWORD                        dwSize; 
    LPTSTR                       buf = NULL; 
    TCHAR                         szSubKey[1024]; 
    LANGID                       lid; 
    LPTSTR                        p; 
    LPTSTR                        p2; 
    PPERF_DATA_BLOCK             pPerf; 
    PPERF_OBJECT_TYPE            pObj; 
    PPERF_INSTANCE_DEFINITION    pInst; 
    PPERF_COUNTER_BLOCK          pCounter; 
    PPERF_COUNTER_DEFINITION     pCounterDef; 
    DWORD                        i; 
    DWORD                        dwProcessIdTitle; 
    DWORD                        dwProcessIdCounter; 
    TCHAR                         szProcessName[MAX_PATH]; 
    DWORD                        dwLimit = dwNumTasks - 1; 
 
 
 
    // 
    // Look for the list of counters.  Always use the neutral 
    // English version, regardless of the local language.  We 
    // are looking for some particular keys, and we are always 
    // going to do our looking in English.  We are not going 
    // to show the user the counter names, so there is no need 
    // to go find the corresponding name in the local language. 
    // 
    lid = MAKELANGID( LANG_ENGLISH, SUBLANG_NEUTRAL ); 
    _stprintf( szSubKey, _T("%s\\%03x"), REGKEY_PERF, lid ); 
    rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
                       szSubKey, 
                       0, 
                       KEY_READ, 
                       &hKeyNames 
                     ); 
    if (rc != ERROR_SUCCESS) { 
        goto exit; 
    } 
 
    // 
    // get the buffer size for the counter names 
    // 
    rc = RegQueryValueEx( hKeyNames, 
                          REGSUBKEY_COUNTERS, 
                          NULL, 
                          &dwType, 
                          NULL, 
                          &dwSize 
                        ); 
 
    if (rc != ERROR_SUCCESS) { 
        goto exit; 
    } 
 
    // 
    // allocate the counter names buffer 
    // 
    buf = (LPTSTR) malloc( dwSize ); 
    if (buf == NULL) { 
        goto exit; 
    } 
    memset( buf, 0, dwSize ); 
 
    // 
    // read the counter names from the registry 
    // 
    rc = RegQueryValueEx( hKeyNames, 
                          REGSUBKEY_COUNTERS, 
                          NULL, 
                          &dwType, 
                          (LPBYTE)buf, 
                          &dwSize 
                        ); 
 
    if (rc != ERROR_SUCCESS) { 
        goto exit; 
    } 
 
    // 
    // now loop thru the counter names looking for the following counters: 
    // 
    //      1.  "Process"           process name 
    //      2.  "ID Process"        process id 
    // 
    // the buffer contains multiple null terminated strings and then 
    // finally null terminated at the end.  the strings are in pairs of 
    // counter number and counter name. 
    // 
 
    p = buf; 
    while (*p) { 
        if (p > buf) { 
            for( p2=p-2; _istdigit(*p2); p2--) ; 
            } 
        if (_tcsicmp(p, PROCESS_COUNTER) == 0) { 
            // 
            // look backwards for the counter number 
            // 
            for( p2=p-2; _istdigit(*p2); p2--) ; 
            _tcscpy( szSubKey, p2+1 ); 
        } 
        else 
        if (_tcsicmp(p, PROCESSID_COUNTER) == 0) { 
            // 
            // look backwards for the counter number 
            // 
            for( p2=p-2; _istdigit(*p2); p2--) ; 
            dwProcessIdTitle = _ttol( p2+1 ); 
        } 
        // 
        // next string 
        // 
        p += (_tcslen(p) + 1); 
    } 
 
    // 
    // free the counter names buffer 
    // 
    free( buf ); 
 
 
    // 
    // allocate the initial buffer for the performance data 
    // 
    dwSize = INITIAL_SIZE; 
    buf = (LPTSTR)malloc( dwSize ); 
    if (buf == NULL) { 
        goto exit; 
    } 
    memset( buf, 0, dwSize ); 
 
 
    while (TRUE) { 
 
        rc = RegQueryValueEx( HKEY_PERFORMANCE_DATA, 
                              szSubKey, 
                              NULL, 
                              &dwType, 
                              (LPBYTE)buf, 
                              &dwSize 
                            ); 
 
        pPerf = (PPERF_DATA_BLOCK) buf; 
 
        // 
        // check for success and valid perf data block signature 
        // 
        if ((rc == ERROR_SUCCESS) && 
            (dwSize > 0) && 
            (pPerf)->Signature[0] == (WCHAR)'P' && 
            (pPerf)->Signature[1] == (WCHAR)'E' && 
            (pPerf)->Signature[2] == (WCHAR)'R' && 
            (pPerf)->Signature[3] == (WCHAR)'F' ) { 
            break; 
        } 
 
        // 
        // if buffer is not big enough, reallocate and try again 
        // 
        if (rc == ERROR_MORE_DATA) { 
            dwSize += EXTEND_SIZE; 
            buf = (LPTSTR)realloc( buf, dwSize ); 
            memset( buf, 0, dwSize ); 
        } 
        else { 
            goto exit; 
        } 
    } 
 
    // 
    // set the perf_object_type pointer 
    // 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -