• 进程操作


    一般要包括

    #include <psapi.h>

    • 创建进程
     1 bool CSysInfoHelper::RunProcess(const CString &commandLine, bool isWait, WORD showWindow, DWORD dwMilliseconds)
     2 {
     3     /*
     4     1.WinExec("cmd.exe",SW_SHOW);可以隐藏程序界面,一般在后台隐藏执行命令行或脚本时使用
     5     2.system("cmd");直接执行命令,不可以隐藏界面
     6     3.CreateProcess,一般执行带界面的进程,可以隐藏界面,隐藏方法要同时设置dwFlags和wShowWindow才可以生效
     7     */
     8 
     9     STARTUPINFO si;
    10     ZeroMemory(&si, sizeof(si));
    11     si.cb = sizeof(si);
    12     si.dwFlags = STARTF_USESHOWWINDOW;
    13     si.wShowWindow = showWindow;
    14 
    15     PROCESS_INFORMATION pi;
    16     ZeroMemory(&pi, sizeof(pi));
    17 
    18     BOOL flag = CreateProcessW(
    19         NULL,                            // No module name (use command line). 
    20         (LPTSTR)(LPCTSTR)commandLine,    // Command line. 
    21         NULL,                            // Process handle not inheritable. 
    22         NULL,                            // Thread handle not inheritable. 
    23         FALSE,                            // Set handle inheritance to FALSE. 
    24         0,                                // No creation flags. 
    25         NULL,                            // Use parent's environment block. 
    26         NULL,                            // Use parent's starting directory. 
    27         &si,                            // Pointer to STARTUPINFO structure.
    28         &pi);                            // Pointer to PROCESS_INFORMATION structure.
    29     
    30     _ASSERTE(flag);
    31     if (!flag)
    32     {
    33         DWORD error = GetLastError();
    34         //AfxMessageBox(L"创建进程失败!");
    35         return false;
    36     }
    37 
    38     bool result = true;
    39 
    40     if (isWait)
    41     {
    42         DWORD rt = WaitForSingleObject(pi.hProcess, dwMilliseconds);
    43 
    44         if (rt != WAIT_OBJECT_0)
    45         {
    46             DWORD error = GetLastError();
    47             result = false;
    48         }
    49     }
    50 
    51     // 关闭子进程的主线程句柄
    52     CloseHandle(pi.hThread);
    53     // 关闭子进程句柄
    54     CloseHandle(pi.hProcess);
    55     
    56     return result;
    57 }
    • 查找进程
     1 int CSysInfoHelper::SearchTheProcessWithName(const CString &processName, DWORD processID)
     2 {
     3     TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
     4 
     5     // Get a handle to the process.
     6     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
     7 
     8     // Get the process name.
     9     if (NULL != hProcess )
    10     {
    11         HMODULE hMod;
    12         DWORD cbNeeded;
    13 
    14         if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
    15         {
    16             GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR));
    17         }
    18 
    19         CloseHandle( hProcess );
    20     }
    21     else
    22     {
    23         return -1;
    24     }
    25 
    26 //     CStdioFile file;
    27 //     file.Open(L"aaaaaaa.log", CFile::modeCreate | CFile::modeReadWrite);
    28 //     file.Write(szProcessName, MAX_PATH);
    29 //     file.Write(L"====", 5);
    30 
    31     _wcsupr_s(szProcessName, wcslen(szProcessName) + 1); 
    32 
    33     if (0 == processName.Compare(szProcessName))
    34     {
    35         //AfxMessageBox(szProcessName);
    36         return 1;
    37     }
    38 
    39     return 0;
    40 }
     // 通过带有扩展的名称+全路径文件名 找进程id
    1
    int CSysInfoHelper::FindProcessByNameAndPath(const CString &processName, const CString &processFullName, DWORD *pHandleArray, int arrayNum) 2 { 3 // Get the list of process identifiers. 4 DWORD aProcesses[1024], cbNeeded, cProcesses; 5 HANDLE hProcessHandle; 6 TCHAR szPath[MAX_PATH]; 7 8 if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) 9 { 10 return -1; 11 } 12 13 // Calculate how many process identifiers were returned. 14 cProcesses = cbNeeded / sizeof(DWORD); 15 16 int findNum = 0; 17 18 // Find the process name. 19 for (DWORD i = 0; i < cProcesses; i++) 20 { 21 if(aProcesses[i] != 0) 22 { 23 if (SearchTheProcessWithName(processName, aProcesses[i]) > 0) 24 { 25 hProcessHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, aProcesses[i]); 26 if (hProcessHandle != NULL) 27 { 28 int length = GetModuleFileNameEx(hProcessHandle, NULL, szPath, MAX_PATH); 29 if (length > 0) 30 { 31 CString temp = szPath; 32 temp.MakeUpper(); 33 34 CString temp11 = processFullName; 35 temp11.MakeUpper(); 36 37 if (temp == temp11) 38 { 39 ++findNum; 40 41 if (findNum >= arrayNum) 42 { 43 CloseHandle(hProcessHandle); 44 break; 45 } 46 else 47 { 48 if (pHandleArray != NULL) 49 { 50 pHandleArray[findNum - 1] = aProcesses[i]; 51 } 52 } 53 } 54 } 55 56 CloseHandle(hProcessHandle); 57 } 58 } 59 } 60 } 61 62 return findNum; 63 }
     // 通过带有扩展的名称找进程id
    1
    int CSysInfoHelper::FindProcessByName(const CString &processName, DWORD *pHandleArray, int arrayNum) 2 { 3 // Get the list of process identifiers. 4 DWORD aProcesses[1024], cbNeeded, cProcesses; 5 6 if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) 7 { 8 return -1; 9 } 10 11 // Calculate how many process identifiers were returned. 12 cProcesses = cbNeeded / sizeof(DWORD); 13 14 int flag = 0; 15 int findNum = 0; 16 17 // Find the process name. 18 for (DWORD i = 0; i < cProcesses; i++) 19 { 20 if(aProcesses[i] != 0) 21 { 22 if (SearchTheProcessWithName(processName, aProcesses[i]) > 0) 23 { 24 if (NULL == pHandleArray) 25 { 26 return 1; 27 } 28 29 flag = 1; 30 ++findNum; 31 32 if (findNum >= arrayNum) 33 { 34 break; 35 } 36 else 37 { 38 pHandleArray[findNum - 1] = aProcesses[i]; 39 } 40 } 41 } 42 } 43 44 return findNum; 45 }
     // 通过类名、路径、文件名,找进程id
    1
    int CSysInfoHelper::GetProcessIDListByClassName(const CString &className, CString execPath, 2 CString execName, DWORD *pHandleArray, int num) 3 { 4 HANDLE hProcessHandle; 5 HWND theWindow; 6 7 theWindow = ::FindWindow(NULL, NULL); 8 9 TCHAR buffer[MAX_PATH]; 10 TCHAR szPath[MAX_PATH]; 11 12 int i = 0; 13 14 while (theWindow != 0) 15 { 16 GetClassName(theWindow, buffer, MAX_PATH); 17 //CCommonLog::WLogf(g_logId, L_ERROR, buffer); 18 if (0 == wcscmp(buffer, className)) 19 { 20 DWORD processId = 0; 21 GetWindowThreadProcessId(theWindow, &processId); 22 hProcessHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, processId); 23 if (processId != 0 && hProcessHandle != NULL) 24 { 25 int length = GetModuleFileNameEx(hProcessHandle, NULL, szPath, MAX_PATH); 26 27 if (length >= 0 ) 28 { 29 CString pathFile = szPath; 30 int position = pathFile.ReverseFind(L'\'); 31 CString path = pathFile.Left(position); 32 CString name = pathFile.Right(length - position - 1); 33 if (path.Find(execPath.MakeUpper()) != -1 && name.Find(execName.MakeUpper()) != -1) 34 { 35 if (i >= num) 36 { 37 return i; 38 } 39 else 40 { 41 pHandleArray[i] = processId; 42 ++i; 43 } 44 } 45 } 46 47 CloseHandle(hProcessHandle); 48 } 49 } 50 51 theWindow = GetNextWindow(theWindow, GW_HWNDNEXT); 52 } 53 54 return i; 55 }
    • 通过进程id杀进程
     1 bool CSysInfoHelper::KillProcess(DWORD *pProcessIdArray, int num)
     2 {
     3     if (NULL == pProcessIdArray || num <= 0)
     4     {
     5         return false;
     6     }
     7 
     8     HANDLE hProcessHandle = NULL;  
     9     int i = 0;
    10     for (; i < num; ++i)
    11     {
    12         if (pProcessIdArray[i] >= 0 )
    13         {
    14             hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, pProcessIdArray[i]);
    15             if (hProcessHandle != NULL)
    16             {
    17                 ::TerminateProcess(hProcessHandle, 4);
    18                 CloseHandle(hProcessHandle);
    19             }        
    20         }
    21     }
    22 
    23     return true;
    24 }
     1 bool CSysInfoHelper::KillProcessByRunName(const CString &runName)
     2 {
     3     DWORD processIdArray[MAX_PATH];
     4 
     5     int num = FindProcessByName(runName, processIdArray, MAX_PATH);
     6     if (num > 0)
     7     {
     8         KillProcess(processIdArray, num);
     9     }
    10 
    11     return true;
    12 }
     1 bool CSysInfoHelper::KillProcessByClassName(const CString &className)
     2 {
     3     HANDLE hProcessHandle;  
     4     ULONG nProcessID;
     5     HWND theWindow;
     6 
     7     theWindow = ::FindWindow(NULL, NULL);
     8 
     9     TCHAR buffer[MAX_PATH];
    10     while (theWindow != 0)
    11     {
    12         GetClassName(theWindow, buffer, MAX_PATH);
    13         if (0 == wcscmp(buffer, className))
    14         {
    15             ::GetWindowThreadProcessId(theWindow, &nProcessID);
    16             if (nProcessID >= 0 )
    17             {
    18                 hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, nProcessID);
    19                 if (hProcessHandle != NULL)
    20                 {
    21                     ::TerminateProcess(hProcessHandle, 4);
    22                     CloseHandle(hProcessHandle);
    23                 }        
    24             }
    25         }
    26     
    27         theWindow = GetNextWindow(theWindow, GW_HWNDNEXT);
    28     }
    29 
    30     return true;
    31 }
  • 相关阅读:
    用js实现cookie的读、写、全部删除和删除指定cookie值的删除---源码
    JS手机号码格式验证
    vuex
    解决“此图片来自微信公众平台未经允许不可引用”的方法
    二叉树
    剑指 Offer 10- II. 青蛙跳台阶问题
    logrotate处理Gunicorn日志
    Linux日志切割神器logrotate原理介绍和配置详解
    Jenkins任务启动的后台进程被自动kill
    Flex、Grid、媒体查询实现响应式布局
  • 原文地址:https://www.cnblogs.com/spriteflk/p/4512525.html
Copyright © 2020-2023  润新知