Global.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include <tchar.h>
  4. #include <direct.h>
  5. #include <SetupAPI.h>
  6. #include <InitGuid.h>
  7. #include <WinIoCtl.h>
  8. #pragma comment(lib, "SetupAPI.lib")
  9. namespace Global
  10. {
  11. //////////////////////////////////////////////////////////////////////////
  12. // 全局变量;
  13. HINSTANCE g_hLanguageDLL = NULL;
  14. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  15. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  16. TCHAR g_szFna[MAX_PATH] = { 0 };
  17. TCHAR g_szConfig[MAX_PATH] = { 0 };
  18. TCHAR g_szResuorceCfg[MAX_PATH] = { 0 };
  19. STConfig g_Config;
  20. TCHAR g_szPython27Dir[MAX_PATH] = {0};
  21. ULONGLONG g_ulWaitTime = 15000;
  22. int g_nSysZoomRatio = 100;
  23. TCHAR g_szVersion[MAX_PATH] = _T("5.50.20200921");
  24. BaiduCfg g_bdcfg;
  25. RsRunCfg g_rscfg;
  26. //////////////////////////////////////////////////////////////////////////
  27. // 全局函数;
  28. /************************************************************************/
  29. /* 函数:WriteTextLog[7/28/2009 Jeff];
  30. /* 描述:写文本日志;
  31. /* 参数:;
  32. /* [IN] :;
  33. /* 返回:void;
  34. /* 注意:;
  35. /* 示例:;
  36. /*
  37. /* 修改:;
  38. /* 日期:;
  39. /* 内容:;
  40. /************************************************************************/
  41. void WriteTextLog(const TCHAR* format, ...)
  42. {
  43. // 解析出日志路径;
  44. TCHAR szlogpath[MAX_PATH] = { 0 };
  45. _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
  46. // 打开或创建文件;
  47. FILE* fp = NULL;
  48. //if (_taccess(szlogpath, 0) != -1)
  49. #ifndef UNICODE
  50. if (_access(szlogpath, 0) != -1)
  51. #else
  52. if (_taccess(szlogpath, 0) != -1)
  53. #endif
  54. {// 存在;
  55. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  56. // 移动到末尾;
  57. fseek(fp, 0, SEEK_END);
  58. }
  59. else
  60. {// 不存在;
  61. _tfopen_s(&fp, szlogpath, _T("w+"));
  62. }
  63. if (fp == NULL)
  64. return;
  65. // 格式化前设置语言区域;
  66. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  67. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  68. // 格式化日志内容;
  69. va_list args = NULL;
  70. int len = 0;
  71. TCHAR* buffer = NULL;
  72. va_start(args, format);
  73. // _vscprintf doesn't count. terminating '\0'
  74. len = _vsctprintf(format, args) + 1;
  75. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  76. _vstprintf_s(buffer, len, format, args);
  77. // 将日志内容输入到文件中;
  78. // 获取今年年份;
  79. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  80. struct tm gmtm = { 0 };
  81. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  82. _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);
  83. // 关闭文件,释放资源并设置回原语言区域;
  84. free(buffer);
  85. fclose(fp);
  86. _tsetlocale(LC_CTYPE, old_locale);
  87. free(old_locale);//还原区域设定;
  88. }
  89. void WriteTextLogEx(int nType, const TCHAR* format, ...)
  90. {
  91. // 解析出日志路径;
  92. TCHAR szlogpath[MAX_PATH] = { 0 };
  93. _stprintf_s(szlogpath, _T("%s%s-%d.txt"), g_szCurModuleDir, g_szFna, nType);
  94. // 打开或创建文件;
  95. FILE* fp = NULL;
  96. //if (_taccess(szlogpath, 0) != -1)
  97. #ifndef UNICODE
  98. if (_access(szlogpath, 0) != -1)
  99. #else
  100. if (_taccess(szlogpath, 0) != -1)
  101. #endif
  102. {// 存在;
  103. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  104. // 移动到末尾;
  105. fseek(fp, 0, SEEK_END);
  106. }
  107. else
  108. {// 不存在;
  109. _tfopen_s(&fp, szlogpath, _T("w+"));
  110. }
  111. if (fp == NULL)
  112. return;
  113. // 格式化前设置语言区域;
  114. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  115. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  116. // 格式化日志内容;
  117. va_list args = NULL;
  118. int len = 0;
  119. TCHAR* buffer = NULL;
  120. va_start(args, format);
  121. // _vscprintf doesn't count. terminating '\0'
  122. len = _vsctprintf(format, args) + 1;
  123. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  124. _vstprintf_s(buffer, len, format, args);
  125. // 将日志内容输入到文件中;
  126. // 获取今年年份;
  127. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  128. struct tm gmtm = { 0 };
  129. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  130. _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);
  131. // 关闭文件,释放资源并设置回原语言区域;
  132. free(buffer);
  133. fclose(fp);
  134. _tsetlocale(LC_CTYPE, old_locale);
  135. free(old_locale);//还原区域设定;
  136. }
  137. int ReadReg(char* path, char* key, char* value)
  138. {
  139. HKEY hKey;
  140. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
  141. if (ret != ERROR_SUCCESS)
  142. {
  143. return 1;
  144. }
  145. //读取KEY
  146. DWORD dwType = REG_SZ; //数据类型
  147. DWORD cbData = 256;
  148. ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData);
  149. if (ret != ERROR_SUCCESS)
  150. {
  151. RegCloseKey(hKey);
  152. return 1;
  153. }
  154. RegCloseKey(hKey);
  155. return 0;
  156. }
  157. BOOL Python27Dir()
  158. {
  159. HKEY hKey;
  160. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
  161. if (ret != ERROR_SUCCESS)
  162. return FALSE;
  163. //读取KEY
  164. DWORD dwType = REG_SZ; //数据类型
  165. DWORD cbData = 256;
  166. ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
  167. if (ret != ERROR_SUCCESS)
  168. {
  169. RegCloseKey(hKey);
  170. return FALSE;
  171. }
  172. RegCloseKey(hKey);
  173. return TRUE;
  174. }
  175. // 通过注册表查找系统当前串口信息;
  176. BOOL GetSysSerialPort(std::vector<std::string>& vtports)
  177. {
  178. HKEY hKey;
  179. LSTATUS lReg = 0;
  180. DWORD dwMaxValLen = 0;
  181. DWORD dwValNum = 0;
  182. lReg = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
  183. if (lReg != ERROR_SUCCESS)
  184. {
  185. //LOG4C((LOG_WARN, "Open Registry Error"));
  186. return FALSE;
  187. }
  188. lReg = RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValNum, &dwMaxValLen, NULL, NULL, NULL);
  189. if (lReg != ERROR_SUCCESS)
  190. {
  191. //LOG4C((LOG_WARN, "Getting Key Info Error"));
  192. return FALSE;
  193. }
  194. if (vtports.size())
  195. {
  196. vtports.clear();
  197. }
  198. LPSTR lpValName, lpComNum;
  199. DWORD dwValName, dwValSize = 6;
  200. for (DWORD i = 0; i < dwValNum; i++)
  201. {
  202. dwValName = dwMaxValLen + 1;
  203. dwValSize = 6;
  204. lpValName = (LPSTR)VirtualAlloc(NULL, dwValName, MEM_COMMIT, PAGE_READWRITE);
  205. lReg = RegEnumValueA(hKey, i, lpValName, &dwValName, NULL, NULL, NULL, NULL);
  206. if ((lReg != ERROR_SUCCESS) && (lReg != ERROR_NO_MORE_ITEMS))
  207. {
  208. //LOG4C((LOG_WARN, "Enum Registry Error or No More Items"));
  209. continue;
  210. }
  211. lpComNum = (LPSTR)VirtualAlloc(NULL, 6, MEM_COMMIT, PAGE_READWRITE);
  212. lReg = RegQueryValueExA(hKey, lpValName, NULL, NULL, (LPBYTE)lpComNum, &dwValSize);
  213. if (lReg != ERROR_SUCCESS)
  214. {
  215. //LOG4C((LOG_WARN, "Can not get the name of the port"));
  216. continue;
  217. }
  218. vtports.push_back(lpComNum);
  219. VirtualFree(lpValName, 0, MEM_RELEASE);
  220. VirtualFree(lpComNum, 0, MEM_RELEASE);
  221. }
  222. return TRUE;
  223. }
  224. // 读取配置文件-预留;
  225. void GetConfig()
  226. {
  227. // Python27安装目录;
  228. Python27Dir();
  229. TCHAR szConfigpath[MAX_PATH] = { 0 };
  230. _stprintf_s(szConfigpath, _T("%s%s.ini"), g_szCurModuleDir, g_szFna);
  231. _tcscpy_s(g_szConfig, szConfigpath);
  232. TCHAR szValue[MAX_PATH] = { 0 };
  233. g_Config.nLanguage = GetPrivateProfileInt(_T("SATHelper"), _T("Language"), 0, szConfigpath);
  234. g_Config.bSVNRealTimeUpdate = GetPrivateProfileInt(_T("SATHelper"), _T("SVNRealTimeUpdate"), 0, szConfigpath);
  235. // 读取配置内容;
  236. g_Config.enableTW = GetPrivateProfileInt(_T("SATHelper"), _T("enableTW"), 0, szConfigpath);
  237. g_Config.useTW = GetPrivateProfileInt(_T("SATHelper"), _T("useTW"), 0, szConfigpath);
  238. g_Config.useUB530 = GetPrivateProfileInt(_T("SATHelper"), _T("useUB530"), 1, szConfigpath);
  239. GetPrivateProfileString(_T("SATHelper"), _T("MIInitBat"), NULL, szValue, MAX_PATH, szConfigpath);
  240. if (_tcslen(szValue) == 0)
  241. {
  242. WritePrivateProfileString(_T("SATHelper"), _T("MIInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_mi.bat"), szConfigpath);
  243. _stprintf_s(szValue, _T("D:\\SAT\\tools\\atx-init\\atx-init_mi.bat"));
  244. }
  245. g_Config.strMIInitBat = szValue;
  246. GetPrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), NULL, szValue, MAX_PATH, szConfigpath);
  247. if (_tcslen(szValue) == 0)
  248. {
  249. WritePrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_scbc.bat"), szConfigpath);
  250. _stprintf_s(szValue, _T("D:\\SAT\\tools\\atx-init\\atx-init_scbc.bat"));
  251. }
  252. g_Config.strSCBCInitBat = szValue;
  253. // svn更新地址;
  254. GetPrivateProfileString(_T("SATHelper"), _T("SVNUpdateUrl"), NULL, szValue, MAX_PATH, szConfigpath);
  255. if (_tcslen(szValue) == 0)
  256. {
  257. WritePrivateProfileString(_T("SATHelper"), _T("SVNUpdateUrl"), _T("http://10.126.16.60:8580/btc_execute_se/ajaxInteractiveManage!getSvnVersion.action"), szConfigpath);
  258. _stprintf_s(szValue, _T("http://10.126.16.60:8580/btc_execute_se/ajaxInteractiveManage!getSvnVersion.action"));
  259. }
  260. g_Config.svnUpdateUrl = szValue;
  261. g_Config.bGenerics = GetPrivateProfileInt(_T("ir-device"), _T("generics"), 0, szConfigpath);
  262. if (PathFileExists(g_szPython27Dir))
  263. {
  264. g_Config.redratpath = g_szPython27Dir;
  265. g_Config.redratpath.append("Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe");
  266. _stprintf_s(g_szResuorceCfg, _T("%sLib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"), g_szPython27Dir);
  267. }
  268. else
  269. {
  270. GetPrivateProfileString(_T("ir-device"), _T("redratpath"), NULL, szValue, MAX_PATH, szConfigpath);
  271. g_Config.redratpath = szValue;
  272. memset(szValue, 0, MAX_PATH);
  273. }
  274. GetPrivateProfileString(_T("ir-device"), _T("signal"), NULL, szValue, MAX_PATH, szConfigpath);
  275. g_Config.signaldir = szValue;
  276. memset(szValue, 0, MAX_PATH);
  277. GetPrivateProfileString(_T("ir-device"), _T("use-signal"), NULL, szValue, MAX_PATH, szConfigpath);
  278. g_Config.use_signal = szValue;
  279. memset(szValue, 0, MAX_PATH);
  280. // Service;
  281. g_Config.bAutoLogin = GetPrivateProfileInt(_T("SATService"), _T("AutoLogin"), 0, szConfigpath);
  282. GetPrivateProfileString(_T("SATService"), _T("UserName"), NULL, szValue, MAX_PATH, szConfigpath);
  283. g_Config.strSATUserName = szValue;
  284. memset(szValue, 0, MAX_PATH);
  285. GetPrivateProfileString(_T("SATService"), _T("Password"), NULL, szValue, MAX_PATH, szConfigpath);
  286. g_Config.strSATPassword = szValue;
  287. memset(szValue, 0, MAX_PATH);
  288. GetPrivateProfileString(_T("SATService"), _T("Actuator"), NULL, szValue, MAX_PATH, szConfigpath);
  289. g_Config.strActuator = szValue;
  290. memset(szValue, 0, MAX_PATH);
  291. GetPrivateProfileString(_T("SATService"), _T("ServiceIP"), NULL, szValue, MAX_PATH, szConfigpath);
  292. g_Config.strServiceIP = szValue;
  293. memset(szValue, 0, MAX_PATH);
  294. GetPrivateProfileString(_T("SATService"), _T("ServicePort"), NULL, szValue, MAX_PATH, szConfigpath);
  295. g_Config.strServicePort = szValue;
  296. memset(szValue, 0, MAX_PATH);
  297. // 是否开启SATHelper自动重连功能;
  298. g_Config.bAutoReconnect = GetPrivateProfileInt(_T("UB530"), _T("AutoReconnect"), 0, szConfigpath);
  299. // 重连周期:分钟为单位;
  300. g_Config.nReconnectCycle = GetPrivateProfileInt(_T("UB530"), _T("ReconnectCycle"), 60, szConfigpath);
  301. //////////////////////////////////////////////////////////////////////////
  302. GetPrivateProfileString(_T("TestWizard"), _T("xmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
  303. g_Config.twSignaldir = szValue;
  304. memset(szValue, 0, MAX_PATH);
  305. GetPrivateProfileString(_T("TestWizard"), _T("useSignal"), NULL, szValue, MAX_PATH, szConfigpath);
  306. g_Config.twUseSignal = szValue;
  307. memset(szValue, 0, MAX_PATH);
  308. g_Config.twPort = GetPrivateProfileInt(_T("TestWizard"), _T("Com"), 0, szConfigpath);
  309. // u盘切换;
  310. g_Config.usbPort = GetPrivateProfileInt(_T("UsbSwitch"), _T("Com"), 0, szConfigpath);
  311. g_Config.enableUSB = GetPrivateProfileInt(_T("SATHelper"), _T("enableUSB"), 0, szConfigpath);
  312. g_Config.nPowerLanIndex = GetPrivateProfileInt(_T("PowerCtrl"), _T("LanIndex"), 0, szConfigpath);
  313. // 电视串口;
  314. g_Config.tvPort = GetPrivateProfileInt(_T("SATHelper"), _T("TVPort"), 0, szConfigpath);
  315. g_Config.enableTVPort = GetPrivateProfileInt(_T("SATHelper"), _T("enableTVPort"), 0, szConfigpath);
  316. g_Config.bRememberFTPInfo = GetPrivateProfileInt(_T("SATHelper"), _T("rememberFTPInfo"), 0, szConfigpath);
  317. GetPrivateProfileString(_T("SATHelper"), _T("ftp_user"), NULL, szValue, MAX_PATH, szConfigpath);
  318. g_Config.strFtpUser = szValue;
  319. GetPrivateProfileString(_T("SATHelper"), _T("ftp_password"), NULL, szValue, MAX_PATH, szConfigpath);
  320. g_Config.strFtpPassword = szValue;
  321. }
  322. BOOL GetOrientation(IN Image* pImg)
  323. {
  324. if (pImg == NULL) return FALSE;
  325. UINT totalBufferSize;
  326. UINT numProperties;
  327. pImg->GetPropertySize(&totalBufferSize, &numProperties);
  328. // Allocate the buffer that will receive the property items.
  329. PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
  330. // Fill the buffer.
  331. pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
  332. // Print the id data member of each property item.
  333. for (UINT j = 0; j < numProperties; ++j)
  334. {
  335. if (PropertyTagOrientation == pAllItems[j].id)
  336. {
  337. short* ptrLong = (short*)(pAllItems[j].value);
  338. int ret = (int)*ptrLong;
  339. free(pAllItems);
  340. return ret;
  341. }
  342. }
  343. free(pAllItems);
  344. return TRUE;
  345. }
  346. /************************************************************************/
  347. /*
  348. 函数:GetEncoderClsid
  349. 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
  350. 参数:
  351. IN: format 要获取的图像格式;
  352. OUT: pClsid 返回符合条件的图像编码器信息;
  353. 返回:成功返回编码器索引,否则返回-1;
  354. */
  355. /************************************************************************/
  356. int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
  357. {
  358. // GDI+支持的图像编码器数量;
  359. UINT numEncoders = 0;
  360. // GDI+所有图像格式编码器详细信息所需要的空间大小;
  361. UINT nSize = 0;
  362. ImageCodecInfo* pImageCodecInfo = NULL;
  363. // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
  364. GetImageEncodersSize(&numEncoders, &nSize);
  365. if (nSize == 0)
  366. return -1;
  367. //3.为ImageCodecInfo数组分配足额空间;
  368. pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
  369. if (pImageCodecInfo == NULL)
  370. return -1;
  371. //4.获取所有的图像编码器信息;
  372. GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
  373. //5.查找符合的图像编码器的Clsid;
  374. for (UINT i = 0; i < numEncoders; ++i)
  375. {
  376. if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
  377. {
  378. *pClsid = pImageCodecInfo[i].Clsid;
  379. free(pImageCodecInfo);
  380. return i; // Success
  381. }
  382. }
  383. //6.释放步骤3分配的内存;
  384. free(pImageCodecInfo);
  385. return -1;
  386. }
  387. BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath)
  388. {
  389. if (!PathFileExists(lpPath))
  390. return FALSE;
  391. if (*pImg)
  392. delete* pImg;
  393. *pImg = NULL;
  394. #ifdef UNICODE
  395. * pImg = Image::FromFile(lpPath);
  396. #else
  397. BSTR strtmp = _bstr_t(lpPath);
  398. *pImg = Image::FromFile(strtmp, TRUE);
  399. SysFreeString(strtmp);
  400. #endif
  401. return (*pImg ? TRUE : FALSE);
  402. }
  403. BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
  404. {
  405. if (pBuffer == NULL)
  406. return FALSE;
  407. if (*pImg)
  408. delete* pImg;
  409. *pImg = NULL;
  410. HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
  411. if (hMemery == NULL)
  412. return FALSE;
  413. BYTE* pMem = (BYTE*)GlobalLock(hMemery);
  414. memcpy(pMem, pBuffer, nBufLen);
  415. IStream* pstream = NULL;
  416. CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
  417. *pImg = Image::FromStream(pstream);
  418. GlobalUnlock(hMemery);
  419. pstream->Release();
  420. return (*pImg ? TRUE : FALSE);
  421. }
  422. // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
  423. BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath)
  424. {
  425. if (!PathFileExists(lpPath))
  426. return FALSE;
  427. if (*pImg)
  428. delete* pImg;
  429. *pImg = NULL;
  430. return LoadImgFromFile(pImg, lpPath);
  431. CFile fp;
  432. CFileException e;
  433. BOOL bRet = FALSE;
  434. if (fp.Open(lpPath, CFile::modeRead, &e))
  435. {
  436. DWORD dwLength = (DWORD)fp.GetLength();
  437. BYTE* pData = new BYTE[dwLength];
  438. fp.Read(pData, dwLength);
  439. fp.Close();
  440. bRet = LoadImgFromBuffer(pImg, pData, dwLength);
  441. if (pData)
  442. delete[]pData;
  443. }
  444. return bRet;
  445. }
  446. /************************************************************************/
  447. /* 函数:LoadImgFromResource[9/21/2016 IT];
  448. /* 描述:从资源中加载;
  449. /* 参数:;
  450. /* [IN] :;
  451. /* [OUT] :;
  452. /* [IN/OUT] :;
  453. /* 返回:void;
  454. /* 注意:;
  455. /* 示例:;
  456. /*
  457. /* 修改:;
  458. /* 日期:;
  459. /* 内容:;
  460. /************************************************************************/
  461. Image* LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
  462. {
  463. HGLOBAL hGlobal = NULL;
  464. HRSRC hSource = NULL;
  465. LPVOID lpBuffer = NULL;
  466. DWORD dwSize = 0;
  467. // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
  468. hSource = FindResource(hModule, lpName, lpType);
  469. if (hSource == NULL)
  470. {
  471. _tprintf(_T("载入资源失败:%s"), lpName);
  472. return NULL;
  473. }
  474. // 2.获取资源的大小;
  475. dwSize = (UINT)SizeofResource(NULL, hSource);
  476. // 3.加载资源;
  477. hGlobal = LoadResource(NULL, hSource);
  478. if (hGlobal == NULL)
  479. {
  480. _tprintf(_T("载入资源失败:%s"), lpName);
  481. return NULL;
  482. }
  483. // 4.锁定资源,获取buffer;
  484. lpBuffer = LockResource(hGlobal);
  485. if (lpBuffer == NULL)
  486. {
  487. _tprintf(_T("载入资源失败:%s"), lpName);
  488. return NULL;
  489. }
  490. // lpFileFullName需要先判断文件是否存在??不需要;
  491. Image* pImg = NULL;
  492. LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
  493. UnlockResource(hGlobal);
  494. FreeResource(hGlobal);
  495. return pImg;
  496. }
  497. BOOL SaveImgByRotate(LPCTSTR lpszFileName, BYTE* pBuffer, const INT& nBufLen, BOOL bHoriontal, BOOL bVertically)
  498. {
  499. Image* pImg = NULL;
  500. HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
  501. if (hMemery == NULL)
  502. return FALSE;
  503. BYTE* pMem = NULL;
  504. pMem = (BYTE*)GlobalLock(hMemery);
  505. if (pMem == NULL)
  506. return FALSE;
  507. memcpy(pMem, pBuffer, nBufLen);
  508. IStream* pstream = NULL;
  509. HRESULT hr = CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
  510. if (pstream == NULL)
  511. return FALSE;
  512. pImg = Image::FromStream(pstream);
  513. GlobalUnlock(hMemery);
  514. pstream->Release();
  515. if (bHoriontal && !bVertically)
  516. pImg->RotateFlip(RotateNoneFlipX);// 水平翻转;
  517. else if (bHoriontal && bVertically)
  518. pImg->RotateFlip(Rotate180FlipNone);// 270度;
  519. else if (!bHoriontal && bVertically)
  520. pImg->RotateFlip(Rotate180FlipX);// 垂直翻转;
  521. CString strNewfile = lpszFileName;
  522. // 需要判断路径是否存在,不存在创建目录;
  523. int nIndex = strNewfile.ReverseFind(_T('\\'));
  524. if (nIndex == -1)
  525. return FALSE;
  526. if (!PathFileExists(strNewfile.Left(nIndex)))
  527. {
  528. // 如果文件夹不存在,创建;
  529. SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
  530. }
  531. nIndex = strNewfile.ReverseFind(_T('.'));
  532. if (nIndex == -1)
  533. return FALSE;
  534. Status stat = GenericError;
  535. CLSID encoderClsid = { 0 };
  536. BSTR newfile = strNewfile.AllocSysString();
  537. strNewfile = strNewfile.Mid(nIndex + 1);
  538. if (strNewfile.CompareNoCase(_T("bmp")) == 0)
  539. {
  540. GetEncoderClsid(L"image/bmp", &encoderClsid);
  541. stat = pImg->Save(newfile, &encoderClsid, NULL);
  542. }
  543. else if (strNewfile.CompareNoCase(_T("png")) == 0)
  544. {
  545. GetEncoderClsid(L"image/png", &encoderClsid);
  546. stat = pImg->Save(newfile, &encoderClsid, NULL);
  547. }
  548. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  549. {
  550. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  551. EncoderParameters encoderParameters;
  552. encoderParameters.Count = 1;
  553. encoderParameters.Parameter[0].Guid = EncoderQuality;
  554. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  555. encoderParameters.Parameter[0].NumberOfValues = 1;
  556. // Save the image as a JPEG with quality level 100.
  557. ULONG uQuality = 100;
  558. encoderParameters.Parameter[0].Value = &uQuality;
  559. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  560. }
  561. if (pImg)
  562. delete pImg;
  563. pImg = NULL;
  564. SysFreeString(newfile);
  565. return stat == Ok ? TRUE : FALSE;
  566. }
  567. void MKDIR(LPCTSTR dir)
  568. {
  569. //////////////////////////////////////////////////////////////////////////
  570. // 创建目录;
  571. int nleft = 0;
  572. int nIndex = -1;
  573. TString strdir = dir;
  574. if (strdir.at(strdir.size() - 1) != _T('\\'))
  575. strdir.append(_T("\\"));
  576. // 共享路径和硬盘盘符;
  577. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  578. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  579. else if (strdir.at(2) == _T('\\'))
  580. nleft = 3;
  581. do
  582. {
  583. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  584. if (nIndex != TString::npos)
  585. {
  586. if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  587. {
  588. WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  589. break;
  590. }
  591. nleft += nIndex + 1;
  592. }
  593. } while (nIndex != -1);
  594. }
  595. // hModule 模块句柄 NULL表示当前模块;
  596. bool GetVersion(OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
  597. {
  598. TCHAR szFilePath[MAX_PATH] = { 0 };
  599. DWORD dwRet = GetModuleFileName(NULL, szFilePath, MAX_PATH);
  600. if (dwRet == 0)
  601. return false;
  602. VS_FIXEDFILEINFO* pVi = NULL;
  603. DWORD dwHandle = 0;
  604. int size = GetFileVersionInfoSize(szFilePath, &dwHandle);
  605. if (size > 0)
  606. {
  607. BYTE* buffer = new BYTE[size];
  608. memset(buffer, 0, size);
  609. if (GetFileVersionInfo(szFilePath, dwHandle, size, buffer))
  610. {
  611. if (VerQueryValue(buffer, _T("\\"), (LPVOID*)&pVi, (PUINT)&size))
  612. {
  613. pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
  614. pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
  615. pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
  616. pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
  617. pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
  618. pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
  619. pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
  620. pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
  621. delete[]buffer;
  622. return true;
  623. }
  624. }
  625. delete[]buffer;
  626. }
  627. return false;
  628. }
  629. void GetSysZoomRatio()
  630. {
  631. // Get desktop dc
  632. HDC desktopDc = GetDC(NULL);
  633. int horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
  634. int verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);
  635. switch ( horizontalDPI )
  636. {
  637. case 96:
  638. g_nSysZoomRatio = 100; // 100%缩放布局;
  639. break;
  640. case 120:
  641. g_nSysZoomRatio = 125; // 125%缩放布局;
  642. break;
  643. case 144:
  644. g_nSysZoomRatio = 150; // 150%缩放布局;
  645. break;
  646. case 192:
  647. g_nSysZoomRatio = 200; // 200%缩放布局;
  648. break;
  649. default:
  650. break;
  651. }
  652. #if 0
  653. // Get native resolution
  654. int horizontalResolution = GetDeviceCaps(desktopDc, HORZRES);
  655. int verticalResolution = GetDeviceCaps(desktopDc, VERTRES);
  656. // 获取窗口当前显示的监视器
  657. // 使用桌面的句柄.
  658. HWND hWnd = GetDesktopWindow();
  659. HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
  660. // 获取监视器逻辑宽度与高度
  661. MONITORINFOEX miex;
  662. miex.cbSize = sizeof(miex);
  663. GetMonitorInfo(hMonitor, &miex);
  664. int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
  665. int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);
  666. // 获取监视器物理宽度与高度
  667. DEVMODE dm;
  668. dm.dmSize = sizeof(dm);
  669. dm.dmDriverExtra = 0;
  670. EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
  671. int cxPhysical = dm.dmPelsWidth;
  672. int cyPhysical = dm.dmPelsHeight;
  673. // 缩放比例计算 实际上使用任何一个即可
  674. double horzScale = ((double)cxPhysical / (double)cxLogical);
  675. double vertScale = ((double)cyPhysical / (double)cyLogical);
  676. assert(horzScale == vertScale); // 宽或高这个缩放值应该是相等的
  677. #endif
  678. }
  679. }