Global.cpp 21 KB

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