Global.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "StdAfx.h"
  2. #include "Global.h"
  3. //#include "CritSection.h"
  4. #include <locale.h>
  5. namespace Global
  6. {
  7. // 控制台输出;
  8. BOOL g_bStdOut = FALSE;
  9. TCHAR g_szModulePath[MAX_PATH] = _T(""); // 软件目录;
  10. TCHAR g_szModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  11. TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串;
  12. // 服务器信息;
  13. TCHAR g_szSvrAddress[MAX_PATH] = _T("");
  14. TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名);
  15. TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口;
  16. DWORD g_dwDBServerPort = 0; // 数据库源端口;
  17. TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户;
  18. TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码;
  19. TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称;
  20. TCHAR g_szStudioID[64] = _T(""); // 影楼ID;
  21. DWORD g_dwSvrPort = 0;
  22. DWORD g_dwDBPoolMaxCount = 1; // 数据库连接池最大对象数;
  23. DWORD g_dwDBPoolDef = 1; // 数据库连接池默认对象数;
  24. DWORD g_dwBalance = 100;
  25. /************************************************************************/
  26. /* 函数:[5/20/2016 IT];
  27. /* 描述:;
  28. /* 参数:;
  29. /* [IN] :;
  30. /* [OUT] :;
  31. /* [IN/OUT] :;
  32. /* 返回:void;
  33. /* 注意:;
  34. /* 示例:;
  35. /*
  36. /* 修改:;
  37. /* 日期:;
  38. /* 内容:;
  39. /************************************************************************/
  40. int GetIniInfo(const TCHAR *szPath, const TCHAR *szIniName)
  41. {
  42. TCHAR szDrive[_MAX_DRIVE] = {0};
  43. TCHAR szDir[_MAX_DIR] = {0};
  44. TCHAR szFName[_MAX_FNAME] = {0};
  45. TCHAR szExt[_MAX_EXT] = {0};
  46. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  47. sprintf(g_szModulePath, _T("%s"), g_szModulePath);
  48. _tsplitpath(g_szModulePath, szDrive, szDir, szFName, szExt);
  49. strcpy(g_szModulePath, szDrive);
  50. strcat(g_szModulePath, szDir);
  51. // -----------------------------------------------------//
  52. TCHAR szIniPath[MAX_PATH] = {0};
  53. if (szPath != NULL && szIniName != NULL)
  54. sprintf(szIniPath, _T("%s%s"), szPath, szIniName);
  55. else
  56. sprintf(szIniPath, _T("%sServiceInfo.ini"), g_szModulePath);
  57. HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  58. if (ERROR_FILE_NOT_FOUND == GetLastError())
  59. {
  60. return -1;
  61. }
  62. CloseHandle(hFile);
  63. // 获取服务器端信息;
  64. // 获取服务器端信息;
  65. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szIniPath);
  66. g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szIniPath);
  67. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szIniPath);
  68. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szIniPath);
  69. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szIniPath);
  70. g_dwDBPoolMaxCount = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolMaxObj"), 50, szIniPath);
  71. g_dwDBPoolDef = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbpoolDef"), 3, szIniPath);
  72. g_dwSvrPort = GetPrivateProfileInt(_T("NetWorkInfo"), _T("TCPChatPort"), 5678, szIniPath);
  73. if (!g_bStdOut)
  74. {
  75. g_bStdOut = GetPrivateProfileInt(_T("NetWorkInfo"), _T("StdOut"), 0, szIniPath);
  76. if (g_bStdOut)
  77. {
  78. AllocConsole(); // 开辟控制台;
  79. SetConsoleTitle(_T("调试输出")); // 设置控制台窗口标题;
  80. freopen("CONOUT$", "w+t", stdout); // 重定向输出;
  81. freopen("CONIN$", "r+t", stdin); // 重定向输入;
  82. HWND hWnd = NULL;
  83. again:
  84. hWnd = ::FindWindow(NULL, _T("调试输出"));
  85. if (hWnd)
  86. {
  87. if (!::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE))
  88. {
  89. _tprintf(_T("前置设置失败\n"));
  90. }
  91. else
  92. {
  93. _tprintf(_T("前置设置成功\n"));
  94. }
  95. }
  96. else
  97. {
  98. goto again;
  99. }
  100. }
  101. }
  102. return 0;
  103. }
  104. /************************************************************************/
  105. /* 函数:WriteTextLog[7/28/2016 IT];
  106. /* 描述:写文本日志;
  107. /* 参数:;
  108. /* [IN] :;
  109. /* 返回:void;
  110. /* 注意:;
  111. /* 示例:;
  112. /*
  113. /* 修改:;
  114. /* 日期:;
  115. /* 内容:;
  116. /************************************************************************/
  117. void WriteTextLog(const TCHAR *format, ...)
  118. {
  119. try
  120. {
  121. // 解析出日志路径;
  122. TCHAR szlogpath[MAX_PATH] = {0};
  123. static TCHAR szModulePath[MAX_PATH] = {0};
  124. static TCHAR szFna[_MAX_DIR] = { 0 };
  125. if ( szModulePath[0] == _T('\0') )
  126. {
  127. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  128. TCHAR szDir[_MAX_DIR] = { 0 };
  129. TCHAR szExt[_MAX_DIR] = { 0 };
  130. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  131. _splitpath(szModulePath, szDrive, szDir, szFna, szExt);
  132. strcpy(szModulePath, szDrive);
  133. strcat(szModulePath, szDir);
  134. }
  135. sprintf(szlogpath, _T("%s客户端日志.txt"), szModulePath);
  136. // 打开或创建文件;
  137. CStdioFile fp;
  138. if (PathFileExists(szlogpath))
  139. {
  140. if ( !fp.Open(szlogpath, CFile::modeWrite) )
  141. {
  142. return;
  143. }
  144. fp.SeekToEnd();
  145. }
  146. else
  147. {
  148. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  149. return;
  150. }
  151. // 格式化前设置语言区域;
  152. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  153. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  154. // 格式化日志内容;
  155. va_list args = NULL;
  156. static TCHAR buffer[8192] = {0};
  157. va_start( args, format );
  158. memset(buffer, 0, 8192);
  159. vsprintf( buffer, format, args ); // C4996
  160. // 将日志内容输入到文件中;
  161. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  162. fp.WriteString(buffer);
  163. fp.WriteString(_T("\n"));
  164. _tsetlocale(LC_CTYPE, old_locale);
  165. free(old_locale);//还原区域设定;
  166. fp.Close();
  167. }
  168. catch (CException *e)
  169. {
  170. e->ReportError();
  171. e->Delete();
  172. }
  173. }
  174. /************************************************************************/
  175. /* 函数:[10/27/2016 IT];
  176. /* 描述:;
  177. /* 参数:;
  178. /* [IN] :;
  179. /* [OUT] :;
  180. /* [IN/OUT] :;
  181. /* 返回:void;
  182. /* 注意:;
  183. /* 示例:;
  184. /*
  185. /* 修改:;
  186. /* 日期:;
  187. /* 内容:;
  188. /************************************************************************/
  189. TCHAR* GetGuidString()
  190. {
  191. GUID guid;
  192. static TCHAR szBuf[64] = { 0 };
  193. memset(szBuf, 0, 64 * sizeof(TCHAR));
  194. CoInitialize(NULL);
  195. if (S_OK == ::CoCreateGuid(&guid))
  196. {
  197. _stprintf(szBuf, _T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
  198. guid.Data1,
  199. guid.Data2,
  200. guid.Data3,
  201. guid.Data4[0],
  202. guid.Data4[1],
  203. guid.Data4[2],
  204. guid.Data4[3],
  205. guid.Data4[4],
  206. guid.Data4[5],
  207. guid.Data4[6],
  208. guid.Data4[7]
  209. );
  210. }
  211. CoUninitialize();
  212. return szBuf;
  213. }
  214. //增加自动启动注册表项
  215. INT AddAutoSysRun(LPBYTE lpExeFile,CONST DWORD &cbExeFile, LPCTSTR lpKeyName, INT iAutoRunSystem)
  216. {
  217. HKEY hKey = NULL;
  218. DWORD dwErrorCode = 0;
  219. //创建注册表,有该键则读取,无则创建
  220. dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  221. _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
  222. 0,
  223. NULL,
  224. REG_OPTION_NON_VOLATILE,
  225. KEY_ALL_ACCESS, //
  226. //KEY_READ,
  227. NULL, //Security
  228. &hKey,
  229. NULL);
  230. if (dwErrorCode == ERROR_SUCCESS)
  231. {
  232. if (iAutoRunSystem)
  233. {
  234. dwErrorCode = RegSetValueEx(hKey, lpKeyName, NULL, REG_SZ, lpExeFile, cbExeFile);
  235. }
  236. else
  237. {
  238. dwErrorCode = RegDeleteValue(hKey, lpKeyName);
  239. dwErrorCode = RegDeleteKey(hKey, lpKeyName);
  240. }
  241. }
  242. if (NULL != hKey)
  243. RegCloseKey(hKey);
  244. hKey = NULL;
  245. return 0;
  246. }
  247. };