Global.cpp 29 KB

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