Global.cpp 21 KB

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