lyfzHDSerial.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // lyfzHDSerial.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "lyfzHDSerial.h"
  5. #include "yxyDES2.h"
  6. #include <iphlpapi.h>
  7. #pragma comment(lib,"iphlpapi.lib")
  8. #include <comdef.h>
  9. #include <wbemidl.h>
  10. #include <Wbemcli.h>
  11. #pragma comment(lib,"Wbemuuid.lib")
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. char bitsCiphertextAnyLength[32768] = {0};
  18. char hexCiphertextAnyLength[16384] = {0};
  19. yxyDES2 g_DES2;
  20. void ConvertCiphertext2OtherFormat(int iBitsLen,char *szCipherInBytes);
  21. int ConvertOtherFormat2Ciphertext(char *szCipher, char* pCiphertext);
  22. //
  23. //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
  24. // 则从此 DLL 导出的任何调入
  25. // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
  26. // 该函数的最前面。
  27. //
  28. // 例如:
  29. //
  30. // extern "C" BOOL PASCAL EXPORT ExportedFunction()
  31. // {
  32. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  33. // // 此处为普通函数体
  34. // }
  35. //
  36. // 此宏先于任何 MFC 调用
  37. // 出现在每个函数中十分重要。这意味着
  38. // 它必须作为函数中的第一个语句
  39. // 出现,甚至先于所有对象变量声明,
  40. // 这是因为它们的构造函数可能生成 MFC
  41. // DLL 调用。
  42. //
  43. // 有关其他详细信息,
  44. // 请参阅 MFC 技术说明 33 和 58。
  45. //
  46. // ClyfzHDSerialApp
  47. BEGIN_MESSAGE_MAP(ClyfzHDSerialApp, CWinApp)
  48. END_MESSAGE_MAP()
  49. // ClyfzHDSerialApp 构造
  50. ClyfzHDSerialApp::ClyfzHDSerialApp()
  51. {
  52. // TODO: 在此处添加构造代码,
  53. // 将所有重要的初始化放置在 InitInstance 中
  54. }
  55. ClyfzHDSerialApp::~ClyfzHDSerialApp()
  56. {
  57. }
  58. // 唯一的一个 ClyfzHDSerialApp 对象
  59. ClyfzHDSerialApp theApp;
  60. // ClyfzHDSerialApp 初始化
  61. BOOL ClyfzHDSerialApp::InitInstance()
  62. {
  63. CWinApp::InitInstance();
  64. return TRUE;
  65. }
  66. int ClyfzHDSerialApp::ExitInstance()
  67. {
  68. return CWinApp::ExitInstance();
  69. }
  70. void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str);
  71. /*
  72. GetSeiralNumberOrID("Win32_BaseBoard", "SerialNumber"); // 主板
  73. GetSeiralNumberOrID("Win32_Processor", "ProcessorID"); // CPU
  74. GetSeiralNumberOrID("Win32_PhysicalMemory", "SerialNumber"); // 内存
  75. GetSeiralNumberOrID("Win32_DiskDrive", "PNPDeviceID"); // 硬盘
  76. */
  77. /************************************************************************/
  78. /*
  79. 函数: GetSeiralNumberOrID
  80. 描述: 获取序列号或ID 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息
  81. 参数:
  82. LPCTSTR lpszClass,
  83. LPCTSTR lpszName
  84. LPSTR str 返回序列号或ID
  85. 返回:
  86. 注意: 1成功, 0失败;
  87. */
  88. /************************************************************************/
  89. extern "C" INT PASCAL EXPORT GetSeiralNumberOrID(IN LPWSTR lpszClass, IN LPWSTR lpszName, OUT LPWSTR str)
  90. {
  91. /*
  92. OutputDebugString(lpszClass);
  93. OutputDebugString(_T("\n"));
  94. OutputDebugString(lpszName);
  95. OutputDebugString(_T("\n"));
  96. */
  97. if(str == NULL)
  98. return 0;
  99. // 初始化COM模块
  100. if (CoInitializeEx(0, COINIT_MULTITHREADED) != S_OK)
  101. return 0;
  102. // 获取访问 WMI 权限
  103. if( CoInitializeSecurity(
  104. NULL,
  105. -1,
  106. NULL,
  107. NULL,
  108. RPC_C_AUTHN_LEVEL_DEFAULT,
  109. RPC_C_IMP_LEVEL_IMPERSONATE,NULL,
  110. EOAC_NONE,
  111. 0 ) != S_OK )
  112. {
  113. AfxMessageBox(_T("访问权限失败"));
  114. return 0;
  115. }
  116. // 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息
  117. CComPtr<IWbemLocator> spWbemLocator;
  118. if( spWbemLocator.CoCreateInstance(
  119. CLSID_WbemAdministrativeLocator,
  120. 0 ,
  121. CLSCTX_INPROC_SERVER| CLSCTX_LOCAL_SERVER
  122. ) != S_OK )
  123. {
  124. AfxMessageBox(_T(" 获取系统信息失败"));
  125. return 0;
  126. }
  127. //通过iwbemlocator连接到WMI::ConnectServer方法
  128. CComPtr<IWbemServices> spWbemServices;
  129. if( spWbemLocator->ConnectServer(
  130. _T("root\\cimv2"),
  131. NULL,
  132. NULL,
  133. NULL,
  134. 0,
  135. NULL,
  136. NULL,
  137. &spWbemServices) != S_OK )
  138. {
  139. spWbemLocator.Release();
  140. AfxMessageBox(_T(" 获取系统信息失败"));
  141. return 0;
  142. }
  143. //在代理上设置安全级别
  144. HRESULT hres = CoSetProxyBlanket(
  145. spWbemServices, // Indicates the proxy to set
  146. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  147. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  148. NULL, // Server principal name
  149. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  150. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  151. NULL, // client identity
  152. EOAC_NONE // proxy capabilities
  153. );
  154. if(FAILED(hres))
  155. {
  156. spWbemServices.Release();
  157. spWbemLocator.Release();
  158. AfxMessageBox(_T(" 获取系统信息失败"));
  159. return 0;
  160. }
  161. // 检索指定信息
  162. USES_CONVERSION ;
  163. CComPtr<IEnumWbemClassObject> spEnumWbemClassObject;
  164. CComBSTR bstrQuery ( _T("Select * from "));
  165. bstrQuery += T2OLE((LPTSTR)lpszClass) ;
  166. //使用iwbemservices指针请求WMI
  167. if( spWbemServices->ExecQuery( _T("WQL"), bstrQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &spEnumWbemClassObject) != S_OK )
  168. {
  169. spWbemServices.Release();
  170. spWbemLocator.Release();
  171. AfxMessageBox(_T(" 获取系统信息失败"));
  172. return 0;
  173. }
  174. CComPtr<IWbemClassObject> spClassObject;
  175. ULONG uCount = 1, uReturned;
  176. if( spEnumWbemClassObject->Reset() != S_OK)
  177. {
  178. spEnumWbemClassObject.Release();
  179. spWbemServices.Release();
  180. spWbemLocator.Release();
  181. AfxMessageBox(_T(" 获取系统信息失败"));
  182. return 0;
  183. }
  184. int iEnumIdx = 0;
  185. while( spEnumWbemClassObject->Next(WBEM_INFINITE,uCount, &spClassObject, &uReturned) == S_OK )
  186. {
  187. CString strtmp = _T("");
  188. _getWmiInfo( spClassObject, lpszName, strtmp);
  189. memcpy(str,strtmp.GetString(), strtmp.GetLength()*2);
  190. spClassObject.Release() ;
  191. }
  192. spEnumWbemClassObject.Release();
  193. spWbemServices.Release();
  194. spWbemLocator.Release();
  195. CoUninitialize();
  196. return 1;
  197. }
  198. void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str)
  199. {
  200. CComVariant varValue;
  201. CComBSTR bstrName(lpszName);
  202. if( pClassObject->Get( bstrName , 0 , &varValue , NULL , 0 ) == S_OK )
  203. {
  204. // 对不同的数据类型分别处理
  205. if( varValue.vt == VT_BSTR )//字符串
  206. {
  207. str = CString(varValue.bstrVal) ;
  208. }
  209. else if( varValue.vt == VT_ARRAY ) //数组
  210. {
  211. long iLowBound = 0 , iUpBound = 0 ;
  212. SafeArrayGetLBound( varValue.parray , 1 , &iLowBound ) ;
  213. SafeArrayGetUBound( varValue.parray , 1 , &iUpBound ) ;
  214. for( long j = iLowBound ; j <= iUpBound ; j ++ )
  215. {
  216. VARIANT *pvar = NULL ;
  217. long temp = j ;
  218. if( SafeArrayGetElement( varValue.parray , &temp , pvar ) == S_OK && pvar )
  219. {
  220. CComVariant varTemp ;
  221. if( varTemp.ChangeType( VT_BSTR , pvar ) == S_OK )
  222. {
  223. if( !str.IsEmpty() )
  224. str += _T(",") ;
  225. str += varTemp.bstrVal ;
  226. } //if( varTemp...
  227. } //if( SafeArrayGet...
  228. } //for( long j=iLowBound;...
  229. } //if (varValue.vt ...
  230. else
  231. {
  232. switch( varValue.vt)
  233. {
  234. case VT_I4:
  235. str.Format(_T("%d"), varValue.lVal);
  236. break;
  237. case VT_I8:
  238. str.Format(_T("%l"), varValue.llVal);
  239. break;
  240. case VT_I2:
  241. str.Format(_T("%d"), varValue.iVal);
  242. break;
  243. case VT_UI1:
  244. str.Format(_T("%uc"), varValue.bVal);
  245. break;
  246. case VT_R4:
  247. str.Format(_T("%f"), varValue.fltVal);
  248. break;
  249. case VT_R8:
  250. str.Format(_T("%lf"), varValue.dblVal);
  251. break;
  252. case VT_I1:
  253. str.Format(_T("%c"), varValue.cVal);
  254. break;
  255. case VT_UI2:
  256. str.Format(_T("%ud"), varValue.uiVal);
  257. break;
  258. case VT_UI4:
  259. str.Format(_T("%ud"), varValue.ulVal);
  260. break;
  261. case VT_UI8:
  262. str.Format(_T("%ul"), varValue.ullVal);
  263. break;
  264. case VT_BOOL:
  265. break;
  266. case VT_NULL:
  267. str = _T("NULL");
  268. break;
  269. default:
  270. if( varValue.ChangeType( VT_BSTR ) == S_OK )
  271. str = varValue.bstrVal ;
  272. }
  273. }
  274. // 打印出硬件信息
  275. //printf("%s = %s\n", bstrName, str);
  276. }
  277. else
  278. str = _T("");
  279. }
  280. /************************************************************************/
  281. /*
  282. 函数: GetLocalMacAddr
  283. 描述: 获取本地mac地址
  284. 参数:
  285. BYTE* pMac 返回mac地址
  286. 返回:
  287. 注意: 0成功, -1失败;
  288. */
  289. /************************************************************************/
  290. extern "C" VOID PASCAL EXPORT GetLocalMacAddr(OUT LPSTR pMac)
  291. {
  292. if(pMac == NULL)
  293. return;
  294. //获取物理地址
  295. IP_ADAPTER_INFO adapter[5]; //Maximum 5 adapters
  296. memset(adapter, 0, sizeof(IP_ADAPTER_INFO)*5);
  297. DWORD buflen = sizeof(adapter);
  298. DWORD status = GetAdaptersInfo(adapter,&buflen);
  299. if(status != ERROR_SUCCESS)
  300. return;
  301. BYTE s[8] = {0};
  302. PIP_ADAPTER_INFO painfo = adapter;
  303. memcpy(s, painfo->Address, 6);
  304. sprintf(pMac, "%02x-%02x-%02x-%02x-%02x-%02x", s[0], s[1], s[2], s[3], s[4], s[5]);
  305. }
  306. /************************************************************************/
  307. /*
  308. 函数: GetLocalMacAddr
  309. 描述: DES加密
  310. 参数:
  311. LPCSTR pPlaintext, 加密的内容
  312. LPCSTR pKey 密钥
  313. LPSTR pCiphertext 返回加密后的密文
  314. 返回:
  315. 注意:
  316. */
  317. /************************************************************************/
  318. extern "C" VOID PASCAL EXPORT DESEncrypt(IN LPCSTR pPlaintext, IN LPCSTR pKey, OUT LPSTR pCiphertext)
  319. {
  320. if(pCiphertext == NULL || pPlaintext == NULL || pKey == NULL)
  321. return;
  322. g_DES2.InitializeKey((char*)pKey, 0);
  323. g_DES2.EncryptAnyLength((char*)pPlaintext, strlen(pPlaintext), 0);
  324. char* ptext = g_DES2.GetCiphertextAnyLength();
  325. ConvertCiphertext2OtherFormat(strlen(pPlaintext) % 8 == 0 ? strlen(pPlaintext) << 3 : ((strlen(pPlaintext)>>3) + 1) << 6, ptext);
  326. if(ptext == NULL)
  327. return;
  328. memcpy(pCiphertext, hexCiphertextAnyLength, strlen(hexCiphertextAnyLength));
  329. }
  330. /************************************************************************/
  331. /*
  332. 函数: DESDecrypt
  333. 描述: DES解密
  334. 参数:
  335. LPCSTR pCiphertext, 要解密的内容
  336. LPCSTR pKey 密钥
  337. LPSTR pPlaintext 返回解密后的明文
  338. 返回:
  339. 注意:
  340. */
  341. /************************************************************************/
  342. extern "C" VOID PASCAL EXPORT DESDecrypt(IN LPCSTR pCiphertext, IN LPCSTR pKey, OUT LPSTR pPlaintext)
  343. {
  344. if(pCiphertext == NULL || pKey == NULL || pPlaintext == NULL)
  345. return;
  346. char szCiphertextData[8192] = {0};
  347. g_DES2.InitializeKey((char*)pKey, 0);
  348. int nLen = ConvertOtherFormat2Ciphertext((char*)pCiphertext, szCiphertextData);
  349. g_DES2.DecryptAnyLength(szCiphertextData, nLen, 0);
  350. char* ptext = g_DES2.GetPlaintextAnyLength();
  351. if(ptext == NULL)
  352. return;
  353. memcpy(pPlaintext, ptext, strlen(ptext));
  354. }
  355. void ConvertCiphertext2OtherFormat(int iBitsLen, char* szCipherInBytes)
  356. {
  357. memset(hexCiphertextAnyLength,0,16384);
  358. memset(bitsCiphertextAnyLength,0,32768);
  359. g_DES2.Bytes2Bits(szCipherInBytes, bitsCiphertextAnyLength, iBitsLen);
  360. g_DES2.Bits2Hex(hexCiphertextAnyLength, bitsCiphertextAnyLength, iBitsLen);
  361. for(int i=0; i<iBitsLen; i++)
  362. bitsCiphertextAnyLength[i] += 48;
  363. }
  364. int ConvertOtherFormat2Ciphertext(char* szCipher, char* pCiphertext)
  365. {
  366. if(pCiphertext == NULL)
  367. return 0;
  368. int iLen = 0;
  369. memset(bitsCiphertextAnyLength,0,32768);
  370. //十六进制
  371. iLen = ((strlen(szCipher)>>2) + (strlen(szCipher) % 4 == 0 ? 0 : 1))<<4;
  372. memcpy(hexCiphertextAnyLength, szCipher, strlen(szCipher));
  373. g_DES2.Hex2Bits(hexCiphertextAnyLength, bitsCiphertextAnyLength, iLen);
  374. g_DES2.Bits2Bytes(pCiphertext, bitsCiphertextAnyLength, iLen);
  375. //二进制
  376. /*
  377. iLen = strlen(szCipher);
  378. memset(bitsCiphertextAnyLength,0,32768);
  379. memcpy(bitsCiphertextAnyLength,szCipher,iLen);
  380. for(long i=0;i< iLen;i++)
  381. bitsCiphertextAnyLength[i]-= 48;
  382. g_DES2.Bits2Bytes(szCiphertextData,bitsCiphertextAnyLength,iLen);
  383. */
  384. return iLen>>3;
  385. }