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

📄 common.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    pObj = (PPERF_OBJECT_TYPE) ((DWORD)pPerf + pPerf->HeaderLength); 
 
    // 
    // loop thru the performance counter definition records looking 
    // for the process id counter and then save its offset 
    // 
    pCounterDef = (PPERF_COUNTER_DEFINITION) ((DWORD)pObj + pObj->HeaderLength); 
    for (i=0; i<(DWORD)pObj->NumCounters; i++) { 
        if (pCounterDef->CounterNameTitleIndex == dwProcessIdTitle) { 
            dwProcessIdCounter = pCounterDef->CounterOffset; 
            break; 
        } 
        pCounterDef++; 
    } 
 
    dwNumTasks = min( dwLimit, (DWORD)pObj->NumInstances ); 
 
    pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD)pObj + pObj->DefinitionLength); 
 
    // 
    // loop thru the performance instance data extracting each process name 
    // and process id 
    // 
    for (i=0; i<dwNumTasks; i++) { 
        // 
        // pointer to the process name 
        // 
        p = (LPTSTR) ((DWORD)pInst + pInst->NameOffset); 
 
        // 
        // convert it to ascii 
        // 
#ifndef _UNICODE
        rc = WideCharToMultiByte( CP_ACP, 
                                  0, 
                                  (LPCWSTR)p, 
                                  -1, 
                                  szProcessName, 
                                  sizeof(szProcessName), 
                                  NULL, 
                                  NULL 
                                ); 
#else 
		rc = _tcslen(_tcscpy(szProcessName,p));
#endif
 
        if (!rc) { 
            // 
    // if we cant convert the string then use a default value 
            // 
            _tcscpy( pTask->ProcessName, UNKNOWN_TASK ); 
        } 
 
        if (_tcslen(szProcessName)+4 <= sizeof(pTask->ProcessName)) { 
            _tcscpy( pTask->ProcessName, szProcessName ); 
            _tcscat( pTask->ProcessName, _T(".exe") ); 
        } 
 
        // 
        // get the process id 
        // 
        pCounter = (PPERF_COUNTER_BLOCK) ((DWORD)pInst + pInst->ByteLength); 
        pTask->flags = 0; 
        pTask->dwProcessId = *((LPDWORD) ((DWORD)pCounter + dwProcessIdCounter)); 
        if (pTask->dwProcessId == 0) { 
            pTask->dwProcessId = (DWORD)-2; 
        } 
 
        // 
        // next process 
        // 
        pTask++; 
        pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD)pCounter + pCounter->ByteLength); 
    } 
 
exit: 
    if (buf) { 
        free( buf ); 
    } 
 
    RegCloseKey( hKeyNames ); 
    RegCloseKey( HKEY_PERFORMANCE_DATA ); 
 
    return dwNumTasks; 
} 
 
 
BOOL 
EnableDebugPriv95( 
    VOID 
    ) 
 
/*++ 
 
Routine Description: 
 
    Changes the process's privilege so that kill works properly. 
 
Arguments: 
 
 
Return Value: 
 
    TRUE             - success 
    FALSE            - failure 
 
Comments:  
    Always returns TRUE 
 
--*/ 
 
{ 
   return TRUE; 
} 
 
 
 
BOOL 
EnableDebugPrivNT( 
    VOID 
    ) 
 
/*++ 
 
Routine Description: 
 
    Changes the process's privilege so that kill works properly. 
 
Arguments: 
 
 
Return Value: 
 
    TRUE             - success 
    FALSE            - failure 
 
--*/ 
 
{ 
    HANDLE hToken; 
    LUID DebugValue; 
    TOKEN_PRIVILEGES tkp; 
 
 
    // 
    // Retrieve a handle of the access token 
    // 
    if (!OpenProcessToken(GetCurrentProcess(), 
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, 
            &hToken)) { 
        printf("OpenProcessToken failed with %d\n", GetLastError()); 
        return FALSE; 
    } 
 
    // 
    // Enable the SE_DEBUG_NAME privilege 
    // 
    if (!LookupPrivilegeValue((LPTSTR) NULL, 
            SE_DEBUG_NAME, 
            &DebugValue)) { 
        printf("LookupPrivilegeValue failed with %d\n", GetLastError()); 
        return FALSE; 
    } 
 
    tkp.PrivilegeCount = 1; 
    tkp.Privileges[0].Luid = DebugValue; 
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 
    AdjustTokenPrivileges(hToken, 
        FALSE, 
        &tkp, 
        sizeof(TOKEN_PRIVILEGES), 
        (PTOKEN_PRIVILEGES) NULL, 
        (PDWORD) NULL); 
 
    // 
    // The return value of AdjustTokenPrivileges can't be tested 
    // 
    if (GetLastError() != ERROR_SUCCESS) { 
        printf("AdjustTokenPrivileges failed with %d\n", GetLastError()); 
        return FALSE; 
    } 
 
    return TRUE; 
} 
 
BOOL 
KillProcess( 
    PTASK_LIST tlist, 
    BOOL       fForce 
    ) 
{ 
    HANDLE            hProcess; 
 
 
    if (fForce || !tlist->hwnd) { 
        hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId ); 
        if (hProcess) { 
            hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId ); 
            if (hProcess == NULL) { 
                return FALSE; 
            } 
 
            if (!TerminateProcess( hProcess, 1 )) { 
                CloseHandle( hProcess ); 
                return FALSE; 
            } 
 
            CloseHandle( hProcess ); 
            return TRUE; 
        } 
    } 
 
    // 
    // kill the process 
    // 
    PostMessage( (HWND)tlist->hwnd, WM_CLOSE, 0, 0 ); 
 
    return TRUE; 
} 
 
 
VOID 
GetWindowTitles( 
    PTASK_LIST_ENUM te 
    ) 
{ 
    // 
    // enumerate all windows 
    // 
    EnumWindows( (int (__stdcall *)(struct HWND__ *,long))EnumWindowsProc, (LPARAM) te ); 
} 
  
BOOL CALLBACK 
EnumWindowsProc( 
    HWND    hwnd, 
    DWORD   lParam 
    ) 
 
/*++ 
 
Routine Description: 
 
    Callback function for window enumeration. 
 
Arguments: 
 
    hwnd             - window handle 
    lParam           - ** not used ** 
 
Return Value: 
 
    TRUE  - continues the enumeration 
 
--*/ 
 
{ 
    DWORD             pid = 0; 
    DWORD             i; 
    TCHAR              buf[TITLE_SIZE]; 
    PTASK_LIST_ENUM   te = (PTASK_LIST_ENUM)lParam; 
    PTASK_LIST        tlist = te->tlist; 
    DWORD             numTasks = te->numtasks; 
 
 
    // 
    // get the processid for this window 
    // 
    if (!GetWindowThreadProcessId( hwnd, &pid )) { 
        return TRUE; 
    } 
 
    // 
    // look for the task in the task list for this window 
    // 
    for (i=0; i<numTasks; i++) { 
        if (tlist[i].dwProcessId == pid) { 
            tlist[i].hwnd = hwnd; 
            // 
    // we found the task so lets try to get the 
            // window text 
            // 
            if (GetWindowText( (HWND)tlist[i].hwnd, buf, sizeof(buf) )) { 
                // 
// got it, so lets save it 
                // 
                _tcscpy( tlist[i].WindowTitle, buf ); 
            } 
            break; 
        } 
    } 
 
    // 
    // continue the enumeration 
    // 
    return TRUE; 
} 
 
 
BOOL 
MatchPattern( 
    LPTSTR String, 
    LPTSTR Pattern 
    ) 
{ 
    TCHAR   c, p, l; 
 
    for (; ;) { 
        switch (p = *Pattern++) { 
            case 0:                             // end of pattern 
                return *String ? FALSE : TRUE;  // if end of string TRUE 
 
            case _T('*'): 
                while (*String) {               // match zero or more char 
                    if (MatchPattern (String++, Pattern)) 
                        return TRUE; 
                } 
                return MatchPattern (String, Pattern); 
 
            case _T('?'): 
                if (*String++ == 0)             // match any one char 
                    return FALSE;                   // not end of string 
                break; 
 
            case _T('['): 
                if ( (c = *String++) == 0)      // match char set 
                    return FALSE;                   // syntax 
 
                c = _totupper(c); 
                l = 0; 
                while (p = *Pattern++) { 
                    if (p == ']')               // if end of char set, then 
                        return FALSE;           // no match found 
 
                    if (p == '-') {             // check a range of chars? 
                        p = *Pattern;           // get high limit of range 
                        if (p == 0  ||  p == ']') 
                            return FALSE;           // syntax 
 
                        if (c >= l  &&  c <= p) 
                            break;              // if in range, move on 
                    } 
 
                    l = p; 
                    if (c == p)                 // if char matches this element 
                        break;                  // move on 
                } 
 
                while (p  &&  p != ']')         // got a match in char set 
                    p = *Pattern++;             // skip to end of set 
 
                break; 
 
            default: 
                c = *String++; 
                if (_totupper(c) != p)            // check for exact char 
                    return FALSE;                   // not a match 
 
                break; 
        } 
    } 
} 

⌨️ 快捷键说明

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