1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243 |
- #include "StdAfx.h"
- #include "Global.h"
- #include <WinVer.h>
- #pragma comment(lib,"version.lib")
- using namespace std;
- #include <psapi.h>
- #pragma comment(lib,"Psapi.lib")
- #include <locale.h>
- #include <io.h>//_access头文件;
- namespace GLOBAL
- {
- CDataImpl g_db;
- CIOCPPipe g_IOCP;
- CONFIG g_config;
- TCHAR g_szModulePath[MAX_PATH] = _T("");
- TCHAR g_szModuleFileName[MAX_PATH] = _T("");
- TCHAR g_szConfigFile[MAX_PATH] = _T("");
- PROC_WND_INFO g_procWndInfo;
-
- BOOL g_bInjected = FALSE;
- #define TRACE4(sz, p1, p2, p3, p4) TRACE(_T(sz), p1, p2, p3, p4)
- BOOL GetConfigInfo(LPCTSTR lpIniDir , LPCTSTR lpConfigName )
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
- _stprintf_s(g_szModuleFileName, _T("%s"), g_szModulePath);
- _tsplitpath_s(g_szModulePath, szDrive, szDir, szFna, szExt);
- _tcscpy_s(g_szModulePath, szDrive);
- _tcscat_s(g_szModulePath, szDir);
- if (lpIniDir != NULL && lpConfigName != NULL)
- _stprintf_s(g_szConfigFile, _T("%s%s"), lpIniDir, lpConfigName);
- else
- _stprintf_s(g_szConfigFile, _T("%s%s.ini"), g_szModulePath, szFna);
- HANDLE hFile = CreateFile(g_szConfigFile, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (ERROR_FILE_NOT_FOUND == GetLastError())
- {
- return FALSE;
- }
- CloseHandle(hFile);
- hFile = NULL;
- #pragma region 获取配置文件信息;
- TCHAR szValue[MAX_PATH] = {0};
-
- GetPrivateProfileString(_T("LOGIN"), _T("account"), _T(""), g_config.szAccount, MAX_PATH, g_szConfigFile);
- GetPrivateProfileString(_T("LOGIN"), _T("password"), _T(""), g_config.szPassword, MAX_PATH, g_szConfigFile);
- GetPrivateProfileString(_T("LOGIN"), _T("line"), _T(""), g_config.szLine, MAX_PATH, g_szConfigFile);
- g_config.nOffline = GetPrivateProfileInt(_T("LOGIN"), _T("offline"), 0, g_szConfigFile);
-
- g_config.nStdOut = GetPrivateProfileInt(_T("SYSTEM"), _T("stdprint"), 0, g_szConfigFile);
- GetPrivateProfileString(_T("SYSTEM"), _T("OGCToolPath"), _T(""), g_config.szOGCToolProgram, MAX_PATH, g_szConfigFile);
- TCHAR *pBuff = _tcsstr(g_config.szOGCToolProgram, _T("\\Demo.exe"));
- if ( pBuff )
- {
-
- memcpy(g_config.szOGCToolPath, g_config.szOGCToolProgram, sizeof(TCHAR)*(pBuff - g_config.szOGCToolProgram));
- }
-
- g_config.nDeltaEType = GetPrivateProfileInt(_T("BATCH"), _T("EType"), -1, g_szConfigFile);
- GetPrivateProfileString(_T("BATCH"), _T("EValue"), _T(""), g_config.szDeltaEValue, MAX_PATH, g_szConfigFile);
- g_config.dDeltaEValue = _tstof(g_config.szDeltaEValue);
- GetPrivateProfileString(_T("BATCH"), _T("batchNumber"), _T(""), g_config.szBatchNumber, MAX_PATH, g_szConfigFile);
- #pragma endregion
- if ( g_config.nStdOut ) {
- AllocConsole();
- SetConsoleTitle(_T("调试输出"));
- freopen("CONOUT$", "w+t", stdout);
- freopen("CONIN$", "r+t", stdin);
- HWND hWnd = NULL;
- again:
- hWnd = ::FindWindow(NULL, _T("调试输出"));
- if( hWnd ) {
- if (!::SetWindowPos(hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE)) {
- _tprintf_s(_T("前置设置失败\n"));
- } else {
- _tprintf_s(_T("前置设置成功\n"));
- }
- } else {
- goto again;
- }
- }
- return TRUE;
- }
- std::string GetFileData(LPCTSTR lpFile)
- {
- if ( !lpFile || lpFile[0] == '\0' || !PathFileExists(lpFile) )
- return std::string();
- CFile file;
- CFileException exp;
- if ( file.Open(lpFile, CFile::modeRead, &exp) )
- {
- DWORD dwRead = 0;
- DWORD dwCount = 0;
- std::string strData;
- char szRead[MAX_PATH] = {0};
- DWORD dwLen = file.GetLength();
- while ( dwCount < dwLen )
- {
- dwRead = file.Read(szRead, MAX_PATH);
- strData.append(szRead, dwRead);
- dwCount += dwRead;
- }
- file.Close();
- return strData;
- }
- return std::string();
- }
- std::string GetFileLine(std::string &strFileData, int nLine)
- {
- int nPos=0;
- int nIndex = 0;
- std::string strTemp = strFileData;
- while ( (nPos=strTemp.find('\r\n')) != std::string::npos )
- {
- if ( nIndex++ == nLine )
- {
- strTemp = strTemp.substr(0, nPos);
- break;
- }
- strTemp = strTemp.substr(nPos+2);
- }
- return nIndex < nLine ? std::string() : strTemp;
- }
- void GetStringList(std::string strData, std::string strSplit, std::vector<std::string> &vtString)
- {
- int nPos = -1;
- std::string strTemp;
- vtString.clear();
- do
- {
- nPos = strData.find(strSplit);
- if ( nPos != std::string::npos )
- {
- strTemp = strData.substr(0, nPos);
- vtString.push_back(strTemp);
- strData = strData.substr(nPos+strSplit.size());
- }
- } while (nPos != -1);
- }
- BOOL FindString(std::vector<std::string> &vtString, std::string strFind, std::string &strResult)
- {
- std::string strTemp;
- for (std::vector<std::string>::iterator it = vtString.begin(); it != vtString.end(); it++ )
- {
- if ( strTemp.find(strFind) != -1 )
- {
- strResult = strTemp;
- return TRUE;
- }
- }
- return FALSE;
- }
- INT CheckOutData(std::vector<std::string> &vtData, std::vector<RGB_PAT> &vtResult)
- {
- if ( vtData.size() < 3 )
- return 0;
- int nIndex = 0;
- for (std::vector<std::string>::iterator it = vtData.begin(); it != vtData.end(); it++ )
- {
- std::vector<std::string> vtItem;
- GetStringList(it->c_str(), ",", vtItem);
- if ( vtItem.size() >= 20 && nIndex > 1)
- {
-
-
- RGB_PAT rgb_pat;
- rgb_pat.R = vtItem.at(0);
- rgb_pat.G = vtItem.at(1);
- rgb_pat.B = vtItem.at(2);
- rgb_pat.deltaE76 = vtItem.at(12);
- rgb_pat.deltaE94 = vtItem.at(13);
- rgb_pat.deltaE00 = vtItem.at(14);
- vtResult.push_back(rgb_pat);
- }
- nIndex++;
- }
- return 0;
- }
- BOOL IsDebugPass(std::vector<RGB_PAT> &vtDebugRGB, INT nDeltaEType, DOUBLE dDeltaEValue)
- {
- BOOL bRet = FALSE;
- DOUBLE dAllDeltaEValue = 0.0;
- INT nDeltaETypeIndex = 3 + nDeltaEType;
- for (std::vector<RGB_PAT>::iterator it = vtDebugRGB.begin(); it != vtDebugRGB.end(); it++)
- {
- if ( nDeltaEType == DeltaE76 )
- dAllDeltaEValue += atof(it->deltaE76.c_str());
- if ( nDeltaEType == DeltaE94 )
- dAllDeltaEValue += atof(it->deltaE94.c_str());
- if ( nDeltaEType == DeltaE00 )
- dAllDeltaEValue += atof(it->deltaE00.c_str());
- }
- if ( dDeltaEValue <= dAllDeltaEValue/vtDebugRGB.size() )
- bRet = TRUE;
- return bRet;
- }
- DWORD FindProcess(LPCTSTR lpProName)
- {
- ASSERT(lpProName!=NULL);
- DWORD dwProcId = 0;
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnapshot == NULL)
- {
- return 0;
- }
- pe32.dwSize = sizeof(PROCESSENTRY32);
- if (Process32First(hSnapshot, &pe32))
- {
- do {
- if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
- {
- dwProcId = pe32.th32ProcessID;
- break;
- }
- } while (Process32Next(hSnapshot, &pe32));
- }
- CloseHandle(hSnapshot);
- return dwProcId;
- }
- vector<DWORD> FindAllProcess(LPCTSTR lpProName)
- {
- ASSERT(lpProName!=NULL);
- vector<DWORD> vtPID;
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnapshot == NULL)
- return vector<DWORD>();
- pe32.dwSize = sizeof(PROCESSENTRY32);
- if (Process32First(hSnapshot, &pe32))
- {
- do {
- if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
- {
- vtPID.push_back(pe32.th32ProcessID);
- }
- } while (Process32Next(hSnapshot, &pe32));
- }
- CloseHandle(hSnapshot);
- return vtPID;
- }
- void FindAllProcess(std::vector<PROC_INFO> &vtProInfo)
- {
- PROCESSENTRY32 pe32 = { 0 };
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (hSnapshot == NULL)
- return;
- pe32.dwSize = sizeof(PROCESSENTRY32);
- if (!Process32First(hSnapshot, &pe32))
- goto end;
- do
- {
- PROC_INFO proinfo;
- proinfo.dwProcId = pe32.th32ProcessID;
- proinfo.strProcName = pe32.szExeFile;
- vtProInfo.push_back(proinfo);
- } while (Process32Next(hSnapshot, &pe32));
- end:
- CloseHandle(hSnapshot);
- }
- HANDLE FindModule(LPCTSTR lpModuleName, DWORD dwProcId)
- {
- ASSERT(lpModuleName!=NULL);
- DWORD dwMID = 0;
- MODULEENTRY32 me32 = { 0 };
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, dwProcId);
- if (hSnapshot == NULL)
- return NULL;
- me32.dwSize = sizeof(MODULEENTRY32);
- if (Module32First(hSnapshot, &me32))
- {
- do {
- if (_tcsicmp(lpModuleName, me32.szModule) == 0)
- {
- break;
- }
- } while (Module32Next(hSnapshot, &me32));
- }
- CloseHandle(hSnapshot);
- return me32.hModule;
- }
- LPCTSTR GetModulePath(LPCTSTR lpModuleName, DWORD dwProcId)
- {
- ASSERT(lpModuleName!=NULL);
- DWORD dwMID = 0;
- MODULEENTRY32 me32 = { 0 };
- HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, dwProcId);
- if (hSnapshot == NULL)
- return NULL;
- BOOL bFound = FALSE;
- me32.dwSize = sizeof(MODULEENTRY32);
- if (Module32First(hSnapshot, &me32)) {
- do
- {
- if (_tcsicmp(lpModuleName, me32.szModule) == 0) {
- bFound = TRUE;
- break;
- }
- } while (Module32Next(hSnapshot, &me32));
- }
- CloseHandle(hSnapshot);
- return bFound ? me32.szExePath : NULL;
- }
- HANDLE FindModuleEx(LPCTSTR lpModuleName, DWORD dwProcId)
- {
- HMODULE hMods[1024] = {0};
- DWORD cbNeeded = 0;
- TCHAR szModName[MAX_PATH];
- BOOL Wow64Process;
- HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwProcId);
- IsWow64Process(hProcess, &Wow64Process);
- if ( EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, Wow64Process?LIST_MODULES_32BIT:LIST_MODULES_64BIT) )
- {
- for (UINT i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
- {
- GetModuleFileNameEx(hProcess, hMods[i], szModName, _countof(szModName));
- #ifdef _DEBUG
- WriteTextLog(_T("目标=%s, 原始=%s, 地址=%p"), szModName, lpModuleName, hMods[i]);
- #endif
- if (_tcsicmp(lpModuleName, szModName) == 0)
- {
- CloseHandle(hProcess);
- WriteTextLog(_T("【目标=%s, 原始=%s, 地址=%p】"), szModName, lpModuleName, hMods[i]);
- return hMods[i];
- }
- }
- }
- CloseHandle(hProcess);
- return NULL;
- }
-
- BOOL GetDebugPriv()
- {
-
- HANDLE hToken;
-
- LUID sedebugnameValue;
-
- TOKEN_PRIVILEGES tkp;
-
-
- HANDLE hCurProc = ::GetCurrentProcess();
-
- if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- {
- return FALSE;
- }
- if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
- {
- CloseHandle(hToken);
- return FALSE;
- }
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Luid = sedebugnameValue;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
- {
- CloseHandle(hToken);
- return FALSE;
- }
- CloseHandle(hCurProc);
- CloseHandle(hToken);
- return TRUE;
- }
-
-
-
- BOOL GetFileVersion( IN HMODULE hModule, OUT DWORD (&dwArray)[4])
- {
- TCHAR fname[MAX_PATH];
- VS_FIXEDFILEINFO *pVi;
- DWORD dwHandle;
- if ( GetModuleFileName(hModule, fname, MAX_PATH))
- {
- INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
- if (nSize > 0)
- {
- BYTE *pBuffer = new BYTE[nSize];
- memset(pBuffer, 0, nSize);
- if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
- {
- if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
- {
- dwArray[0] = HIWORD(pVi->dwFileVersionMS);
- dwArray[1] = LOWORD(pVi->dwFileVersionMS);
- dwArray[2] = HIWORD(pVi->dwFileVersionLS);
- dwArray[3] = LOWORD(pVi->dwFileVersionLS);
- delete[]pBuffer;
- return TRUE;
- }
- }
- if ( pBuffer )
- delete[]pBuffer;
- }
- }
- return FALSE;
- }
-
-
-
- BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
- {
- if ( lpFileName == NULL || !PathFileExists(lpFileName) )
- {
- OutputDebugString(_T("文件名错误或文件不存在\n"));
- return FALSE;
- }
- DWORD dwHandle = 0;
- VS_FIXEDFILEINFO *pVi = NULL;
- INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
- if ( nSize > 0 )
- {
- BYTE *pBuffer = new BYTE[nSize];
- memset(pBuffer, 0, nSize);
- if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
- {
- if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
- {
- dwArray[0] = HIWORD(pVi->dwFileVersionMS);
- dwArray[1] = LOWORD(pVi->dwFileVersionMS);
- dwArray[2] = HIWORD(pVi->dwFileVersionLS);
- dwArray[3] = LOWORD(pVi->dwFileVersionLS);
- if (pBuffer)
- delete[]pBuffer;
- return TRUE;
- }
- }
- if (pBuffer)
- delete[]pBuffer;
- }
- return FALSE;
- }
-
-
-
- BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
- {
- TCHAR fname[MAX_PATH];
- VS_FIXEDFILEINFO *pVi;
- DWORD dwHandle;
- if (::GetModuleFileName(hModule, fname, MAX_PATH))
- {
- INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
- if (nSize > 0)
- {
- BYTE *pBuffer = new BYTE[nSize];
- memset(pBuffer, 0, nSize);
- if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
- {
- if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
- {
- dwArray[0] = HIWORD(pVi->dwProductVersionMS);
- dwArray[1] = LOWORD(pVi->dwProductVersionMS);
- dwArray[2] = HIWORD(pVi->dwProductVersionLS);
- dwArray[3] = LOWORD(pVi->dwProductVersionLS);
- if(pBuffer)
- delete[]pBuffer;
- return TRUE;
- }
- }
- if(pBuffer)
- delete[]pBuffer;
- }
- }
- return FALSE;
- }
-
-
-
- BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
- {
- if ( lpFileName == NULL || !PathFileExists(lpFileName) )
- {
- OutputDebugString(_T("文件名错误或文件不存在\n"));
- return FALSE;
- }
- DWORD dwHandle = 0;
- VS_FIXEDFILEINFO *pVi = NULL;
- INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
- if ( nSize > 0 )
- {
- BYTE *pBuffer = new BYTE[nSize];
- memset(pBuffer, 0, nSize);
- if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
- {
- if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
- {
- dwArray[0] = HIWORD(pVi->dwProductVersionMS);
- dwArray[1] = LOWORD(pVi->dwProductVersionMS);
- dwArray[2] = HIWORD(pVi->dwProductVersionLS);
- dwArray[3] = LOWORD(pVi->dwProductVersionLS);
- if (pBuffer)
- delete[]pBuffer;
- return TRUE;
- }
- }
- if (pBuffer)
- delete[]pBuffer;
- }
- return FALSE;
- }
-
-
- void WriteTextLog(const TCHAR *format, ...)
- {
- #if 0
- try
- {
-
-
-
- TCHAR szlogpath[MAX_PATH] = {0};
- static TCHAR szModulePath[MAX_PATH] = {0};
- static TCHAR szFna[_MAX_DIR] = { 0 };
- if ( szModulePath[0] == _T('\0') )
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
- _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
- _tcscpy_s(szModulePath, szDrive);
- _tcscat_s(szModulePath, szDir);
- }
- _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]"));
-
- CStdioFile fp;
- if (PathFileExists(szlogpath))
- {
- if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
- {
- return;
- }
- fp.SeekToEnd();
- }
- else
- {
- fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite);
- }
-
- 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 );
-
- len = _vsctprintf( format, args ) + 1;
- buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
- _vstprintf_s( buffer, len, format, args );
-
-
- fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
- fp.WriteString(buffer);
- fp.WriteString(_T("\n"));
-
- free( buffer );
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);
- fp.Close();
- }
- catch (CException *e)
- {
- e->ReportError();
- e->Delete();
- }
- #else
-
- TCHAR szlogpath[MAX_PATH] = { 0 };
- static TCHAR szModulePath[MAX_PATH] = { 0 };
- static TCHAR szFna[MAX_PATH] = { 0 };
- if (szModulePath[0] == _T('\0'))
- {
- TCHAR szDrive[MAX_PATH] = { 0 };
- TCHAR szDir[MAX_PATH] = { 0 };
- TCHAR szExt[MAX_PATH] = { 0 };
- ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
- _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
- _tcscpy_s(szModulePath, szDrive);
- _tcscat_s(szModulePath, szDir);
- }
- _stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
-
- FILE *fp = NULL;
-
- #ifndef UNICODE
- if (_access(szlogpath, 0) != -1)
- #else
- if (_taccess(szlogpath, 0) != -1)
- #endif
- {
- fp = _tfopen(szlogpath, _T("a+"));
-
- fseek(fp, 0, SEEK_END);
- }
- else
- {
- fp = _tfopen(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);
-
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
-
-
- __time64_t gmt = time(NULL);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt);
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, 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);
- #endif
- }
-
-
-
-
-
-
-
-
- BOOL IsDirectoryLegitimate(const CString &strDirectory)
- {
- if (strDirectory.Find('/') != -1 ||
- strDirectory.Find('\\') != -1 ||
- strDirectory.Find(':') != -1 ||
- strDirectory.Find('*') != -1 ||
- strDirectory.Find('?') != -1 ||
- strDirectory.Find('\"') != -1 ||
- strDirectory.Find('>') != -1 ||
- strDirectory.Find('<') != -1 ||
- strDirectory.Find('|') != -1
- )
- return FALSE;
- return TRUE;
- }
-
-
-
-
-
-
-
-
-
- void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError)
- {
- #if 1
- LPVOID lpMsgBuf;
- BOOL fOk = FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dwError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&lpMsgBuf,
- 0, NULL);
- if (!fOk)
- {
-
- HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
- if (hDll != NULL)
- {
- FormatMessage(
- FORMAT_MESSAGE_FROM_HMODULE |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- hDll,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL);
- FreeLibrary(hDll);
- }
- }
- if (lpMsgBuf != NULL)
- {
- CString strDisplay;
- strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
-
- LocalFree(lpMsgBuf);
- }
- else
- {
-
- }
- #else
- HLOCAL hlocal = NULL;
-
- BOOL fOk = FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (PTSTR)&hlocal,
- 0,
- NULL);
- if (!fOk)
- {
-
- HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
- if (hDll != NULL)
- {
- FormatMessage(
- FORMAT_MESSAGE_FROM_HMODULE |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- hDll,
- dwError,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (PTSTR)&hlocal,
- 0,
- NULL);
- FreeLibrary(hDll);
- }
- }
- if (hlocal != NULL)
- {
- CString strDisplay;
- strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
-
- LocalFree(hlocal);
- }
- else
- {
-
- }
- #endif
- }
-
-
-
- BOOL MySystemShutdown(LPTSTR lpMsg)
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
- BOOL fResult;
-
-
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- return FALSE;
-
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
- &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES)NULL, 0);
-
- if (GetLastError() != ERROR_SUCCESS)
- return FALSE;
-
- fResult = InitiateSystemShutdown(
- NULL,
- lpMsg,
- 30,
- FALSE,
- TRUE);
- if (!fResult)
- return FALSE;
-
- tkp.Privileges[0].Attributes = 0;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES)NULL, 0);
- return TRUE;
- }
-
-
-
- BOOL PreventSystemShutdown()
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
-
-
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- return FALSE;
-
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
- &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES)NULL, 0);
- if (GetLastError() != ERROR_SUCCESS)
- return FALSE;
-
- if (!AbortSystemShutdown(NULL))
- return FALSE;
-
- tkp.Privileges[0].Attributes = 0;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES)NULL, 0);
- return TRUE;
- }
-
-
-
- BOOL MySystemShutdown()
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
-
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
- return(FALSE);
-
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
- &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
- (PTOKEN_PRIVILEGES)NULL, 0);
- if (GetLastError() != ERROR_SUCCESS)
- return FALSE;
-
- if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
- SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
- SHTDN_REASON_MINOR_UPGRADE |
- SHTDN_REASON_FLAG_PLANNED))
- return FALSE;
- return TRUE;
- }
- 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)) {
-
- dwCurPorcessId = 0;
-
- DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
- if (dwThreadId != 0) {
-
- 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;
- }
- BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)
- {
- DWORD dwPid = 0;
- LPPROC_WND_INFO lpWndData = (LPPROC_WND_INFO)lParam;
- GetWindowThreadProcessId(hWnd, &dwPid);
- if(dwPid == lpWndData->dwProcId)
- {
- WND_INFO wndInfo;
- wndInfo.hWnd=hWnd;
- SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)wndInfo.szWndTitle);
- GetClassName(hWnd, wndInfo.szClassName, MAX_PATH);
- wndInfo.dwCtrlId = ::GetDlgCtrlID(hWnd);
- lpWndData->AddWnd(wndInfo);
-
- TRACE4("A-0x%08X, %ld, %s, %s\n", hWnd, wndInfo.dwCtrlId, wndInfo.szClassName, wndInfo.szWndTitle);
-
-
- return TRUE;
- }
- return FALSE;
- }
- BOOL CALLBACK EnumWindowCallBack(HWND hWnd, LPARAM lParam)
- {
- DWORD dwPid = 0;
- LPPROC_WND_INFO lpWndData = (LPPROC_WND_INFO)lParam;
- GetWindowThreadProcessId(hWnd, &dwPid);
- if(dwPid == lpWndData->dwProcId)
- {
- WND_INFO wndInfo;
- wndInfo.hWnd=hWnd;
- SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)wndInfo.szWndTitle);
- GetClassName(hWnd, wndInfo.szClassName, MAX_PATH);
- wndInfo.dwCtrlId = ::GetDlgCtrlID(hWnd);
- lpWndData->AddWnd(wndInfo);
-
- TRACE4("A-0x%08X, %ld, %s, %s\n", hWnd, wndInfo.dwCtrlId, wndInfo.szClassName, wndInfo.szWndTitle);
- EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam);
- }
- return TRUE;
- }
- BOOL EnumProcessAllWnd(LPPROC_WND_INFO lpProcWndInfo)
- {
- lpProcWndInfo->vtWndInfo.clear();
- return EnumWindows(EnumWindowCallBack, (LPARAM)lpProcWndInfo);
- }
- BOOL StartProcess(LPCTSTR lpPath, BOOL bShowWnd, BOOL bSuspend)
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi, sizeof(pi));
- CString strDir = lpPath;
- strDir = strDir.Left(strDir.ReverseFind(_T('\\')));
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = bShowWnd ? SW_SHOW : SW_HIDE;
-
- BOOL bRet = ::CreateProcess (
- lpPath,
- NULL,
- NULL,
- NULL,
- FALSE,
- bSuspend ? CREATE_SUSPENDED : NULL,
- NULL,
- strDir,
- &si,
- &pi);
- if(bRet)
- {
-
-
-
-
-
-
-
-
-
-
- g_procWndInfo.dwProcId = pi.dwProcessId;
-
- ::CloseHandle (pi.hThread);
- ::CloseHandle (pi.hProcess);
- }
- return bRet;
- }
-
-
- int IsDigitString(IN CONST CString& str)
- {
- int npt = 0;
- for (int i = 0; i < str.GetLength(); i++)
- {
- if (!_istdigit(str.GetAt(i)))
- {
- if ( str.GetAt(i) == _T('.') && i != 0)
- {
- if ( npt > 1)
- {
- return -1;
- }
- npt++;
- }
- else
- {
- return -1;
- }
- }
- }
- return npt ? 2 : 1;
- }
- };
|