Utility.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "StdAfx.h"
  2. #include "Utility.h"
  3. #include <time.h>
  4. #define MAX_SIZE 8192
  5. namespace Utility
  6. {
  7. // 当前DLL模块句柄;
  8. HMODULE g_hDLLModule = NULL;
  9. // 当前DLL内部工作者线程句柄;
  10. HANDLE hWorkThreadProc = NULL;
  11. // 当前DLL所在路径;
  12. TCHAR g_szCurModulePath[MAX_PATH] = {0};
  13. // 管道实例;
  14. CPipeClient *g_pPipeClient = NULL;
  15. // 配置文件名称;
  16. TCHAR g_szConfigFile[MAX_PATH] = {0};
  17. // 配置文件内容;
  18. CFG_CTX g_cfgCtx;
  19. // DLL所在进程窗口信息;
  20. WndInfo g_WndInfo;
  21. //////////////////////////////////////////////////////////////////////////
  22. // 全局函数;
  23. // 获取配置文件内容;
  24. void GetConfigContent()
  25. {
  26. // 获取dll的目录;
  27. TCHAR szDrive[MAX_PATH] = { 0 };
  28. TCHAR szDir[MAX_PATH] = { 0 };
  29. TCHAR szExt[MAX_PATH] = { 0 };
  30. TCHAR szFna[MAX_PATH] = { 0 };
  31. ::GetModuleFileName(g_hDLLModule, g_szCurModulePath, sizeof(g_szCurModulePath) / sizeof(TCHAR));
  32. _tsplitpath_s(g_szCurModulePath, szDrive, szDir, szFna, szExt);
  33. _tcscpy_s(g_szCurModulePath, szDrive);
  34. _tcscat_s(g_szCurModulePath, szDir);
  35. // 设置配置文件;
  36. _stprintf_s(g_szConfigFile, _T("%s%s"), g_szCurModulePath, _T("Assist.ini"));
  37. // 读取配置文件;
  38. TCHAR szValue[MAX_PATH] = {0};
  39. GetPrivateProfileString(_T("Windows"), _T("Title"), _T(""), g_cfgCtx.szWindowTitel, MAX_PATH, g_szConfigFile);
  40. GetPrivateProfileString(_T("Pipe"), _T("Name"), _T("Assist"), szValue, MAX_PATH, g_szConfigFile);
  41. _stprintf_s(g_cfgCtx.szPipeName, _T("\\\\.\\pipe\\%s"), szValue);
  42. GetPrivateProfileString(_T("Process"), _T("Name"), _T("Game.exe"), g_cfgCtx.szWndProcessName, MAX_PATH, g_szConfigFile);
  43. }
  44. void FreeLibraryAndExit()
  45. {
  46. if ( g_pPipeClient )
  47. delete g_pPipeClient;
  48. g_pPipeClient = NULL;
  49. if ( g_hDLLModule )
  50. FreeLibraryAndExitThread(g_hDLLModule, 0);
  51. }
  52. void dprintf(TCHAR* pszStr, ...)
  53. {
  54. TCHAR szData[MAX_SIZE] = {0};
  55. // 获取今年年份;
  56. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  57. struct tm gmtm = { 0 };
  58. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  59. #if _MSC_VER >= 1200 && _MSC_VER < 1500
  60. sprintf(szData, _T("%s %s "), _T("[Assist] "), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
  61. #else
  62. _stprintf_s(szData, _T("%s %04d-%02d-%02d %02d:%02d:%02d "), _T("[Assist] "), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec);
  63. #endif
  64. int len = _tcslen(szData)*sizeof(TCHAR);
  65. va_list args;
  66. va_start(args, pszStr);
  67. _vsntprintf_s(szData + len, MAX_SIZE - len, MAX_SIZE - len, pszStr, args);
  68. va_end(args);
  69. _tcscat_s(szData, "\n");
  70. OutputDebugString(szData);
  71. }
  72. };