123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #include "Assist.h"
- #include <time.h>
- #include <locale.h>
- #include "CritSection.h"
- namespace Assist
- {
- int frameWidth = 2;//6
- int titleBarHeight = 27;//29
- ThreadSection g_csTextLog;
- TCHAR g_szCurModuleFileName[MAX_PATH];
- TCHAR g_szCurModuleDir[MAX_PATH];
- TCHAR g_szCurModulePath[MAX_PATH];
- TCHAR g_szFna[_MAX_FNAME];
- TCHAR g_szExt[_MAX_EXT];
- TCHAR g_szAssistConfig[MAX_PATH];
- std::string g_strAppdir;
- std::string g_strGameDir;
- TCHAR g_szGameApp[MAX_PATH];
- int nGameWndType;
- int nAttackCount = 0;
- void Init()
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_FNAME] = { 0 };
- TCHAR szExt[_MAX_EXT] = { 0 };
- DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
- _tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
- _tcscat_s(g_szCurModuleDir, MAX_PATH, szDrive);
- _tcscat_s(g_szCurModuleDir, MAX_PATH, szDir);
- //g_strAppdir = g_szCurModuleDir;
- _stprintf_s(g_szAssistConfig, _T("%s%s"), g_szCurModuleDir, _T("Assist.ini"));
- TCHAR szValue[MAX_PATH] = { 0 };
- GetPrivateProfileString(_T("Assist"), _T("GamePath"), _T(""), szValue, MAX_PATH, g_szAssistConfig);
- //g_strGameDir = szValue;
- GetPrivateProfileString(_T("Assist"), _T("GameApp"), _T(""), g_szGameApp, MAX_PATH, g_szAssistConfig);
- // 读取窗口类型;
- _stprintf_s(szValue, _T("%s%s"), g_szGameApp, _T("save\\config.ini"));
- int ScreenWidth = GetPrivateProfileInt(_T("SysCfg"), _T("ScreenWidth"), 640, szValue);
- // Alt+A次数;
- nAttackCount = GetPrivateProfileInt(_T("SysCfg"), _T("AttackCount"), 120, szValue);
- }
- void SetWindowForeground(HWND hWnd)
- {
- if (!IsWindow(hWnd))
- return;
- ::ShowWindow(hWnd, SW_SHOWNORMAL);
- ::SetForegroundWindow(hWnd); // 窗口前置才能单击成功;
- }
- bool IsWindowForeground(HWND hWnd)
- {
- return false;
- }
- bool StartApp(const TCHAR* szAppPath, const TCHAR* szAppArgs)
- {
- if (!PathFileExists(szAppPath))
- {
- WriteTextLog(_T("Error, %ls path not exists!\n"), szAppPath);
- return false;
- }
- SHELLEXECUTEINFO ShExecInfo;
- ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
- ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
- ShExecInfo.hwnd = NULL;
- ShExecInfo.lpVerb = _T("open");
- ShExecInfo.lpFile = szAppPath;
- ShExecInfo.lpParameters = szAppArgs;
- ShExecInfo.lpDirectory = NULL;
- ShExecInfo.nShow = SW_SHOW;
- ShExecInfo.hInstApp = NULL;
- if (ShellExecuteEx(&ShExecInfo))
- {
- CloseHandle(ShExecInfo.hProcess);
- return true;
- }
- WriteTextLog(_T("启动进程失败:%d"), GetLastError());
- return false;
- }
- void CloseProcess(LPCTSTR lpProcessName)
- {
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == NULL) return;
- // 遍历进程;
- pe32.dwSize = sizeof(PROCESSENTRY32);
- for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
- if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
- HANDLE hProc = ::OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
- TerminateProcess(hProc, 4);
- }
- }
- CloseHandle(hProcessSnap);
- }
- // 返回第一个找到的进程pid
- DWORD GetProcessId(LPCTSTR lpProcessName)
- {
- DWORD dwProcessId = 0;
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == NULL) return 0;
- // 遍历进程;
- pe32.dwSize = sizeof(PROCESSENTRY32);
- for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
- if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
- dwProcessId = pe32.th32ProcessID;
- break;
- }
- }
- CloseHandle(hProcessSnap);
- return dwProcessId;
- }
- void GetAllProcessId(std::vector<DWORD>& vtPid, LPCTSTR lpProcessName)
- {
- vtPid.clear();
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == NULL) return;
- // 遍历进程;
- pe32.dwSize = sizeof(PROCESSENTRY32);
- for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
- if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
- vtPid.push_back(pe32.th32ProcessID);
- GetClassNameWnd(pe32.th32ProcessID, _T("Intermediate D3D Window"));
- GetWindowNameWnd(pe32.th32ProcessID, _T("Feishu Rooms"));
- }
- }
- CloseHandle(hProcessSnap);
- }
- HWND GetProcessMainWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWndName)
- {
- DWORD dwCurPorcessId = 0;
- HWND hTagetProcessWnd = NULL;
- TCHAR szWndName[MAX_PATH] = { 0 };
- TCHAR szClassName[MAX_PATH] = { 0 };
- // 取得第一个窗口句柄;
- for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
- // 重置为0;
- dwCurPorcessId = 0;
- // 通过窗口句柄反查进程pid;
- DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
- if (dwThreadId != 0) {
- // 判断当前进程id是否和目标进程id相同;
- if (dwCurPorcessId == dwTagetProcessId) {
- if (lpTagetWndName == NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- else {
- // 获取窗口名称;
- ::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
- // 获取窗口类名;
- ::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
- #ifdef _DEBUG
- TCHAR szLogMsg[MAX_PATH] = { 0 };
- _stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
- OutputDebugString(szLogMsg);
- #endif
- if (_tcsstr(szWndName, lpTagetWndName) != NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- }
- }
- }
- }
- // 当前窗口有可能不是进程父窗口;
- HWND hParentWnd = hTagetProcessWnd;
- while (hParentWnd) {
- hParentWnd = ::GetParent(hTagetProcessWnd);
- if (hParentWnd == NULL)
- break;
- hTagetProcessWnd = hParentWnd;
- }
- return hTagetProcessWnd;
- }
- HWND GetProcessMainWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWndName)
- {
- HWND hTagetWnd = NULL;
- DWORD dwProcessId = 0;
- if ((dwProcessId = GetProcessId(lpProcessName)) != 0) {
- hTagetWnd = GetProcessMainWnd(dwProcessId, lpTagetWndName);
- }
- return hTagetWnd;
- }
- HWND GetClassNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetClassName)
- {
- DWORD dwCurPorcessId = 0;
- HWND hTagetProcessWnd = NULL;
- TCHAR szWndName[MAX_PATH] = { 0 };
- TCHAR szClassName[MAX_PATH] = { 0 };
- // 取得第一个窗口句柄;
- for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
- // 重置为0;
- dwCurPorcessId = 0;
- // 通过窗口句柄反查进程pid;
- DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
- if (dwThreadId != 0) {
- // 判断当前进程id是否和目标进程id相同;
- if (dwCurPorcessId == dwTagetProcessId) {
- if (lpTagetClassName == NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- else {
- // 获取窗口名称;
- ::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
- // 获取窗口类名;
- ::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
- #ifdef _DEBUG
- TCHAR szLogMsg[MAX_PATH] = { 0 };
- _stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
- OutputDebugString(szLogMsg);
- #endif
- if (_tcsstr(szClassName, lpTagetClassName) != NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- }
- }
- }
- }
- return hTagetProcessWnd;
- }
- HWND GetWindowNameWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWindowName)
- {
- DWORD dwCurPorcessId = 0;
- HWND hTagetProcessWnd = NULL;
- TCHAR szWndName[MAX_PATH] = { 0 };
- TCHAR szClassName[MAX_PATH] = { 0 };
- // 取得第一个窗口句柄;
- for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
- // 重置为0;
- dwCurPorcessId = 0;
- // 通过窗口句柄反查进程pid;
- DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
- if (dwThreadId != 0) {
- // 判断当前进程id是否和目标进程id相同;
- if (dwCurPorcessId == dwTagetProcessId) {
- if (lpTagetWindowName == NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- else {
- // 获取窗口名称;
- ::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
- // 获取窗口类名;
- ::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
- #ifdef _DEBUG
- TCHAR szLogMsg[MAX_PATH] = { 0 };
- _stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
- OutputDebugString(szLogMsg);
- #endif
- if (_tcsstr(szWndName, lpTagetWindowName) != NULL) {
- hTagetProcessWnd = hCurWnd;
- break;
- }
- }
- }
- }
- }
- return hTagetProcessWnd;
- }
- HWND GetWindowNameWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWindowName)
- {
- HWND hTagWnd = NULL;
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hProcessSnap == NULL) return NULL;
- // 遍历进程;
- pe32.dwSize = sizeof(PROCESSENTRY32);
- for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
- if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
- hTagWnd = GetWindowNameWnd(pe32.th32ProcessID, lpTagetWindowName);
- if (hTagWnd != NULL)
- break;
- }
- }
- CloseHandle(hProcessSnap);
- return hTagWnd;
- }
- void WriteTextLog(const TCHAR* format, ...)
- {
- AutoThreadSection aSection(&g_csTextLog);
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s\\%04d-%02d-%02d.txt"), g_szCurModuleDir, gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday);
- // 打开或创建文件;
- FILE* fp = NULL;
- if (_taccess(szlogpath, 0) != -1)
- {// 存在;
- if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
- // 移动到末尾;
- fseek(fp, 0, SEEK_END);
- }
- else
- {// 不存在;
- _tfopen_s(&fp, szlogpath, _T("w+"));
- }
- if (fp == NULL)
- return;
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR* buffer = NULL;
- va_start(args, format);
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
- // 将日志内容输入到文件中;
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
- // 关闭文件,释放资源并设置回原语言区域;
- free(buffer);
- fclose(fp);
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- };
|