Changeset 436

Show
Ignore:
Timestamp:
09/06/08 13:20:20 (4 months ago)
Author:
t-bone
Message:

list threads with PDH

Location:
VidSave
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • VidSave/VidSave.cpp

    r435 r436  
    11#include "stdafx.h" 
    22 
    3 bool ScanModules(DWORD processID) { 
    4     HMODULE hMods[1024]; 
    5     HANDLE hProcess; 
    6     DWORD cbNeeded; 
     3// http://msdn.microsoft.com/en-us/library/aa372151(VS.85).aspx 
     4int _tmain () { 
     5    PDH_STATUS  pdhStatus               = ERROR_SUCCESS; 
     6    LPTSTR      szCounterListBuffer     = NULL; 
     7    DWORD       dwCounterListSize       = 0; 
     8    LPTSTR      szInstanceListBuffer    = NULL; 
     9    DWORD       dwInstanceListSize      = 0; 
     10    LPTSTR      szThisInstance          = NULL; 
    711 
    8     // Get a list of all the modules in this process. 
    9     hProcess=OpenProcess( 
    10         PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID 
    11     ); 
    12     if (NULL==hProcess) return 0; 
    1312 
    14     if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) { 
    15         for (unsigned int i=0; i<cbNeeded/sizeof(HMODULE); i++) { 
    16             TCHAR szModName[MAX_PATH]; 
     13    // Determine the required buffer size for the data.  
     14    pdhStatus = PdhEnumObjectItems ( 
     15        NULL,                   // real time source 
     16        NULL,                   // local machine 
     17        TEXT("Thread"),        // object to enumerate 
     18        szCounterListBuffer,    // pass NULL and 0 
     19        &dwCounterListSize,     // to get length required 
     20        szInstanceListBuffer,   // buffer size  
     21        &dwInstanceListSize,    //  
     22        PERF_DETAIL_WIZARD,     // counter detail level 
     23        0);  
    1724 
    18             if (GetModuleBaseName( 
    19                 hProcess, hMods[i], szModName, sizeof(szModName)/sizeof(TCHAR) 
    20             )) { 
    21                 if (0==_wcsicmp(szModName, _T("npswf32.dll"))) { 
    22                     CloseHandle(hProcess); 
    23                     return 1; 
    24                 } 
    25             } 
    26         } 
    27     } 
     25    if (pdhStatus == PDH_MORE_DATA)  
     26    { 
     27        // Allocate the buffers and try the call again. 
     28        szCounterListBuffer = (LPTSTR)malloc ( 
     29            (dwCounterListSize * sizeof (TCHAR))); 
     30        szInstanceListBuffer = (LPTSTR)malloc ( 
     31            (dwInstanceListSize * sizeof (TCHAR))); 
    2832 
    29     CloseHandle(hProcess); 
    30     return 0; 
     33        if ((szCounterListBuffer != NULL) && 
     34            (szInstanceListBuffer != NULL))  
     35        { 
     36            pdhStatus = PdhEnumObjectItems ( 
     37                NULL,                 // real time source 
     38                NULL,                 // local machine 
     39                TEXT("Thread"),      // object to enumerate 
     40                szCounterListBuffer,  // buffer to receive counter list 
     41                &dwCounterListSize,  
     42                szInstanceListBuffer, // buffer to receive instance list  
     43                &dwInstanceListSize,     
     44                PERF_DETAIL_WIZARD,   // counter detail level 
     45                0); 
     46      
     47            if (pdhStatus == ERROR_SUCCESS)  
     48            { 
     49                _tprintf (TEXT("\nEnumerating Processes:")); 
     50 
     51                // Walk the instance list. The list can contain one 
     52                // or more null-terminated strings. The last string  
     53                // is followed by a second null-terminator. 
     54                for (szThisInstance = szInstanceListBuffer; 
     55                     *szThisInstance != 0; 
     56                     szThisInstance += lstrlen(szThisInstance) + 1)  
     57                { 
     58                     _tprintf (TEXT("\n  %s"), szThisInstance); 
     59                } 
     60            } 
     61            else  
     62            { 
     63                _tprintf(TEXT("\nPdhEnumObjectItems failed with %ld."), pdhStatus); 
     64            } 
     65        }  
     66        else  
     67        { 
     68            _tprintf (TEXT("\nUnable to allocate buffers")); 
     69            pdhStatus = ERROR_OUTOFMEMORY; 
     70        } 
     71 
     72        if (szCounterListBuffer != NULL)  
     73            free (szCounterListBuffer); 
     74 
     75        if (szInstanceListBuffer != NULL)  
     76            free (szInstanceListBuffer); 
     77    }  
     78    else  
     79    { 
     80        _tprintf(TEXT("\nPdhEnumObjectItems failed with %ld."), pdhStatus); 
     81    } 
     82 
     83    _getch(); 
     84    return pdhStatus; 
    3185} 
    32  
    33 bool FindFlash() { 
    34     DWORD aProcesses[1024], cbNeeded; 
    35  
    36     // Get the list of process identifiers. 
    37     if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) { 
    38         return 0; 
    39     } 
    40  
    41     // Calculate how many process identifiers were returned. 
    42     DWORD cProcesses=cbNeeded/sizeof(DWORD); 
    43  
    44     for (unsigned int i=0; i<cProcesses; i++) { 
    45         if (ScanModules(aProcesses[i])) return 1; 
    46     } 
    47  
    48     return 0; 
    49 } 
    50  
    51 void _tmain() { 
    52     printf("Found flash? %s\n", FindFlash()?"Yes":"No"); 
    53     _getch(); 
    54 } 
  • VidSave/VidSave.vcproj

    r435 r436  
    6161            <Tool 
    6262                Name="VCLinkerTool" 
    63                 AdditionalDependencies="kernel32.lib psapi.lib user32.lib $(NOINHERIT)" 
     63                AdditionalDependencies="kernel32.lib pdh.lib $(NOINHERIT)" 
    6464                LinkIncremental="2" 
    6565                GenerateDebugInformation="true" 
  • VidSave/stdafx.h

    r432 r436  
    88// The stuff I put here. 
    99#include <windows.h> 
    10 #include <psapi.h> 
     10#include <winperf.h> 
     11#include <malloc.h> 
     12#include <stdio.h> 
     13#include <tchar.h> 
     14#include <pdh.h> 
     15#include <pdhmsg.h> 
     16 
    1117#include <conio.h>