Windows Phone平台下获取IMEI、IMSI、制造商、型号、机器ID、网卡MAC地址、机主等信息

开发语言:VC++

支持平台:

  • Windows Mobile 5.0/6.0/6.1/6.5
  • Windows CE 5.0/6.0

实现功能:

  1. 基于TAPI 2.0协议获取IMEI、IMSI、制造商、型号;
  2. 获取机器ID、网卡MAC地址、机主信息。

下载地址:

WCE_DeviceQuery.zip

 

版本历史:

V1.1 2010年06月09日

  • 增加获取设备型号

V1.0 2010年06月08日

  • 完成正式版本。

接口函数:

WCE_DeviceQuery

 源代码:

WCE_DeviceQuery.h

 /* ---------------------------------------------------------- 文件名称:WCE_DeviceQuery.h 作者:秦建辉 MSN:[email protected] 版本历史: V1.1 2010年06月09日 增加获取设备型号。 V1.0 2010年06月08日 完成正式版本。 功能描述: Windows Phone平台获取IMEI、IMSI、制造商、型号、机器ID、网卡MAC地址、机主。 接口函数: WCE_DeviceQuery ------------------------------------------------------------ */ #pragma once #include // 定义程序应用范围 #if (_WIN32_WCE > 0x500) && (_WIN32_WCE < 0x600) #define MACRO_WCE_DEVICEQUERY_FULL 1 // 开启所有功能 #else #define MACRO_WCE_DEVICEQUERY_FULL 0 // 开启部分功能 #endif #ifndef MACRO_T_DEVICE_PROPERTY #define MACRO_T_DEVICE_PROPERTY #define PROPERTY_MAX_LEN 128 // 属性字段最大长度 typedef struct _T_DEVICE_PROPERTY { TCHAR szProperty[PROPERTY_MAX_LEN]; } T_DEVICE_PROPERTY; #endif #ifdef __cplusplus extern "C" { #endif /* 功能:Windows Phone平台获取用于加密的硬件信息 参数说明: iQueryType:需要查询的设备属性 0:IMEI或者ESN 1:IMSI 2:制造商 3:型号 4:机器ID 5:网卡MAC地址 6:机主 properties:存储设备属性值 iSize:可存储的最大设备数 返回值: -1:获取设备信息表失败 -2:不支持的查询类型 >=0:实际获取的设备数 说明: Windows Mobile 5.0/6.0的设备支持查询IMEI、IMSI、制造商和机器ID Windows CE 5.0/6.0的设备只支持查询网卡MAC地址和机主 飞行模式下无法获取IMEI、IMSI、制造商信息 */ INT WCE_DeviceQuery( INT iQueryType, T_DEVICE_PROPERTY *properties, INT iSize ); #ifdef __cplusplus } #endif   

 WCE_DeviceQuery.cpp

 #include "WCE_DeviceQuery.h" #if (MACRO_WCE_DEVICEQUERY_FULL == 1) #include #include #pragma comment(lib, "cellcore.lib") #include #endif #include #pragma comment(lib, "Iphlpapi.lib") // 网卡MAC地址 INT WCE_DeviceQuery_MACAddress( T_DEVICE_PROPERTY *properties, INT iSize ) { INT iTotal = 0; ULONG BufferLength = 0; if( GetAdaptersInfo( NULL, &BufferLength ) == ERROR_BUFFER_OVERFLOW ) { LPBYTE pBuffer = new BYTE[BufferLength]; if( pBuffer != NULL ) { PIP_ADAPTER_INFO pAdapterInfo = reinterpret_cast(pBuffer); if( GetAdaptersInfo( pAdapterInfo, &BufferLength ) == NO_ERROR ) { while( pAdapterInfo ) { // 获取网卡MAC地址 if( pAdapterInfo->AddressLength > 0 ) { if( properties != NULL ) { for( UINT i = 0; i < pAdapterInfo->AddressLength; i++ ) { StringCchPrintf( properties[iTotal].szProperty + (i << 1), PROPERTY_MAX_LEN - (i << 1), TEXT("%02X"), pAdapterInfo->Address[i] ); } if( ++iTotal >= iSize ) { break; } } else { iTotal++; } } pAdapterInfo = pAdapterInfo->Next; } // End While } delete[] pBuffer; } } return iTotal; } // 机主 INT WCE_DeviceQuery_Owner( T_DEVICE_PROPERTY *properties, INT iSize ) { INT iTotal = 0; HKEY hKey; if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("ControlPanel//Owner"), 0, 0, &hKey ) == ERROR_SUCCESS ) { if( properties != NULL ) { DWORD dwByteLen = PROPERTY_MAX_LEN * sizeof(TCHAR); if( RegQueryValueEx( hKey, TEXT("Name"), NULL, NULL, reinterpret_cast(properties[iTotal].szProperty), &dwByteLen ) == ERROR_SUCCESS ) { iTotal++; } } else { iTotal++; } RegCloseKey(hKey); } return iTotal; } #if (MACRO_WCE_DEVICEQUERY_FULL == 1) #define TAPI_API_LOW_VERSION TAPI_CURRENT_VERSION #define TAPI_API_HIGH_VERSION TAPI_CURRENT_VERSION #ifndef CELLTSP_LINENAME_STRING #define CELLTSP_LINENAME_STRING TEXT("Cellular Line") #endif // 如果iQueryType==0,则获取设备IMEI或者ESN,如果iQueryType==1,则获取设备IMSI。 INT WCE_DeviceQuery_LINE( INT iQueryType, T_DEVICE_PROPERTY *properties, INT iSize ) { INT iTotal = 0; HLINEAPP hLineApp; DWORD dwNumDevs = 0; DWORD dwAPIVersion = TAPI_CURRENT_VERSION; LONG nErrCode; // 初始化TAPI LINEINITIALIZEEXPARAMS liep; liep.dwTotalSize = sizeof(liep); liep.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT; if( lineInitializeEx( &hLineApp, NULL, NULL, NULL, &dwNumDevs, &dwAPIVersion, &liep ) != 0 ) { return 0; } // 协商TAPI版本 for( DWORD dwDeviceID = 0; dwDeviceID < dwNumDevs; dwDeviceID++ ) { LINEEXTENSIONID LineExtensionID; if( lineNegotiateAPIVersion( hLineApp, dwDeviceID, TAPI_API_LOW_VERSION, TAPI_API_HIGH_VERSION, &dwAPIVersion, &LineExtensionID ) == 0 ) { LINEDEVCAPS LineDevCaps; LineDevCaps.dwTotalSize = sizeof(LineDevCaps); // 检查设备 nErrCode = lineGetDevCaps( hLineApp, dwDeviceID, dwAPIVersion, 0, &LineDevCaps ); if( (nErrCode == 0) || (nErrCode == LINEERR_STRUCTURETOOSMALL) ) { LPBYTE pLineDevCapsBytes = new BYTE[LineDevCaps.dwNeededSize]; if( pLineDevCapsBytes != NULL ) { // 获取设备名称 LINEDEVCAPS* pLineDevCaps = reinterpret_cast(pLineDevCapsBytes); pLineDevCaps->dwTotalSize = LineDevCaps.dwNeededSize; if( lineGetDevCaps( hLineApp, dwDeviceID, dwAPIVersion, 0, pLineDevCaps) == 0 ) { if( pLineDevCaps->dwLineNameSize > 0 ) { TCHAR* pszLineName = reinterpret_cast(reinterpret_cast(pLineDevCaps) + pLineDevCaps->dwLineNameOffset); if( _tcscmp( pszLineName, CELLTSP_LINENAME_STRING ) == 0 ) { HLINE hLine; // 打开线路 if( lineOpen( hLineApp, dwDeviceID, &hLine, dwAPIVersion, 0, 0, LINECALLPRIVILEGE_OWNER, LINEMEDIAMODE_DATAMODEM, NULL ) == 0 ) { // 获取设备信息 LINEGENERALINFO LineGeneralInfo; LineGeneralInfo.dwTotalSize = sizeof(LineGeneralInfo); nErrCode = lineGetGeneralInfo( hLine, &LineGeneralInfo ); if( (nErrCode == 0) || (nErrCode == LINEERR_STRUCTURETOOSMALL) ) { LPBYTE pLineGeneralInfoBytes = new BYTE[LineGeneralInfo.dwNeededSize]; if( pLineGeneralInfoBytes != NULL ) { LPLINEGENERALINFO lpLineGeneralInfo = reinterpret_cast(pLineGeneralInfoBytes); lpLineGeneralInfo->dwTotalSize = LineGeneralInfo.dwNeededSize; if( lineGetGeneralInfo( hLine, lpLineGeneralInfo ) == 0 ) { DWORD dwSize = 0; TCHAR* pSource = NULL; switch( iQueryType ) { case 0: // IMEI dwSize = lpLineGeneralInfo->dwSerialNumberSize; pSource = reinterpret_cast(reinterpret_cast(lpLineGeneralInfo) + lpLineGeneralInfo->dwSerialNumberOffset); break; case 1: // IMSI dwSize = lpLineGeneralInfo->dwSubscriberNumberSize; pSource = reinterpret_cast(reinterpret_cast(lpLineGeneralInfo) + lpLineGeneralInfo->dwSubscriberNumberOffset); break; case 2: // 制造商 dwSize = lpLineGeneralInfo->dwManufacturerSize; pSource = reinterpret_cast(reinterpret_cast(lpLineGeneralInfo) + lpLineGeneralInfo->dwManufacturerOffset); break; case 3: // 型号 dwSize = lpLineGeneralInfo->dwModelSize; pSource = reinterpret_cast(reinterpret_cast(lpLineGeneralInfo) + lpLineGeneralInfo->dwModelOffset); break; } if( dwSize > 0 ) { if( properties != NULL ) { dwSize >>= 1; // 将字节数转换为字符数 StringCchCopyN( properties[iTotal].szProperty, PROPERTY_MAX_LEN, pSource, dwSize ); if( properties[iTotal].szProperty[dwSize - 1] != TEXT('/0') ) { // 补上字符串结束标记 properties[iTotal].szProperty[dwSize] = TEXT('/0'); } if( ++iTotal >= iSize ) { break; } } else { iTotal++; } } } delete[] pLineGeneralInfoBytes; } } // 获取设备信息 lineClose( hLine ); // 关闭线路 } // 打开线路 } // 设备名称 } } delete[] pLineDevCapsBytes; } } // 检查设备 } // 协商版本 } // End for lineShutdown( hLineApp ); return iTotal; } // 设备ID INT WCE_DeviceQuery_DeviceID( T_DEVICE_PROPERTY *properties, INT iSize ) { INT iTotal = 0; DEVICE_ID Device_ID; BOOL isOK; Device_ID.dwSize = 0; isOK = KernelIoControl( IOCTL_HAL_GET_DEVICEID, NULL, 0, &Device_ID, sizeof(Device_ID), NULL ); if( GetLastError() == ERROR_INSUFFICIENT_BUFFER ) { LPBYTE pDeviceIDBytes = new BYTE[Device_ID.dwSize]; if( pDeviceIDBytes != NULL ) { PDEVICE_ID pDevice_ID = reinterpret_cast(pDeviceIDBytes); pDevice_ID->dwSize = Device_ID.dwSize; isOK = KernelIoControl( IOCTL_HAL_GET_DEVICEID, NULL, 0, pDevice_ID, Device_ID.dwSize, NULL ); if( isOK ) { if( properties != NULL ) { if( pDevice_ID->dwPresetIDBytes > 0 ) { LPBYTE pb = reinterpret_cast(pDevice_ID) + pDevice_ID->dwPresetIDOffset; DWORD dwEnd = pDevice_ID->dwPresetIDBytes; for( DWORD i = 0; i < dwEnd; i++ ) { StringCchPrintf( properties[iTotal].szProperty + (i << 1), PROPERTY_MAX_LEN - ( i << 1), TEXT("%02X"), pb[i] ); } } if( pDevice_ID->dwPlatformIDBytes > 0 ) { LPBYTE pb = reinterpret_cast(pDevice_ID) + pDevice_ID->dwPlatformIDOffset; DWORD dwBegin = pDevice_ID->dwPresetIDBytes; DWORD dwEnd = pDevice_ID->dwPresetIDBytes + pDevice_ID->dwPlatformIDBytes; for( DWORD i = dwBegin; i < dwEnd; i++ ) { StringCchPrintf( properties[iTotal].szProperty + (i << 1), PROPERTY_MAX_LEN - ( i << 1), TEXT("%02X"), pb[i - dwBegin] ); } } } if( pDevice_ID->dwPresetIDBytes + pDevice_ID->dwPlatformIDBytes > 0 ) { iTotal++; } } delete[] pDeviceIDBytes; } } return iTotal; } #endif INT WCE_DeviceQuery( INT iQueryType, T_DEVICE_PROPERTY *properties, INT iSize ) { INT iTotal = 0; switch( iQueryType ) { #if (MACRO_WCE_DEVICEQUERY_FULL == 1) case 0: // IMEI或者ESN iTotal = WCE_DeviceQuery_LINE( 0, properties, iSize ); break; case 1: // IMSI iTotal = WCE_DeviceQuery_LINE( 1, properties, iSize ); break; case 2: // 制造商 iTotal = WCE_DeviceQuery_LINE( 2, properties, iSize ); break; case 3: // 型号 iTotal = WCE_DeviceQuery_LINE( 3, properties, iSize ); break; case 4: // 机器ID iTotal = WCE_DeviceQuery_DeviceID( properties, iSize ); break; #endif case 5: // 网卡MAC地址 iTotal = WCE_DeviceQuery_MACAddress( properties, iSize ); break; case 6: // 机主 iTotal = WCE_DeviceQuery_Owner( properties, iSize ); break; } return iTotal; }

你可能感兴趣的:(Windows Phone平台下获取IMEI、IMSI、制造商、型号、机器ID、网卡MAC地址、机主等信息)