开启停止服务,强杀进程
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
查服务状态 DWORD DetectServiceStats( LPCTSTR pszName ) { SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE); if( schSCManager == NULL ) { return -1; } SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_QUERY_STATUS); if(schService == NULL) { ::CloseServiceHandle(schSCManager); return -1; } SERVICE_STATUS ssStatus; BOOL bRet = ::QueryServiceStatus(schService, &ssStatus); ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; return -1; } 开启服务 DWORD StartService( LPCTSTR pszName ) { if(DetectServiceStats(pszName) == SERVICE_RUNNING) return 0; SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if( schSCManager == NULL ) return -1; SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_ALL_ACCESS); if( schService== NULL ) { ::CloseServiceHandle(schSCManager); return -1; } SERVICE_STATUS ssStatus; BOOL bRet = ::StartService(schService, 0, 0); if(bRet) { Sleep(1000); while( ::QueryServiceStatus(schService, &ssStatus)) { if(ssStatus.dwCurrentState == SERVICE_START_PENDING ) Sleep(1000); else break; } if(ssStatus.dwCurrentState != SERVICE_RUNNING) bRet = FALSE; } ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; return -1; } 关闭服务 DWORD StopService( LPCTSTR pszName ) { if(DetectServiceStats(pszName) == SERVICE_STOPPED) { ASSERT(0); return 0; } SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if( schSCManager == NULL ) { ASSERT(0); return -1; } SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_ALL_ACCESS); if( schService== NULL ) { ASSERT(0); ::CloseServiceHandle(schSCManager); return -1; } SERVICE_STATUS ssStatus; BOOL bRet = ControlService(schService,SERVICE_CONTROL_STOP,&ssStatus); if(bRet) { if(ssStatus.dwCurrentState == SERVICE_STOP_PENDING) { Sleep(1000); while( ::QueryServiceStatus(schService, &ssStatus)) { if(ssStatus.dwCurrentState == SERVICE_START_PENDING ) Sleep(1000); else break; } } if(ssStatus.dwCurrentState != SERVICE_STOPPED) bRet = FALSE; } else { ASSERT(0); } ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; ASSERT(0); return -1; } 杀进程 BOOL FindAndKillProcessByName(LPCTSTR strProcessName) { if(NULL == strProcessName) { return FALSE; } HANDLE handle32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE == handle32Snapshot) { return FALSE; } PROCESSENTRY32 pEntry; pEntry.dwSize = sizeof( PROCESSENTRY32 ); //Search for all the process and terminate it if(Process32First(handle32Snapshot, &pEntry)) { BOOL bFound = FALSE; if (!_tcsicmp(pEntry.szExeFile, strProcessName)) { bFound = TRUE; } while((!bFound)&&Process32Next(handle32Snapshot, &pEntry)) { if (!_tcsicmp(pEntry.szExeFile, strProcessName)) { bFound = TRUE; } } if(bFound) { CloseHandle(handle32Snapshot); HANDLE handLe = OpenProcess(PROCESS_TERMINATE , FALSE, pEntry.th32ProcessID); BOOL bResult = TerminateProcess(handLe,0); return bResult; } } CloseHandle(handle32Snapshot); return FALSE; } 强杀进程 ShellExecuteW(NULL, L"open", L"taskkill", L"/F /IM edpa.exe /T", NULL, SW_HIDE); |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=283