Global.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. std::string g_strDefaultTip = "指纹机采集程序";
  4. std::vector<STGeneralLogData> g_vtGeneralLogData;
  5. std::vector<STSuperLogData> g_vtSuperLogData;
  6. HWND g_hwnd = NULL;
  7. TCHAR g_ModulePath[_MAX_PATH] = {0};
  8. TCHAR g_ModuleFileName[_MAX_PATH] = {0};
  9. TCHAR g_szDBServer[MAX_PATH+1] = {0};
  10. DWORD g_dwDBServerPort = 0;
  11. TCHAR g_szDBAccount[MAX_PATH+1] = {0};
  12. TCHAR g_szDBPassWord[MAX_PATH+1] = {0};
  13. TCHAR g_szDBName[MAX_PATH+1] = {0};
  14. long g_lLicense = 4335;
  15. //////////////////////////////////////////////////////////////////////////
  16. typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
  17. LPFN_ISWOW64PROCESS fnIsWow64Process;
  18. BOOL IsWow64()
  19. {
  20. BOOL bIsWow64 = FALSE;
  21. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
  22. GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
  23. if (NULL != fnIsWow64Process)
  24. {
  25. if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
  26. {
  27. // handle error
  28. }
  29. }
  30. return bIsWow64;
  31. }
  32. //////////////////////////////////////////////////////////////////////////
  33. //wchar* to char*
  34. int WChar2Char(char* pDest, const wchar_t* pSource)
  35. {
  36. if(pSource == NULL || pDest == NULL)
  37. return -1;
  38. int nLen = ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), NULL, 0, NULL, NULL);
  39. // Unicode版对应的strlen是wcslen
  40. ::WideCharToMultiByte(CP_ACP, NULL, pSource, wcslen(pSource), pDest, nLen, NULL, NULL);
  41. // 最后加上'\0'
  42. pDest[nLen] = '\0';
  43. return nLen;
  44. }
  45. //char* to wchar*
  46. int Char2WChar(wchar_t* pDest, const char* pSource)
  47. {
  48. if(pSource == NULL || pDest == NULL)
  49. return -1;
  50. int nLen = ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, NULL, 0);
  51. ::MultiByteToWideChar(CP_ACP, 0, pSource, -1, pDest, nLen);
  52. return nLen;
  53. }
  54. /************************************************************************/
  55. /* 函数:[2/6/2017 IT];
  56. /* 描述:注册ocx控件;
  57. /* 参数:;
  58. /* [IN] lpszOCXfile:ocx控件路径;
  59. /* [OUT] :;
  60. /* [IN/OUT] :;
  61. /* 返回:void;
  62. /* 注意:;
  63. /* 示例:;
  64. /*
  65. /* 修改:;
  66. /* 日期:;
  67. /* 内容:;
  68. /************************************************************************/
  69. BOOL RegisterOcx(IN LPCTSTR lpszOCXfile)
  70. {
  71. // 参数有效性判断 ;
  72. if ( lpszOCXfile == NULL )
  73. {
  74. #ifdef _DEBUG
  75. OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
  76. #endif
  77. return FALSE;
  78. }
  79. if ( !PathFileExists(lpszOCXfile) )
  80. {
  81. #ifdef _DEBUG
  82. OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
  83. #endif
  84. return FALSE;
  85. }
  86. HMODULE hModule = LoadLibrary(lpszOCXfile);
  87. if ( hModule == NULL)
  88. {
  89. #ifdef _DEBUG
  90. DWORD dwError = GetLastError();
  91. CString strError = _T("");
  92. strError.Format(_T("加载ocx时出错:%d\n"), dwError);
  93. OutputDebugString(strError);
  94. #endif
  95. return FALSE;
  96. }
  97. // ocx入口点方法指针;
  98. FARPROC lpDllEntryPoint = NULL;
  99. //获取注册函数DllRegisterServer地址;
  100. lpDllEntryPoint = GetProcAddress(hModule, "DllRegisterServer");
  101. if ( lpDllEntryPoint == NULL )
  102. {
  103. #ifdef _DEBUG
  104. OutputDebugString(_T("参数DllRegisterServer 指针无效\n"));
  105. #endif
  106. FreeLibrary(hModule);
  107. return FALSE;
  108. }
  109. // 开始调用注册函数注册ocx控件;
  110. if ( FAILED((*lpDllEntryPoint)()) )
  111. {
  112. #ifdef _DEBUG
  113. OutputDebugString(_T("参数DllRegisterServer 注册失败\n"));
  114. #endif
  115. FreeLibrary(hModule);
  116. return FALSE;
  117. }
  118. FreeLibrary(hModule);
  119. return TRUE;
  120. }
  121. /************************************************************************/
  122. /* 函数:[2/6/2017 IT];
  123. /* 描述:卸载ocx控件;
  124. /* 参数:;
  125. /* [IN] lpszOCXfile:ocx控件路径;
  126. /* [OUT] :;
  127. /* [IN/OUT] :;
  128. /* 返回:void;
  129. /* 注意:;
  130. /* 示例:;
  131. /*
  132. /* 修改:;
  133. /* 日期:;
  134. /* 内容:;
  135. /************************************************************************/
  136. BOOL UnRegisterOcx(IN LPCTSTR lpszOCXfile)
  137. {
  138. // 参数有效性判断 ;
  139. if ( lpszOCXfile == NULL )
  140. {
  141. #ifdef _DEBUG
  142. OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
  143. #endif
  144. return FALSE;
  145. }
  146. if ( !PathFileExists(lpszOCXfile) )
  147. {
  148. #ifdef _DEBUG
  149. OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
  150. #endif
  151. return FALSE;
  152. }
  153. HMODULE hModule = LoadLibrary(lpszOCXfile);
  154. if ( hModule == NULL)
  155. {
  156. #ifdef _DEBUG
  157. DWORD dwError = GetLastError();
  158. CString strError = _T("");
  159. strError.Format(_T("加载ocx时出错:%d\n"), dwError);
  160. OutputDebugString(strError);
  161. #endif
  162. return FALSE;
  163. }
  164. // ocx入口点方法指针;
  165. FARPROC lpDllEntryPoint = NULL;
  166. //获取卸载函数DllUnregisterServer地址;
  167. lpDllEntryPoint = GetProcAddress(hModule, "DllUnregisterServer");
  168. if ( lpDllEntryPoint == NULL )
  169. {
  170. #ifdef _DEBUG
  171. OutputDebugString(_T("参数DllUnregisterServer 指针无效\n"));
  172. #endif
  173. FreeLibrary(hModule);
  174. return FALSE;
  175. }
  176. // 开始调用卸载函数卸载ocx控件;
  177. if ( FAILED((*lpDllEntryPoint)()) )
  178. {
  179. #ifdef _DEBUG
  180. OutputDebugString(_T("参数DllUnregisterServer 注册失败\n"));
  181. #endif
  182. FreeLibrary(hModule);
  183. return FALSE;
  184. }
  185. FreeLibrary(hModule);
  186. return TRUE;
  187. }
  188. /************************************************************************/
  189. /* 函数:[2/6/2017 IT];
  190. /* 描述:;
  191. /* 参数:;
  192. /* [IN] :;
  193. /* [OUT] :;
  194. /* [IN/OUT] :;
  195. /* 返回:void;
  196. /* 注意:;
  197. /* 示例:;
  198. /*
  199. /* 修改:;
  200. /* 日期:;
  201. /* 内容:;
  202. /************************************************************************/
  203. BOOL IsRegisterOcx(IN LPCTSTR lpszOCXfile)
  204. {
  205. // 参数有效性判断 ;
  206. if ( lpszOCXfile == NULL )
  207. {
  208. #ifdef _DEBUG
  209. OutputDebugString(_T("参数lpszOCXfile 指针无效\n"));
  210. #endif
  211. return FALSE;
  212. }
  213. if ( !PathFileExists(lpszOCXfile) )
  214. {
  215. #ifdef _DEBUG
  216. OutputDebugString(_T("参数lpszOCXfile 路径无效\n"));
  217. #endif
  218. return FALSE;
  219. }
  220. // 载入TypeLib
  221. ITypeLib *pTypeLib = NULL;
  222. #ifdef UNICODE
  223. HRESULT hr = LoadTypeLib(lpszOCXfile, &pTypeLib);
  224. #else
  225. // 多字节需要转换成UNICODE编码;
  226. // ....
  227. HRESULT hr = LoadTypeLib(lpszOCXfile, &pTypeLib);
  228. #endif
  229. if (FAILED(hr))
  230. {
  231. #ifdef _DEBUG
  232. OutputDebugString(_T("载入TypeLib失败\n"));
  233. #endif
  234. return FALSE;
  235. }
  236. // 成功载入后,获取guid;
  237. CString strGuid = _T("");
  238. // 获取类型信息;
  239. ITypeInfo *pTypeInfo = NULL;
  240. hr = pTypeLib->GetTypeInfo(0, &pTypeInfo); // 索引0方法,1事件,2控制;
  241. if (FAILED(hr)) {
  242. pTypeLib->Release();
  243. return FALSE;
  244. }
  245. // 获取类型属性;
  246. TYPEATTR* pTypeAttr = NULL;
  247. hr = pTypeInfo->GetTypeAttr(&pTypeAttr);
  248. if (FAILED(hr)) {
  249. pTypeLib->Release();
  250. return FALSE;
  251. }
  252. // 找到guid;
  253. if (pTypeAttr->typekind != TKIND_DISPATCH)
  254. {
  255. pTypeInfo->ReleaseTypeAttr(pTypeAttr);
  256. pTypeLib->Release();
  257. return FALSE;
  258. }
  259. strGuid.Format(_T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  260. pTypeAttr->guid.Data1,pTypeAttr->guid.Data2,pTypeAttr->guid.Data3
  261. ,pTypeAttr->guid.Data4[0],pTypeAttr->guid.Data4[1],pTypeAttr->guid.Data4[2],pTypeAttr->guid.Data4[3],
  262. pTypeAttr->guid.Data4[4],pTypeAttr->guid.Data4[5],pTypeAttr->guid.Data4[6],pTypeAttr->guid.Data4[7]);
  263. pTypeInfo->ReleaseTypeAttr(pTypeAttr);
  264. pTypeLib->Release();
  265. // 再到注册表里查找是否被注册过了;
  266. CString strRegKey = _T("");
  267. if ( IsWow64() )
  268. strRegKey.Format(_T("SOFTWARE\\CLASSES\\WOW6432NODE\\INTERFACE\\%s"), strGuid);
  269. else
  270. strRegKey.Format(_T("SOFTWARE\\CLASSES\\INTERFACE\\%s"), strGuid);
  271. HKEY hKey;
  272. return (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,strRegKey,0,KEY_READ,&hKey));
  273. }
  274. /************************************************************************/
  275. /* 函数:WriteTextLog[7/28/2016 IT];
  276. /* 描述:写文本日志;
  277. /* 参数:;
  278. /* [IN] :;
  279. /* 返回:void;
  280. /* 注意:;
  281. /* 示例:;
  282. /*
  283. /* 修改:;
  284. /* 日期:;
  285. /* 内容:;
  286. /************************************************************************/
  287. void WriteTextLog(const TCHAR *format, ...)
  288. {
  289. try
  290. {
  291. static ThreadSection _critSection;
  292. AutoThreadSection aSection(&_critSection);
  293. // 解析出日志路径;
  294. TCHAR szlogpath[MAX_PATH] = {0};
  295. static TCHAR szModulePath[MAX_PATH] = {0};
  296. static TCHAR szFna[_MAX_DIR] = { 0 };
  297. if ( szModulePath[0] == _T('\0') )
  298. {
  299. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  300. TCHAR szDir[_MAX_DIR] = { 0 };
  301. TCHAR szExt[_MAX_DIR] = { 0 };
  302. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  303. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  304. _tcscpy_s(szModulePath, szDrive);
  305. _tcscat_s(szModulePath, szDir);
  306. }
  307. _stprintf_s(szlogpath, _T("%s%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]").GetString());
  308. // 打开或创建文件;
  309. CStdioFile fp;
  310. if (PathFileExists(szlogpath))
  311. {
  312. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  313. {
  314. return;
  315. }
  316. fp.SeekToEnd();
  317. }
  318. else
  319. {
  320. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  321. return;
  322. }
  323. // 格式化前设置语言区域;
  324. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  325. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  326. // 格式化日志内容;
  327. va_list args = NULL;
  328. int len = 0;
  329. TCHAR *buffer = NULL;
  330. va_start( args, format );
  331. // _vscprintf doesn't count. terminating '\0'
  332. len = _vsctprintf_p( format, args );
  333. if ( len == -1 )
  334. {
  335. goto clear;
  336. }
  337. len++;
  338. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  339. _vstprintf_s( buffer, len, format, args ); // C4996
  340. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  341. // 将日志内容输入到文件中;
  342. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  343. fp.WriteString(buffer);
  344. fp.WriteString(_T("\n"));
  345. // 关闭文件,释放资源并设置回原语言区域;
  346. free( buffer );
  347. clear:
  348. _tsetlocale(LC_CTYPE, old_locale);
  349. free(old_locale);//还原区域设定;
  350. fp.Close();
  351. }
  352. catch (CException *e)
  353. {
  354. e->ReportError();
  355. e->Delete();
  356. }
  357. }