diff --git a/CMakeLists.txt b/CMakeLists.txt index 4af4dd9..af16c0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,10 +44,17 @@ file(COPY ${THIRD} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/third/include/ FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) message( "copy net \n") +message( "copy function library \n") +file(GLOB FUNCTION ${PROJECT_SOURCE_DIR}/general/src/function/*.h) +message("function " ${FUNCTION}) +file(COPY ${FUNCTION} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/function/ + FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) + +message( "copy net \n") file(GLOB NET ${PROJECT_SOURCE_DIR}/general/src/net/*.h) -file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/ +file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/net/ FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) file(GLOB ALGORITHM ${PROJECT_SOURCE_DIR}/general/src/algorithm/*) -file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/ +file(COPY ${ALGORITHM} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/algorithm/ FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ) diff --git a/general/src/function/daemon.cpp b/general/src/function/daemon.cpp index ee5b791..cf9cc59 100644 --- a/general/src/function/daemon.cpp +++ b/general/src/function/daemon.cpp @@ -1,14 +1,77 @@ /* * @Author: your name * @Date: 2021-07-23 23:39:57 - * @LastEditTime: 2021-07-25 02:31:24 - * @LastEditors: your name + * @LastEditTime: 2021-07-26 00:28:04 + * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \generallib\general\src\function\daemon.c */ #include "daemon.h" -BOOL SetProcessPrivilege(char *lpName, BOOL opt) + +static BOOL KillProcess(DWORD dwPid) +{ + HANDLE hPrc; + + if (0 == dwPid) + return FALSE; + + hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process. + + if (!TerminateProcess(hPrc, 0)) // Terminates a process. + { + CloseHandle(hPrc); + return FALSE; + } + else + WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond. + + CloseHandle(hPrc); + return TRUE; +} + +static BOOL KillProcessByName(const TCHAR *lpszProcessName) +{ + unsigned int pid = -1; + BOOL retval = TRUE; + + if (lpszProcessName == NULL) + return -1; + + DWORD dwRet = 0; + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + PROCESSENTRY32 processInfo; + processInfo.dwSize = sizeof(PROCESSENTRY32); + int flag = Process32First(hSnapshot, &processInfo); + + // Find the process with name as same as lpszProcessName + while (flag != 0) + { + printf("kill process find %s\r\n",processInfo.szExeFile); + if (strcmp(processInfo.szExeFile, lpszProcessName) == 0) + { + // Terminate the process. + pid = processInfo.th32ProcessID; + HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); + printf("kill process pid %d\r\n",pid); + if (TerminateProcess(hProcess, 0) != TRUE) + { // Failed to terminate it. + retval = FALSE; + break; + } + } + + flag = Process32Next(hSnapshot, &processInfo); + } // while (flag != 0) + CloseHandle(hSnapshot); + if (pid == -1) + return FALSE; + + return retval; +} + + +static BOOL SetProcessPrivilege(char *lpName, BOOL opt) { HANDLE tokenhandle; TOKEN_PRIVILEGES NewState; @@ -28,7 +91,7 @@ BOOL SetProcessPrivilege(char *lpName, BOOL opt) } } -int RangeProcess() +static int RangeProcess() { DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; unsigned int i; @@ -55,7 +118,7 @@ int RangeProcess() return 0; } -int test_fork() +static int test_fork() { char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe"; STARTUPINFO si = {sizeof(si)}; @@ -83,3 +146,109 @@ int test_fork() } return 0; } + +DaemonizeMonitor::DaemonizeMonitor(string path){ + this->m_path_process.push_back(path); + +} + +int DaemonizeMonitor::AddNewProcess(string path){ + this->m_path_process.push_back(path); + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 + si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口 + BOOL bRet = CreateProcess( + NULL, //不在此指定可执行文件的文件名 + (LPSTR)path.c_str(), //命令行参数 + NULL, //默认进程安全性 + NULL, //默认进程安全性 + FALSE, //指定当前进程内句柄不可以被子进程继承 + CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 + NULL, //使用本进程的环境变量 + NULL, //使用本进程的驱动器和目录 + &si, + &pi); + if (bRet) + { + //不使用的句柄最好关掉 + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + + printf("new process id %d\n", pi.dwProcessId); + this->m_running_pid[path] = pi.dwProcessId; + return 0; + } + return -1; +} + +int DaemonizeMonitor::StartMonitor(){ + DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; + unsigned int i; + HMODULE hMod[1024]; + HANDLE hProcess; + char szModName[MAX_PATH]; + while (true) + { + /* code */ + Sleep(1000); + for(auto itr = m_running_pid.begin();itr != m_running_pid.end();itr++){ + bool found = false; + if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes)) + { + Proc_count = Retn_bytes / sizeof(DWORD); + SetProcessPrivilege("SeDebugPrivilege", 1); + for (i = 0; i < Proc_count; i++) + { + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]); + if (hProcess != NULL) + { + EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2); + GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName)); + printf("PID=%d Path=%s\n", Proc_pid[i], szModName); + if(itr->second == Proc_pid[i]){ + found = true; + break; + } + } + CloseHandle(hProcess); + } + SetProcessPrivilege("SeDebugPrivilege", 0); + if(!found){ + // 没找到该应用实例就重启应用 + for(int i = 0;i < m_path_process.size();i++){ + if(itr->first == m_path_process.at(i)){ + + } + } + this->AddNewProcess(itr->first); + } + } + } + + } + + return 0; +} + + +int DaemonizeMonitor::StopMonitor(string){ + + return 0; +} + +/* + +*/ +int DaemonizeMonitor::StopProcess(string path){ + if(m_running_pid.find(path) != m_running_pid.end()){ + DWORD pid = m_running_pid.at(path); + m_running_pid.erase(path); + if(KillProcess(pid)) + return 0; + else{ + return -1; + } + } + return 0; +} \ No newline at end of file diff --git a/general/src/function/daemon.h b/general/src/function/daemon.h index 0b24123..640c23d 100644 --- a/general/src/function/daemon.h +++ b/general/src/function/daemon.h @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-07-23 23:40:36 - * @LastEditTime: 2021-07-25 02:42:00 + * @LastEditTime: 2021-07-26 00:03:14 * @LastEditors: Please set LastEditors * @Description: In User Settings Edi * @FilePath: \generallib\general\src\function\daemon.h @@ -23,12 +23,16 @@ extern "C"{ #include #include #include - +#include +#include class DaemonizeMonitor{ public: DaemonizeMonitor(string path); - int Start(); int AddNewProcess(string); + int StopMonitor(string); + int StopProcess(string); + int StartMonitor(); private: std::vector m_path_process; + std::map m_running_pid; }; \ No newline at end of file diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index 7e5d39a..f0f2a3b 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-03-15 23:07:25 - * @LastEditTime: 2021-07-24 23:19:57 + * @LastEditTime: 2021-07-25 23:47:46 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \cpp11\cpp11_test.cpp @@ -25,6 +25,73 @@ extern "C"{ #include #include "loger.h" #include +#include "function/daemon.h" + +// [Added by thinkhy 09/12/20] +// Description: Kill process(es) by PID. +// Reference: http://www.vckbase.com/document/viewdoc/?id=1882 +// RETVALUE: SUCCESS TRUE +// FAILED FALSE +BOOL KillProcess(DWORD dwPid) +{ + HANDLE hPrc; + + if (0 == dwPid) + return FALSE; + + hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); // Opens handle to the process. + + if (!TerminateProcess(hPrc, 0)) // Terminates a process. + { + CloseHandle(hPrc); + return FALSE; + } + else + WaitForSingleObject(hPrc, 2000); // At most ,waite 2000 millisecond. + + CloseHandle(hPrc); + return TRUE; +} + +BOOL KillProcessByName(const TCHAR *lpszProcessName) +{ + unsigned int pid = -1; + BOOL retval = TRUE; + + if (lpszProcessName == NULL) + return -1; + + DWORD dwRet = 0; + HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + PROCESSENTRY32 processInfo; + processInfo.dwSize = sizeof(PROCESSENTRY32); + int flag = Process32First(hSnapshot, &processInfo); + + // Find the process with name as same as lpszProcessName + while (flag != 0) + { + printf("kill process find %s\r\n",processInfo.szExeFile); + if (strcmp(processInfo.szExeFile, lpszProcessName) == 0) + { + // Terminate the process. + pid = processInfo.th32ProcessID; + HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid); + printf("kill process pid %d\r\n",pid); + if (TerminateProcess(hProcess, 0) != TRUE) + { // Failed to terminate it. + retval = FALSE; + break; + } + } + + flag = Process32Next(hSnapshot, &processInfo); + } // while (flag != 0) + CloseHandle(hSnapshot); + if (pid == -1) + return FALSE; + + return retval; +} BOOL SetProcessPrivilege(char *lpName, BOOL opt) { @@ -73,9 +140,12 @@ int RangeProcess() return 0; } -int test_fork() +int test_fork(char *szCommandLine) { - char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe"; + if(nullptr == szCommandLine) + { + return -1; + } STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi; si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 @@ -111,12 +181,19 @@ int main(int argc, char **argv) // }catch( std::exception e){ // std::cout<<"exception"< // i;