// lyfzHDSerial.cpp : 定义 DLL 的初始化例程。 // #include "stdafx.h" #include "lyfzHDSerial.h" #include "yxyDES2.h" #include #pragma comment(lib,"iphlpapi.lib") #include #include #include #pragma comment(lib,"Wbemuuid.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif char bitsCiphertextAnyLength[32768] = {0}; char hexCiphertextAnyLength[16384] = {0}; yxyDES2 g_DES2; void ConvertCiphertext2OtherFormat(int iBitsLen,char *szCipherInBytes); int ConvertOtherFormat2Ciphertext(char *szCipher, char* pCiphertext); // //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的, // 则从此 DLL 导出的任何调入 // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到 // 该函数的最前面。 // // 例如: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // 此处为普通函数体 // } // // 此宏先于任何 MFC 调用 // 出现在每个函数中十分重要。这意味着 // 它必须作为函数中的第一个语句 // 出现,甚至先于所有对象变量声明, // 这是因为它们的构造函数可能生成 MFC // DLL 调用。 // // 有关其他详细信息, // 请参阅 MFC 技术说明 33 和 58。 // // ClyfzHDSerialApp BEGIN_MESSAGE_MAP(ClyfzHDSerialApp, CWinApp) END_MESSAGE_MAP() // ClyfzHDSerialApp 构造 ClyfzHDSerialApp::ClyfzHDSerialApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } ClyfzHDSerialApp::~ClyfzHDSerialApp() { } // 唯一的一个 ClyfzHDSerialApp 对象 ClyfzHDSerialApp theApp; // ClyfzHDSerialApp 初始化 BOOL ClyfzHDSerialApp::InitInstance() { CWinApp::InitInstance(); return TRUE; } int ClyfzHDSerialApp::ExitInstance() { return CWinApp::ExitInstance(); } void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str); /* GetSeiralNumberOrID("Win32_BaseBoard", "SerialNumber"); // 主板 GetSeiralNumberOrID("Win32_Processor", "ProcessorID"); // CPU GetSeiralNumberOrID("Win32_PhysicalMemory", "SerialNumber"); // 内存 GetSeiralNumberOrID("Win32_DiskDrive", "PNPDeviceID"); // 硬盘 */ /************************************************************************/ /* 函数: GetSeiralNumberOrID 描述: 获取序列号或ID 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息 参数: LPCTSTR lpszClass, LPCTSTR lpszName LPSTR str 返回序列号或ID 返回: 注意: 1成功, 0失败; */ /************************************************************************/ extern "C" INT PASCAL EXPORT GetSeiralNumberOrID(IN LPWSTR lpszClass, IN LPWSTR lpszName, OUT LPWSTR str) { /* OutputDebugString(lpszClass); OutputDebugString(_T("\n")); OutputDebugString(lpszName); OutputDebugString(_T("\n")); */ if(str == NULL) return 0; // 初始化COM模块 if (CoInitializeEx(0, COINIT_MULTITHREADED) != S_OK) return 0; // 获取访问 WMI 权限 if( CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,NULL, EOAC_NONE, 0 ) != S_OK ) { AfxMessageBox(_T("访问权限失败")); return 0; } // 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息 CComPtr spWbemLocator; if( spWbemLocator.CoCreateInstance( CLSID_WbemAdministrativeLocator, 0 , CLSCTX_INPROC_SERVER| CLSCTX_LOCAL_SERVER ) != S_OK ) { AfxMessageBox(_T(" 获取系统信息失败")); return 0; } //通过iwbemlocator连接到WMI::ConnectServer方法 CComPtr spWbemServices; if( spWbemLocator->ConnectServer( _T("root\\cimv2"), NULL, NULL, NULL, 0, NULL, NULL, &spWbemServices) != S_OK ) { spWbemLocator.Release(); AfxMessageBox(_T(" 获取系统信息失败")); return 0; } //在代理上设置安全级别 HRESULT hres = CoSetProxyBlanket( spWbemServices, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if(FAILED(hres)) { spWbemServices.Release(); spWbemLocator.Release(); AfxMessageBox(_T(" 获取系统信息失败")); return 0; } // 检索指定信息 USES_CONVERSION ; CComPtr spEnumWbemClassObject; CComBSTR bstrQuery ( _T("Select * from ")); bstrQuery += T2OLE((LPTSTR)lpszClass) ; //使用iwbemservices指针请求WMI if( spWbemServices->ExecQuery( _T("WQL"), bstrQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &spEnumWbemClassObject) != S_OK ) { spWbemServices.Release(); spWbemLocator.Release(); AfxMessageBox(_T(" 获取系统信息失败")); return 0; } CComPtr spClassObject; ULONG uCount = 1, uReturned; if( spEnumWbemClassObject->Reset() != S_OK) { spEnumWbemClassObject.Release(); spWbemServices.Release(); spWbemLocator.Release(); AfxMessageBox(_T(" 获取系统信息失败")); return 0; } int iEnumIdx = 0; while( spEnumWbemClassObject->Next(WBEM_INFINITE,uCount, &spClassObject, &uReturned) == S_OK ) { CString strtmp = _T(""); _getWmiInfo( spClassObject, lpszName, strtmp); memcpy(str,strtmp.GetString(), strtmp.GetLength()*2); spClassObject.Release() ; } spEnumWbemClassObject.Release(); spWbemServices.Release(); spWbemLocator.Release(); CoUninitialize(); return 1; } void _getWmiInfo(IWbemClassObject *pClassObject, LPCTSTR lpszName, CString& str) { CComVariant varValue; CComBSTR bstrName(lpszName); if( pClassObject->Get( bstrName , 0 , &varValue , NULL , 0 ) == S_OK ) { // 对不同的数据类型分别处理 if( varValue.vt == VT_BSTR )//字符串 { str = CString(varValue.bstrVal) ; } else if( varValue.vt == VT_ARRAY ) //数组 { long iLowBound = 0 , iUpBound = 0 ; SafeArrayGetLBound( varValue.parray , 1 , &iLowBound ) ; SafeArrayGetUBound( varValue.parray , 1 , &iUpBound ) ; for( long j = iLowBound ; j <= iUpBound ; j ++ ) { VARIANT *pvar = NULL ; long temp = j ; if( SafeArrayGetElement( varValue.parray , &temp , pvar ) == S_OK && pvar ) { CComVariant varTemp ; if( varTemp.ChangeType( VT_BSTR , pvar ) == S_OK ) { if( !str.IsEmpty() ) str += _T(",") ; str += varTemp.bstrVal ; } //if( varTemp... } //if( SafeArrayGet... } //for( long j=iLowBound;... } //if (varValue.vt ... else { switch( varValue.vt) { case VT_I4: str.Format(_T("%d"), varValue.lVal); break; case VT_I8: str.Format(_T("%l"), varValue.llVal); break; case VT_I2: str.Format(_T("%d"), varValue.iVal); break; case VT_UI1: str.Format(_T("%uc"), varValue.bVal); break; case VT_R4: str.Format(_T("%f"), varValue.fltVal); break; case VT_R8: str.Format(_T("%lf"), varValue.dblVal); break; case VT_I1: str.Format(_T("%c"), varValue.cVal); break; case VT_UI2: str.Format(_T("%ud"), varValue.uiVal); break; case VT_UI4: str.Format(_T("%ud"), varValue.ulVal); break; case VT_UI8: str.Format(_T("%ul"), varValue.ullVal); break; case VT_BOOL: break; case VT_NULL: str = _T("NULL"); break; default: if( varValue.ChangeType( VT_BSTR ) == S_OK ) str = varValue.bstrVal ; } } // 打印出硬件信息 //printf("%s = %s\n", bstrName, str); } else str = _T(""); } /************************************************************************/ /* 函数: GetLocalMacAddr 描述: 获取本地mac地址 参数: BYTE* pMac 返回mac地址 返回: 注意: 0成功, -1失败; */ /************************************************************************/ extern "C" VOID PASCAL EXPORT GetLocalMacAddr(OUT LPSTR pMac) { if(pMac == NULL) return; //获取物理地址 IP_ADAPTER_INFO adapter[5]; //Maximum 5 adapters memset(adapter, 0, sizeof(IP_ADAPTER_INFO)*5); DWORD buflen = sizeof(adapter); DWORD status = GetAdaptersInfo(adapter,&buflen); if(status != ERROR_SUCCESS) return; BYTE s[8] = {0}; PIP_ADAPTER_INFO painfo = adapter; memcpy(s, painfo->Address, 6); sprintf(pMac, "%02x-%02x-%02x-%02x-%02x-%02x", s[0], s[1], s[2], s[3], s[4], s[5]); } /************************************************************************/ /* 函数: GetLocalMacAddr 描述: DES加密 参数: LPCSTR pPlaintext, 加密的内容 LPCSTR pKey 密钥 LPSTR pCiphertext 返回加密后的密文 返回: 注意: */ /************************************************************************/ extern "C" VOID PASCAL EXPORT DESEncrypt(IN LPCSTR pPlaintext, IN LPCSTR pKey, OUT LPSTR pCiphertext) { if(pCiphertext == NULL || pPlaintext == NULL || pKey == NULL) return; g_DES2.InitializeKey((char*)pKey, 0); g_DES2.EncryptAnyLength((char*)pPlaintext, strlen(pPlaintext), 0); char* ptext = g_DES2.GetCiphertextAnyLength(); ConvertCiphertext2OtherFormat(strlen(pPlaintext) % 8 == 0 ? strlen(pPlaintext) << 3 : ((strlen(pPlaintext)>>3) + 1) << 6, ptext); if(ptext == NULL) return; memcpy(pCiphertext, hexCiphertextAnyLength, strlen(hexCiphertextAnyLength)); } /************************************************************************/ /* 函数: DESDecrypt 描述: DES解密 参数: LPCSTR pCiphertext, 要解密的内容 LPCSTR pKey 密钥 LPSTR pPlaintext 返回解密后的明文 返回: 注意: */ /************************************************************************/ extern "C" VOID PASCAL EXPORT DESDecrypt(IN LPCSTR pCiphertext, IN LPCSTR pKey, OUT LPSTR pPlaintext) { if(pCiphertext == NULL || pKey == NULL || pPlaintext == NULL) return; char szCiphertextData[8192] = {0}; g_DES2.InitializeKey((char*)pKey, 0); int nLen = ConvertOtherFormat2Ciphertext((char*)pCiphertext, szCiphertextData); g_DES2.DecryptAnyLength(szCiphertextData, nLen, 0); char* ptext = g_DES2.GetPlaintextAnyLength(); if(ptext == NULL) return; memcpy(pPlaintext, ptext, strlen(ptext)); } void ConvertCiphertext2OtherFormat(int iBitsLen, char* szCipherInBytes) { memset(hexCiphertextAnyLength,0,16384); memset(bitsCiphertextAnyLength,0,32768); g_DES2.Bytes2Bits(szCipherInBytes, bitsCiphertextAnyLength, iBitsLen); g_DES2.Bits2Hex(hexCiphertextAnyLength, bitsCiphertextAnyLength, iBitsLen); for(int i=0; i>2) + (strlen(szCipher) % 4 == 0 ? 0 : 1))<<4; memcpy(hexCiphertextAnyLength, szCipher, strlen(szCipher)); g_DES2.Hex2Bits(hexCiphertextAnyLength, bitsCiphertextAnyLength, iLen); g_DES2.Bits2Bytes(pCiphertext, bitsCiphertextAnyLength, iLen); //二进制 /* iLen = strlen(szCipher); memset(bitsCiphertextAnyLength,0,32768); memcpy(bitsCiphertextAnyLength,szCipher,iLen); for(long i=0;i< iLen;i++) bitsCiphertextAnyLength[i]-= 48; g_DES2.Bits2Bytes(szCiphertextData,bitsCiphertextAnyLength,iLen); */ return iLen>>3; }