Global.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. g_Config.strMIInitBat = szValue;
  234. if (_tcslen(szValue) == 0)
  235. {
  236. WritePrivateProfileString(_T("SATHelper"), _T("MIInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_mi.bat"), szConfigpath);
  237. }
  238. GetPrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), NULL, szValue, MAX_PATH, szConfigpath);
  239. g_Config.strSCBCInitBat= szValue;
  240. if (_tcslen(szValue) == 0)
  241. {
  242. WritePrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_scbc.bat"), szConfigpath);
  243. }
  244. g_Config.bGenerics = GetPrivateProfileInt(_T("ir-device"), _T("generics"), 0, szConfigpath);
  245. if (PathFileExists(g_szPython27Dir))
  246. {
  247. g_Config.redratpath = g_szPython27Dir;
  248. g_Config.redratpath.append("Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe");
  249. _stprintf_s(g_szResuorceCfg, _T("%sLib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"), g_szPython27Dir);
  250. }
  251. else
  252. {
  253. GetPrivateProfileString(_T("ir-device"), _T("redratpath"), NULL, szValue, MAX_PATH, szConfigpath);
  254. g_Config.redratpath = szValue;
  255. memset(szValue, 0, MAX_PATH);
  256. }
  257. GetPrivateProfileString(_T("ir-device"), _T("signal"), NULL, szValue, MAX_PATH, szConfigpath);
  258. g_Config.signaldir = szValue;
  259. memset(szValue, 0, MAX_PATH);
  260. GetPrivateProfileString(_T("ir-device"), _T("use-signal"), NULL, szValue, MAX_PATH, szConfigpath);
  261. g_Config.use_signal = szValue;
  262. memset(szValue, 0, MAX_PATH);
  263. // Service;
  264. g_Config.bAutoLogin = GetPrivateProfileInt(_T("SATService"), _T("AutoLogin"), 0, szConfigpath);
  265. GetPrivateProfileString(_T("SATService"), _T("UserName"), NULL, szValue, MAX_PATH, szConfigpath);
  266. g_Config.strSATUserName = szValue;
  267. memset(szValue, 0, MAX_PATH);
  268. GetPrivateProfileString(_T("SATService"), _T("Password"), NULL, szValue, MAX_PATH, szConfigpath);
  269. g_Config.strSATPassword = szValue;
  270. memset(szValue, 0, MAX_PATH);
  271. GetPrivateProfileString(_T("SATService"), _T("Actuator"), NULL, szValue, MAX_PATH, szConfigpath);
  272. g_Config.strActuator = szValue;
  273. memset(szValue, 0, MAX_PATH);
  274. GetPrivateProfileString(_T("SATService"), _T("ServiceIP"), NULL, szValue, MAX_PATH, szConfigpath);
  275. g_Config.strServiceIP = szValue;
  276. memset(szValue, 0, MAX_PATH);
  277. GetPrivateProfileString(_T("SATService"), _T("ServicePort"), NULL, szValue, MAX_PATH, szConfigpath);
  278. g_Config.strServicePort = szValue;
  279. memset(szValue, 0, MAX_PATH);
  280. // 是否开启SATHelper自动重连功能;
  281. g_Config.bAutoReconnect = GetPrivateProfileInt(_T("UB530"), _T("AutoReconnect"), 0, szConfigpath);
  282. // 重连周期:分钟为单位;
  283. g_Config.nReconnectCycle = GetPrivateProfileInt(_T("UB530"), _T("ReconnectCycle"), 60, szConfigpath);
  284. //////////////////////////////////////////////////////////////////////////
  285. GetPrivateProfileString(_T("TestWizard"), _T("xmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
  286. g_Config.twSignaldir = szValue;
  287. memset(szValue, 0, MAX_PATH);
  288. GetPrivateProfileString(_T("TestWizard"), _T("useSignal"), NULL, szValue, MAX_PATH, szConfigpath);
  289. g_Config.twUseSignal = szValue;
  290. memset(szValue, 0, MAX_PATH);
  291. g_Config.twPort = GetPrivateProfileInt(_T("TestWizard"), _T("Com"), 0, szConfigpath);
  292. }
  293. BOOL GetOrientation(IN Image* pImg)
  294. {
  295. if (pImg == NULL) return FALSE;
  296. UINT totalBufferSize;
  297. UINT numProperties;
  298. pImg->GetPropertySize(&totalBufferSize, &numProperties);
  299. // Allocate the buffer that will receive the property items.
  300. PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
  301. // Fill the buffer.
  302. pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
  303. // Print the id data member of each property item.
  304. for (UINT j = 0; j < numProperties; ++j)
  305. {
  306. if (PropertyTagOrientation == pAllItems[j].id)
  307. {
  308. short* ptrLong = (short*)(pAllItems[j].value);
  309. int ret = (int)*ptrLong;
  310. free(pAllItems);
  311. return ret;
  312. }
  313. }
  314. free(pAllItems);
  315. return TRUE;
  316. }
  317. /************************************************************************/
  318. /*
  319. 函数:GetEncoderClsid
  320. 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
  321. 参数:
  322. IN: format 要获取的图像格式;
  323. OUT: pClsid 返回符合条件的图像编码器信息;
  324. 返回:成功返回编码器索引,否则返回-1;
  325. */
  326. /************************************************************************/
  327. int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
  328. {
  329. // GDI+支持的图像编码器数量;
  330. UINT numEncoders = 0;
  331. // GDI+所有图像格式编码器详细信息所需要的空间大小;
  332. UINT nSize = 0;
  333. ImageCodecInfo* pImageCodecInfo = NULL;
  334. // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
  335. GetImageEncodersSize(&numEncoders, &nSize);
  336. if (nSize == 0)
  337. return -1;
  338. //3.为ImageCodecInfo数组分配足额空间;
  339. pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
  340. if (pImageCodecInfo == NULL)
  341. return -1;
  342. //4.获取所有的图像编码器信息;
  343. GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
  344. //5.查找符合的图像编码器的Clsid;
  345. for (UINT i = 0; i < numEncoders; ++i)
  346. {
  347. if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
  348. {
  349. *pClsid = pImageCodecInfo[i].Clsid;
  350. free(pImageCodecInfo);
  351. return i; // Success
  352. }
  353. }
  354. //6.释放步骤3分配的内存;
  355. free(pImageCodecInfo);
  356. return -1;
  357. }
  358. BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath)
  359. {
  360. if (!PathFileExists(lpPath))
  361. return FALSE;
  362. if (*pImg)
  363. delete* pImg;
  364. *pImg = NULL;
  365. #ifdef UNICODE
  366. * pImg = Image::FromFile(lpPath);
  367. #else
  368. BSTR strtmp = _bstr_t(lpPath);
  369. *pImg = Image::FromFile(strtmp, TRUE);
  370. SysFreeString(strtmp);
  371. #endif
  372. return (*pImg ? TRUE : FALSE);
  373. }
  374. BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
  375. {
  376. if (pBuffer == NULL)
  377. return FALSE;
  378. if (*pImg)
  379. delete* pImg;
  380. *pImg = NULL;
  381. HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
  382. if (hMemery == NULL)
  383. return FALSE;
  384. BYTE* pMem = (BYTE*)GlobalLock(hMemery);
  385. memcpy(pMem, pBuffer, nBufLen);
  386. IStream* pstream = NULL;
  387. CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
  388. *pImg = Image::FromStream(pstream);
  389. GlobalUnlock(hMemery);
  390. pstream->Release();
  391. return (*pImg ? TRUE : FALSE);
  392. }
  393. // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
  394. BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath)
  395. {
  396. if (!PathFileExists(lpPath))
  397. return FALSE;
  398. if (*pImg)
  399. delete* pImg;
  400. *pImg = NULL;
  401. return LoadImgFromFile(pImg, lpPath);
  402. CFile fp;
  403. CFileException e;
  404. BOOL bRet = FALSE;
  405. if (fp.Open(lpPath, CFile::modeRead, &e))
  406. {
  407. DWORD dwLength = (DWORD)fp.GetLength();
  408. BYTE* pData = new BYTE[dwLength];
  409. fp.Read(pData, dwLength);
  410. fp.Close();
  411. bRet = LoadImgFromBuffer(pImg, pData, dwLength);
  412. if (pData)
  413. delete[]pData;
  414. }
  415. return bRet;
  416. }
  417. /************************************************************************/
  418. /* 函数:LoadImgFromResource[9/21/2016 IT];
  419. /* 描述:从资源中加载;
  420. /* 参数:;
  421. /* [IN] :;
  422. /* [OUT] :;
  423. /* [IN/OUT] :;
  424. /* 返回:void;
  425. /* 注意:;
  426. /* 示例:;
  427. /*
  428. /* 修改:;
  429. /* 日期:;
  430. /* 内容:;
  431. /************************************************************************/
  432. Image* LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
  433. {
  434. HGLOBAL hGlobal = NULL;
  435. HRSRC hSource = NULL;
  436. LPVOID lpBuffer = NULL;
  437. DWORD dwSize = 0;
  438. // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
  439. hSource = FindResource(hModule, lpName, lpType);
  440. if (hSource == NULL)
  441. {
  442. _tprintf(_T("载入资源失败:%s"), lpName);
  443. return NULL;
  444. }
  445. // 2.获取资源的大小;
  446. dwSize = (UINT)SizeofResource(NULL, hSource);
  447. // 3.加载资源;
  448. hGlobal = LoadResource(NULL, hSource);
  449. if (hGlobal == NULL)
  450. {
  451. _tprintf(_T("载入资源失败:%s"), lpName);
  452. return NULL;
  453. }
  454. // 4.锁定资源,获取buffer;
  455. lpBuffer = LockResource(hGlobal);
  456. if (lpBuffer == NULL)
  457. {
  458. _tprintf(_T("载入资源失败:%s"), lpName);
  459. return NULL;
  460. }
  461. // lpFileFullName需要先判断文件是否存在??不需要;
  462. Image* pImg = NULL;
  463. LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
  464. UnlockResource(hGlobal);
  465. FreeResource(hGlobal);
  466. return pImg;
  467. }
  468. BOOL SaveImgByRotate(LPCTSTR lpszFileName, BYTE* pBuffer, const INT& nBufLen, BOOL bHoriontal, BOOL bVertically)
  469. {
  470. Image* pImg = NULL;
  471. HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
  472. if (hMemery == NULL)
  473. return FALSE;
  474. BYTE* pMem = NULL;
  475. pMem = (BYTE*)GlobalLock(hMemery);
  476. if (pMem == NULL)
  477. return FALSE;
  478. memcpy(pMem, pBuffer, nBufLen);
  479. IStream* pstream = NULL;
  480. HRESULT hr = CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
  481. if (pstream == NULL)
  482. return FALSE;
  483. pImg = Image::FromStream(pstream);
  484. GlobalUnlock(hMemery);
  485. pstream->Release();
  486. if (bHoriontal && !bVertically)
  487. pImg->RotateFlip(RotateNoneFlipX);// 水平翻转;
  488. else if (bHoriontal && bVertically)
  489. pImg->RotateFlip(Rotate180FlipNone);// 270度;
  490. else if (!bHoriontal && bVertically)
  491. pImg->RotateFlip(Rotate180FlipX);// 垂直翻转;
  492. CString strNewfile = lpszFileName;
  493. // 需要判断路径是否存在,不存在创建目录;
  494. int nIndex = strNewfile.ReverseFind(_T('\\'));
  495. if (nIndex == -1)
  496. return FALSE;
  497. if (!PathFileExists(strNewfile.Left(nIndex)))
  498. {
  499. // 如果文件夹不存在,创建;
  500. SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
  501. }
  502. nIndex = strNewfile.ReverseFind(_T('.'));
  503. if (nIndex == -1)
  504. return FALSE;
  505. Status stat = GenericError;
  506. CLSID encoderClsid = { 0 };
  507. BSTR newfile = strNewfile.AllocSysString();
  508. strNewfile = strNewfile.Mid(nIndex + 1);
  509. if (strNewfile.CompareNoCase(_T("bmp")) == 0)
  510. {
  511. GetEncoderClsid(L"image/bmp", &encoderClsid);
  512. stat = pImg->Save(newfile, &encoderClsid, NULL);
  513. }
  514. else if (strNewfile.CompareNoCase(_T("png")) == 0)
  515. {
  516. GetEncoderClsid(L"image/png", &encoderClsid);
  517. stat = pImg->Save(newfile, &encoderClsid, NULL);
  518. }
  519. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  520. {
  521. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  522. EncoderParameters encoderParameters;
  523. encoderParameters.Count = 1;
  524. encoderParameters.Parameter[0].Guid = EncoderQuality;
  525. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  526. encoderParameters.Parameter[0].NumberOfValues = 1;
  527. // Save the image as a JPEG with quality level 100.
  528. ULONG uQuality = 100;
  529. encoderParameters.Parameter[0].Value = &uQuality;
  530. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  531. }
  532. if (pImg)
  533. delete pImg;
  534. pImg = NULL;
  535. SysFreeString(newfile);
  536. return stat == Ok ? TRUE : FALSE;
  537. }
  538. void MKDIR(LPCTSTR dir)
  539. {
  540. //////////////////////////////////////////////////////////////////////////
  541. // 创建目录;
  542. int nleft = 0;
  543. int nIndex = -1;
  544. TString strdir = dir;
  545. if (strdir.at(strdir.size() - 1) != _T('\\'))
  546. strdir.append(_T("\\"));
  547. // 共享路径和硬盘盘符;
  548. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  549. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  550. else if (strdir.at(2) == _T('\\'))
  551. nleft = 3;
  552. do
  553. {
  554. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  555. if (nIndex != TString::npos)
  556. {
  557. if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  558. {
  559. WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  560. break;
  561. }
  562. nleft += nIndex + 1;
  563. }
  564. } while (nIndex != -1);
  565. }
  566. // hModule 模块句柄 NULL表示当前模块;
  567. bool GetVersion(OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
  568. {
  569. TCHAR szFilePath[MAX_PATH] = { 0 };
  570. DWORD dwRet = GetModuleFileName(NULL, szFilePath, MAX_PATH);
  571. if (dwRet == 0)
  572. return false;
  573. VS_FIXEDFILEINFO* pVi = NULL;
  574. DWORD dwHandle = 0;
  575. int size = GetFileVersionInfoSize(szFilePath, &dwHandle);
  576. if (size > 0)
  577. {
  578. BYTE* buffer = new BYTE[size];
  579. memset(buffer, 0, size);
  580. if (GetFileVersionInfo(szFilePath, dwHandle, size, buffer))
  581. {
  582. if (VerQueryValue(buffer, _T("\\"), (LPVOID*)&pVi, (PUINT)&size))
  583. {
  584. pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
  585. pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
  586. pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
  587. pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
  588. pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
  589. pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
  590. pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
  591. pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
  592. delete[]buffer;
  593. return true;
  594. }
  595. }
  596. delete[]buffer;
  597. }
  598. return false;
  599. }
  600. void GetSysZoomRatio()
  601. {
  602. // Get desktop dc
  603. HDC desktopDc = GetDC(NULL);
  604. int horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
  605. int verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);
  606. switch ( horizontalDPI )
  607. {
  608. case 96:
  609. g_nSysZoomRatio = 100; // 100%缩放布局;
  610. break;
  611. case 120:
  612. g_nSysZoomRatio = 125; // 125%缩放布局;
  613. break;
  614. case 144:
  615. g_nSysZoomRatio = 150; // 150%缩放布局;
  616. break;
  617. case 192:
  618. g_nSysZoomRatio = 200; // 200%缩放布局;
  619. break;
  620. default:
  621. break;
  622. }
  623. #if 0
  624. // Get native resolution
  625. int horizontalResolution = GetDeviceCaps(desktopDc, HORZRES);
  626. int verticalResolution = GetDeviceCaps(desktopDc, VERTRES);
  627. // 获取窗口当前显示的监视器
  628. // 使用桌面的句柄.
  629. HWND hWnd = GetDesktopWindow();
  630. HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
  631. // 获取监视器逻辑宽度与高度
  632. MONITORINFOEX miex;
  633. miex.cbSize = sizeof(miex);
  634. GetMonitorInfo(hMonitor, &miex);
  635. int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
  636. int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);
  637. // 获取监视器物理宽度与高度
  638. DEVMODE dm;
  639. dm.dmSize = sizeof(dm);
  640. dm.dmDriverExtra = 0;
  641. EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
  642. int cxPhysical = dm.dmPelsWidth;
  643. int cyPhysical = dm.dmPelsHeight;
  644. // 缩放比例计算 实际上使用任何一个即可
  645. double horzScale = ((double)cxPhysical / (double)cxLogical);
  646. double vertScale = ((double)cyPhysical / (double)cyLogical);
  647. assert(horzScale == vertScale); // 宽或高这个缩放值应该是相等的
  648. #endif
  649. }
  650. }