DeviceQuery.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. #include "StdAfx.h"
  2. #include "DeviceQuery.h"
  3. #include <winioctl.h>
  4. #include <Cfgmgr32.h>
  5. #include "..\cJson\cJSON.h"
  6. #include "..\Base64\Base64.h"
  7. #include "..\des\des.h"
  8. #include <locale.h>
  9. #include <Shlwapi.h>
  10. #ifdef _WRITE_LOG_
  11. /************************************************************************/
  12. /* 函数:WriteTextLog[7/28/2016 IT];
  13. /* 描述:写文本日志;
  14. /* 参数:;
  15. /* [IN] :;
  16. /* 返回:void;
  17. /* 注意:;
  18. /* 示例:;
  19. /*
  20. /* 修改:;
  21. /* 日期:;
  22. /* 内容:;
  23. /************************************************************************/
  24. void WriteTextLog(const TCHAR *format, ...)
  25. {
  26. try
  27. {
  28. //static ThreadSection _critSection;
  29. //AutoThreadSection aSection(&_critSection);
  30. // 解析出日志路径;
  31. TCHAR szlogpath[MAX_PATH] = {0};
  32. static TCHAR szModulePath[MAX_PATH] = {0};
  33. static TCHAR szFna[_MAX_DIR] = { 0 };
  34. if ( szModulePath[0] == _T('\0') )
  35. {
  36. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  37. TCHAR szDir[_MAX_DIR] = { 0 };
  38. TCHAR szExt[_MAX_DIR] = { 0 };
  39. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  40. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  41. _tcscpy_s(szModulePath, szDrive);
  42. _tcscat_s(szModulePath, szDir);
  43. }
  44. _stprintf_s(szlogpath, _T("%s%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]").GetString());
  45. // 打开或创建文件;
  46. CStdioFile fp;
  47. if (PathFileExists(szlogpath))
  48. {
  49. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  50. {
  51. return;
  52. }
  53. fp.SeekToEnd();
  54. }
  55. else
  56. {
  57. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  58. return;
  59. }
  60. // 格式化前设置语言区域;
  61. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  62. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  63. // 格式化日志内容;
  64. va_list args = NULL;
  65. int len = 0;
  66. TCHAR *buffer = NULL;
  67. va_start( args, format );
  68. // _vscprintf doesn't count. terminating '\0'
  69. len = _vsctprintf_p( format, args );
  70. if ( len == -1 )
  71. {
  72. goto clear;
  73. }
  74. len++;
  75. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  76. _vstprintf_s( buffer, len, format, args ); // C4996
  77. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  78. // 将日志内容输入到文件中;
  79. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  80. fp.WriteString(buffer);
  81. fp.WriteString(_T("\n"));
  82. // 关闭文件,释放资源并设置回原语言区域;
  83. free( buffer );
  84. clear:
  85. _tsetlocale(LC_CTYPE, old_locale);
  86. free(old_locale);//还原区域设定;
  87. fp.Close();
  88. }
  89. catch (CException *e)
  90. {
  91. e->ReportError();
  92. e->Delete();
  93. }
  94. }
  95. #endif
  96. // usb;
  97. static GUID UsbClassGuid = { 0xA5DCBF10L, 0x6530, 0x11D2,{ 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
  98. // mac;
  99. static GUID MacClassGuid = { 0xAD498944, 0x762F, 0x11D0, { 0x8D, 0xCB, 0x00, 0xC0, 0x4F, 0xC3, 0x35, 0x8C } };
  100. // hdd;
  101. static GUID HDDClassGuid = { 0x53F56307, 0xB6BF, 0x11D0, { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } };
  102. CDeviceQuery::CDeviceQuery(void)
  103. {
  104. }
  105. CDeviceQuery::~CDeviceQuery(void)
  106. {
  107. }
  108. //////////////////////////////////////////////////////////////////////////
  109. // 获得设备注册表中的内容;
  110. //////////////////////////////////////////////////////////////////////////
  111. BOOL CDeviceQuery::GetRegistryProperty(
  112. IN HDEVINFO DeviceInfoSet,
  113. OUT PSP_DEVINFO_DATA DeviceInfoData,
  114. IN ULONG Property,
  115. OUT PVOID Buffer,
  116. OUT PULONG Length
  117. )
  118. {
  119. while ( !SetupDiGetDeviceRegistryProperty( DeviceInfoSet,
  120. DeviceInfoData,
  121. Property,
  122. NULL,
  123. (BYTE *)*(TCHAR **)Buffer,
  124. *Length,
  125. Length))
  126. {
  127. // 长度不够则重新分配缓冲区;
  128. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  129. {
  130. if (*(LPTSTR *)Buffer)
  131. LocalFree(*(LPTSTR *)Buffer);
  132. *(LPTSTR *)Buffer = (PTCHAR)LocalAlloc(LPTR,*Length);
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. return (BOOL)(*(LPTSTR *)Buffer)[0];
  140. }
  141. void CDeviceQuery::EnumNetCards()
  142. {
  143. DWORD Status, Problem;
  144. LPTSTR Buffer = NULL;
  145. DWORD BufSize = 0;
  146. // 返回所有设备信息;
  147. HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES) ;
  148. if (INVALID_HANDLE_VALUE == hDevInfo )
  149. return;
  150. SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
  151. // 枚举设备 ;
  152. for ( DWORD DeviceId=0; SetupDiEnumDeviceInfo( hDevInfo,DeviceId,&DeviceInfoData); DeviceId++)
  153. {
  154. // 获得设备的状态 ;
  155. if (CM_Get_DevNode_Status(&Status, &Problem, DeviceInfoData.DevInst ,0) != CR_SUCCESS)
  156. continue;
  157. // 获取设备类名;
  158. TCHAR szDevName [MAX_PATH] = _T("") ;
  159. if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS , &Buffer, (PULONG)&BufSize))
  160. {
  161. lstrcpyn( szDevName, Buffer, MAX_PATH ) ;
  162. }
  163. if ( lstrcmp( szDevName, _T("Net") ) == 0 )
  164. {
  165. TCHAR szName [MAX_PATH] = _T("") ;
  166. if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize))
  167. {
  168. lstrcpyn( szName, Buffer, MAX_PATH ) ;
  169. }
  170. if ( lstrcmp( szName, _T("ROOT") ) != 0 && lstrcmp( szName, _T("SWD") ) != 0)
  171. {
  172. if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER , &Buffer, (PULONG)&BufSize))
  173. {
  174. lstrcpyn( szName, Buffer, MAX_PATH ) ;
  175. GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC , &Buffer, (PULONG)&BufSize);
  176. GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASSGUID , &Buffer, (PULONG)&BufSize);
  177. GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ADDRESS , &Buffer, (PULONG)&BufSize);
  178. GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize);
  179. GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ADDRESS/*SPDRP_DEVICEDESC*/ , &Buffer, (PULONG)&BufSize);
  180. // 获取设备描述
  181. if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ADDRESS/*SPDRP_DEVICEDESC*/ , &Buffer, (PULONG)&BufSize))
  182. {
  183. lstrcpyn( szName, Buffer, MAX_PATH ) ;
  184. // if(ControlDevice(DeviceId,hDevInfo))
  185. // {
  186. // printf("Successful\n");
  187. // }
  188. // else
  189. // {
  190. // printf("FAILED\n");
  191. // }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. SetupDiDestroyDeviceInfoList(hDevInfo);
  198. }
  199. INT CDeviceQuery::GetMacAddress()
  200. {
  201. HDEVINFO hDevInfo;
  202. DWORD MemberIndex, RequiredSize;
  203. SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
  204. PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
  205. INT nTotal = 0;
  206. INT nNICKind = 0;
  207. // 获取设备信息集;
  208. hDevInfo = SetupDiGetClassDevs(&MacClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
  209. if (hDevInfo == INVALID_HANDLE_VALUE)
  210. {
  211. return -1;
  212. }
  213. m_vtMacAddress.clear();
  214. // 枚举设备信息集中所有设备;
  215. DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  216. for (MemberIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &MacClassGuid, MemberIndex, &DeviceInterfaceData); MemberIndex++)
  217. {
  218. // 获取接收缓冲区大小,函数返回值为FALSE,GetLastError()=ERROR_INSUFFICIENT_BUFFER;
  219. SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, NULL, 0, &RequiredSize, NULL);
  220. // 申请接收缓冲区;
  221. DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  222. DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  223. // 获取设备细节信息;
  224. if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, RequiredSize, NULL, NULL))
  225. {
  226. HANDLE hDeviceFile;
  227. BOOL isOK = FALSE;
  228. if( _tcsnicmp( DeviceInterfaceDetailData->DevicePath + 4, TEXT("root"), 4 ) == 0 )
  229. {// 剔除虚拟网卡;
  230. nNICKind = NIC_ROOT;
  231. }
  232. else if ( _tcsnicmp(DeviceInterfaceDetailData->DevicePath + 4, TEXT("swd"), 3 ) == 0 )
  233. {// 剔除仿真网卡;
  234. nNICKind = NIC_SWD;
  235. }
  236. else if( _tcsnicmp( DeviceInterfaceDetailData->DevicePath + 4, TEXT("usb"), 3 ) == 0 )
  237. {// 保留USB网卡(无线网卡);
  238. nNICKind = NIC_USB;
  239. }
  240. else if (_tcsnicmp( DeviceInterfaceDetailData->DevicePath + 4, TEXT("pci"), 3 ) == 0 )
  241. {// pci网卡;
  242. nNICKind = NIC_PCI;
  243. }
  244. // 获取设备句柄;
  245. hDeviceFile = CreateFile( DeviceInterfaceDetailData->DevicePath,
  246. 0,
  247. FILE_SHARE_READ | FILE_SHARE_WRITE,
  248. NULL,
  249. OPEN_EXISTING,
  250. 0,
  251. NULL);
  252. if( hDeviceFile != INVALID_HANDLE_VALUE )
  253. {
  254. ULONG dwID;
  255. BYTE ucData[8];
  256. DWORD dwByteRet;
  257. // 获取原生MAC地址;
  258. dwID = OID_802_3_PERMANENT_ADDRESS;
  259. isOK = DeviceIoControl( hDeviceFile, IOCTL_NDIS_QUERY_GLOBAL_STATS, &dwID, sizeof(dwID), ucData, sizeof(ucData), &dwByteRet, NULL );
  260. if( isOK )
  261. {
  262. ++nTotal;
  263. MacAddress tagMacAddress;
  264. _stprintf_s(tagMacAddress.szDevicePath, _T("%s"), DeviceInterfaceDetailData->DevicePath);
  265. memset(tagMacAddress.szMacAddress, 0, sizeof(TCHAR)*MAX_PATH);
  266. // 将字节数组转换成16进制字符串;
  267. for ( DWORD i = 0; i < dwByteRet; i++ )
  268. {
  269. _stprintf_s( &tagMacAddress.szMacAddress[i << 1], MAX_PATH - (i << 1), _T("%02X"), ucData[i] );
  270. }
  271. switch(nNICKind)
  272. {
  273. case NIC_PCI:
  274. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_PCI"));
  275. break;
  276. case NIC_USB:
  277. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_USB"));
  278. break;
  279. case NIC_ROOT:
  280. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_ROOT"));
  281. break;
  282. case NIC_SWD:
  283. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_SWD"));
  284. break;
  285. default:
  286. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_UNK"));
  287. break;
  288. }
  289. tagMacAddress.nNICKind = nNICKind;
  290. m_vtMacAddress.push_back(tagMacAddress);
  291. #ifdef _DEBUG
  292. WriteTextLog(_T("网卡MAC地址:%s"),tagMacAddress.szMacAddress);
  293. WriteTextLog(DeviceInterfaceDetailData->DevicePath);
  294. #endif
  295. }
  296. CloseHandle( hDeviceFile );
  297. }
  298. }
  299. free(DeviceInterfaceDetailData);
  300. }
  301. SetupDiDestroyDeviceInfoList(hDevInfo);
  302. return nTotal;
  303. }
  304. INT CDeviceQuery::GetHardDiskInfo()
  305. {
  306. HDEVINFO hDevInfo;
  307. DWORD MemberIndex, RequiredSize;
  308. SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
  309. PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
  310. INT nTotal = 0;
  311. // 获取设备信息集;
  312. hDevInfo = SetupDiGetClassDevs(&HDDClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
  313. if (hDevInfo == INVALID_HANDLE_VALUE)
  314. {
  315. return -1;
  316. }
  317. m_vtHardDiskID.clear();
  318. // 枚举设备信息集中所有设备;
  319. DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  320. for (MemberIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &HDDClassGuid, MemberIndex, &DeviceInterfaceData); MemberIndex++)
  321. {
  322. // 获取接收缓冲区大小,函数返回值为FALSE,GetLastError()=ERROR_INSUFFICIENT_BUFFER;
  323. SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, NULL, 0, &RequiredSize, NULL);
  324. // 申请接收缓冲区;
  325. DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  326. DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  327. // 获取设备细节信息;
  328. if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, RequiredSize, NULL, NULL))
  329. {
  330. HANDLE hDeviceFile;
  331. BOOL isOK = FALSE;
  332. // 获取设备句柄;
  333. hDeviceFile = CreateFile( DeviceInterfaceDetailData->DevicePath,
  334. GENERIC_READ | GENERIC_WRITE, // 注意:需要系统管理员权限才能执行读写操作;
  335. FILE_SHARE_READ | FILE_SHARE_WRITE,
  336. NULL,
  337. OPEN_EXISTING,
  338. 0,
  339. NULL);
  340. if( hDeviceFile != INVALID_HANDLE_VALUE )
  341. {
  342. GETVERSIONINPARAMS VersionInParams;
  343. DWORD dwByteRet;
  344. // 获取硬盘属性;
  345. isOK = DeviceIoControl( hDeviceFile, SMART_GET_VERSION, NULL, 0, &VersionInParams, sizeof(GETVERSIONINPARAMS), &dwByteRet, NULL );
  346. if( isOK )
  347. {
  348. isOK = FALSE;
  349. if( VersionInParams.fCapabilities & CAP_ATA_ID_CMD )
  350. {
  351. BYTE abSCIP[sizeof(SENDCMDINPARAMS) - 1] = {0};
  352. BYTE abSCOP[sizeof(SENDCMDOUTPARAMS) - 1 + 512] = {0};
  353. SENDCMDINPARAMS *scip = reinterpret_cast<SENDCMDINPARAMS*>(abSCIP);
  354. SENDCMDOUTPARAMS *scop = reinterpret_cast<SENDCMDOUTPARAMS*>(abSCOP);
  355. // 设置输入参数;
  356. scip->irDriveRegs.bCommandReg = ID_CMD;
  357. // 设置输出参数;
  358. scop->cBufferSize = 512;
  359. // 获取硬盘详细信息;
  360. isOK = DeviceIoControl( hDeviceFile, SMART_RCV_DRIVE_DATA, scip, sizeof(abSCIP), scop, sizeof(abSCOP), &dwByteRet, NULL );
  361. if( isOK )
  362. {
  363. ++nTotal;
  364. std::string strHDSerialNumber;
  365. strHDSerialNumber.resize(32,'\0');
  366. // 提取硬盘序列号;
  367. for( UINT i = 0, j = 0; i < 20; i += 2 )
  368. { // 颠倒高低位字节;
  369. if ( scop->bBuffer[i+20] != 32)
  370. {
  371. if (scop->bBuffer[i+21] != 32)
  372. strHDSerialNumber[j++] = scop->bBuffer[i+21];
  373. strHDSerialNumber[j++] = scop->bBuffer[i+20];
  374. }
  375. }
  376. m_vtHardDiskID.push_back(strHDSerialNumber);
  377. #ifdef _DEBUG
  378. WriteTextLog(_T("硬盘序列号:%s"), strHDSerialNumber.c_str());
  379. #endif
  380. }
  381. }
  382. }
  383. CloseHandle( hDeviceFile );
  384. }
  385. }
  386. free(DeviceInterfaceDetailData);
  387. }
  388. SetupDiDestroyDeviceInfoList(hDevInfo);
  389. return nTotal;
  390. }
  391. void CDeviceQuery::OutHDString()
  392. {
  393. for ( std::vector<MacAddress>::iterator it = m_vtMacAddress.begin(); it != m_vtMacAddress.end(); it++ )
  394. {
  395. std::string strNICKind;
  396. switch(it->nNICKind)
  397. {
  398. case NIC_PCI:
  399. strNICKind = "PCI网卡";
  400. break;
  401. case NIC_USB:
  402. strNICKind = "USB无线网卡";
  403. break;
  404. case NIC_SWD:
  405. strNICKind = "仿真网卡";
  406. break;
  407. case NIC_ROOT:
  408. strNICKind = "虚拟网卡";
  409. break;
  410. default:
  411. break;
  412. }
  413. WriteTextLog(_T("类型:%s, 物理地址:%s"), strNICKind.c_str(), it->szMacAddress);
  414. }
  415. for ( std::vector<std::string>::iterator it = m_vtHardDiskID.begin(); it != m_vtHardDiskID.end(); it++ )
  416. {
  417. WriteTextLog(_T("硬盘序列号:%s"), it->c_str());
  418. }
  419. }
  420. BYTE* CDeviceQuery::GetHardWareSerialNumber(IN DWORD &dwBytelen)
  421. {
  422. char *out = NULL;
  423. cJSON *pJson = NULL;
  424. pJson = cJSON_CreateObject();
  425. // 存在网卡时;
  426. if ( m_vtMacAddress.size() )
  427. {
  428. INT nNIC_PCI = 0;
  429. INT nNIC_USB = 0;
  430. std::vector<MacAddress> vtMacAddress;
  431. for ( std::vector<MacAddress>::iterator it = m_vtMacAddress.begin(); it != m_vtMacAddress.end(); it++ )
  432. {
  433. if ( it->nNICKind == NIC_ROOT || it->nNICKind == NIC_SWD)
  434. continue;
  435. if ( it->nNICKind == NIC_PCI )
  436. ++nNIC_PCI;
  437. else if (it->nNICKind == NIC_USB)
  438. ++nNIC_USB;
  439. vtMacAddress.push_back(*it);
  440. }
  441. // 只获取pic和usb网卡;
  442. if ( vtMacAddress.size() )
  443. {
  444. cJSON *pMacArry = cJSON_CreateArray();
  445. cJSON_AddItemToObject(pJson, "MAC", pMacArry);
  446. std::vector<MacAddress>::iterator it;
  447. for ( it = vtMacAddress.begin(); it != vtMacAddress.end(); it++)
  448. {
  449. cJSON *pMacItem = cJSON_CreateObject();
  450. cJSON_AddStringToObject(pMacItem, "Address", it->szMacAddress);
  451. cJSON_AddStringToObject(pMacItem, "kind", it->szNICKind);
  452. cJSON_AddItemToArray(pMacArry, pMacItem);
  453. }
  454. }
  455. }
  456. // 存在硬盘时;
  457. if ( m_vtHardDiskID.size() )
  458. {
  459. cJSON *pHDArry = cJSON_CreateArray();
  460. cJSON_AddItemToObject(pJson, "HD", pHDArry);
  461. for ( std::vector<std::string>::iterator it = m_vtHardDiskID.begin(); it != m_vtHardDiskID.end(); it++)
  462. {
  463. cJSON *pHDItem = cJSON_CreateObject();
  464. cJSON_AddStringToObject(pHDItem, "SerialNum", it->c_str());
  465. cJSON_AddItemToArray(pHDArry, pHDItem);
  466. }
  467. }
  468. out = cJSON_Print(pJson);
  469. #ifdef _DEBUG
  470. WriteTextLog(_T("JSON:%s"), out);
  471. #endif
  472. cJSON_Delete(pJson);
  473. #if 0 // 以下是加密;
  474. INT nSrclen = strlen(out);
  475. INT nEncryptlen = des_enc_len(nSrclen);
  476. INT nBase64len = CBase64::CalcBase64Len(nEncryptlen);
  477. dwBytelen = nBase64len;
  478. BYTE *pEncryptStr = new BYTE[nBase64len+1];
  479. memset(pEncryptStr, 0, nBase64len+1);
  480. des_crypt_cbc(DES_ENCRYPT,nSrclen, (LPBYTE)out, pEncryptStr, nBase64len);
  481. if(0) // 还原测试-ok;
  482. {
  483. nSrclen = strlen((char*)pEncryptStr);
  484. nEncryptlen = des_dec_len(pEncryptStr, nSrclen);
  485. nBase64len = CBase64::CalcBinLen(nEncryptlen);
  486. BYTE *pDecryptStr = new BYTE[nBase64len+1];
  487. memset(pDecryptStr, 0, nBase64len + 1);
  488. des_crypt_cbc(DES_DECRYPT, nSrclen, pEncryptStr, pDecryptStr, nBase64len);
  489. if (pDecryptStr)
  490. {
  491. delete pDecryptStr;
  492. }
  493. if ( pEncryptStr )
  494. {
  495. delete pEncryptStr;
  496. }
  497. }
  498. if ( out )
  499. {
  500. delete out;
  501. }
  502. return pEncryptStr;
  503. #else
  504. return (BYTE*)out;
  505. #endif
  506. }