IMEI(International Mobile Equipment Identity,国际移动身份识别)俗称”串号”存储在手机的EEPROM(俗称码片)里,每一个移动设备都对应一个唯一的IMEI。其组成结构为TAC(6位数字)+FAC(两位数字)+SNR(6位数字)+SP (1位数字)。IMEI唯一地识别一个移动设备,用于监控被窃或无效的移动设备。
IMSI的全称是International Mobile Subscriber Identification Number,也就是客户识别码。在GSM系统中,给每个移动用户分配一个唯一的国际用户识别码IMSI,此码在网中所有位置包括漫游区都是有效的。
下面的类是自己封装的,用来获取IMEI/IMSI号码。
#pragma once
#endif // _MSC_VER > 1000
//#include "resource.h"
#define TAPI_API_LOW_VERSION 0x00010003
#define TAPI_CURRENT_VERSION 0x00020000
#define EXT_API_LOW_VERSION 0x00010000
#define EXT_API_HIGH_VERSION 0x00010000
typedef struct Info
{
char IMSI[128];
char IMEI[128];
}Siminfo;
class SimInfo
{
public:
SimInfo();
virtual ~SimInfo();
static SimInfo Info;
public:
bool Init();
Siminfo GetGeneralInfo();
DWORD m_dwNumDevs;
DWORD m_dwAPIVersion;
HLINEAPP m_hLineApp;
HLINE m_hLine;
DWORD m_dwExtVersion;
DWORD m_dwTSPILineDeviceID;
LPTSTR g_dwImei;
private:
void GetTAPIErrorMsg(TCHAR *szMsg,int nSize, DWORD dwError);
DWORD GetTSPLineDeviceID(const TCHAR* const psTSPLineName);
};
#endif // !defined(AFX_IMEI1_H__84CB8CFD_9DA9_470E_867E_2D689F916CBE__INCLUDED_)
// DevInfo.cpp: implementation of the DevInfo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "imei.h"
#include "DevInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DevInfo::DevInfo()
{
m_dwAPIVersion = TAPI_CURRENT_VERSION;
m_hLineApp = 0;
m_hLine = 0;
m_dwExtVersion = 0;
}
DevInfo::~DevInfo()
{
}
bool DevInfo::Init()
{
// set the line init params
LINEINITIALIZEEXPARAMS LineExtParams;
LineExtParams.dwTotalSize = sizeof(LineExtParams);
LineExtParams.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
if (::lineInitializeEx(&m_hLineApp, 0, 0, _T("Developer.com Test"),
&m_dwNumDevs, &m_dwAPIVersion, &LineExtParams))
{
//EndDialog(IDOK);
return false;
}
m_dwTSPILineDeviceID = GetTSPLineDeviceID(CELLTSP_LINENAME_STRING);
//m_dwTSPILineDeviceID = GetTSPLineDeviceID(L"GPRS Modem - Data Port as COM7:");
if ( m_dwTSPILineDeviceID == (DWORD)-1 )
{
::lineShutdown(m_hLineApp);
//EndDialog(IDOK);
return false;
}
// open the line
if( ::lineOpen(m_hLineApp, m_dwTSPILineDeviceID,
&m_hLine, m_dwAPIVersion, 0, 0,
LINECALLPRIVILEGE_OWNER, LINEMEDIAMODE_DATAMODEM, 0) )
{
::lineShutdown(m_hLineApp);
//EndDialog(IDOK);
return false;
}
// set up ExTAPI
if ( ::lineNegotiateExtVersion(m_hLineApp, m_dwTSPILineDeviceID,
m_dwAPIVersion, EXT_API_LOW_VERSION,
EXT_API_HIGH_VERSION, &m_dwExtVersion) )
{
::lineClose(m_hLine);
::lineShutdown(m_hLineApp);
//EndDialog(IDOK);
return false;
}
return true;
}
DWORD DevInfo::GetTSPLineDeviceID(const TCHAR *const psTSPLineName)
{
DWORD dwReturn = -1;
for(DWORD dwCurrentDevID = 0 ; dwCurrentDevID < m_dwNumDevs ; dwCurrentDevID++)
{
LINEEXTENSIONID LineExtensionID;
if( ::lineNegotiateAPIVersion(m_hLineApp, dwCurrentDevID,
TAPI_API_LOW_VERSION, TAPI_CURRENT_VERSION,
&m_dwAPIVersion, &LineExtensionID) == 0 )
{
LINEDEVCAPS LineDevCaps;
LineDevCaps.dwTotalSize = sizeof(LineDevCaps);
if( ::lineGetDevCaps(m_hLineApp, dwCurrentDevID,
m_dwAPIVersion, 0, &LineDevCaps) == 0 )
{
BYTE* pLineDevCapsBytes = new BYTE[LineDevCaps.dwNeededSize];
if(0 != pLineDevCapsBytes)
{
LINEDEVCAPS* pLineDevCaps = (LINEDEVCAPS*)pLineDevCapsBytes;
pLineDevCaps->dwTotalSize = LineDevCaps.dwNeededSize;
if( ::lineGetDevCaps(m_hLineApp, dwCurrentDevID,
m_dwAPIVersion, 0, pLineDevCaps) == 0 )
{
if(0 == _tcscmp((TCHAR*)((BYTE*)pLineDevCaps+pLineDevCaps->dwLineNameOffset),
psTSPLineName))
{
dwReturn = dwCurrentDevID;
}
}
delete[] pLineDevCapsBytes;
}
}
}
}
return dwReturn;
}
//获取手机SIM卡信息(IMEI国际移动设备识别码)国际移动用户识别码(IMSI)
DevInfo DevInfo::GetGeneralInfo()
{
DevInfo Result;
memset(Result.IMEI, 0, 128);
memset(Result.IMSI, 0, 128);
LPBYTE pLineGeneralInfoBytes = NULL;
const DWORD dwMediaMode = LINEMEDIAMODE_DATAMODEM;
LINEGENERALINFO lviGeneralInfo;
LPLINEGENERALINFO plviGeneralInfo;
LPTSTR tsManufacturer, tsModel, tsRevision, tsSerialNumber, tsSubscriberNumber;
CString sInfo;
lviGeneralInfo.dwTotalSize = sizeof(lviGeneralInfo);
LONG lRes = ::lineGetGeneralInfo(m_hLine, &lviGeneralInfo);
if (lRes != 0 && lRes != LINEERR_STRUCTURETOOSMALL)
{
TCHAR szMsg[255];
GetTAPIErrorMsg(szMsg,sizeof(szMsg), lRes);
AfxMessageBox(szMsg);
// return c;
}
pLineGeneralInfoBytes = new BYTE[lviGeneralInfo.dwNeededSize];
plviGeneralInfo = (LPLINEGENERALINFO)pLineGeneralInfoBytes;
if(pLineGeneralInfoBytes != NULL)
{
plviGeneralInfo->dwTotalSize = lviGeneralInfo.dwNeededSize;
if ( (lRes = ::lineGetGeneralInfo(m_hLine, plviGeneralInfo)) != 0 )
{
TCHAR szMsg[255];
GetTAPIErrorMsg(szMsg,sizeof(szMsg), lRes);
AfxMessageBox(szMsg);
}
else
{
TCHAR szUnavailable[] = L"Unavailable";
if(plviGeneralInfo->dwManufacturerSize)
{
tsManufacturer = (WCHAR*)(((BYTE*)plviGeneralInfo)+plviGeneralInfo->dwManufacturerOffset);
}
else
{
tsManufacturer = szUnavailable;
}
if(plviGeneralInfo->dwModelSize)
{
tsModel = (WCHAR*)(((BYTE*)plviGeneralInfo)+plviGeneralInfo->dwModelOffset);
}
else
{
tsModel = szUnavailable;
}
if(plviGeneralInfo->dwRevisionSize)
{
tsRevision = (WCHAR*)(((BYTE*)plviGeneralInfo)+plviGeneralInfo->dwRevisionOffset);
}
else
{
tsRevision = szUnavailable;
}
if(plviGeneralInfo->dwSerialNumberSize)
{
tsSerialNumber = (WCHAR*)(((BYTE*)plviGeneralInfo)+plviGeneralInfo->dwSerialNumberOffset);
}
else
{
tsSerialNumber = szUnavailable;
}
if(plviGeneralInfo->dwSubscriberNumberSize)
{
tsSubscriberNumber = (WCHAR*)(((BYTE*)plviGeneralInfo)+plviGeneralInfo->dwSubscriberNumberOffset);
}
else
{
tsSubscriberNumber = szUnavailable;
}
sInfo.Format(L"Manufacturer: %s\nModel: %s\nRevision: %s\nSerial No: %s\nSubscriber No: %s\n",
tsManufacturer,
tsModel,
tsRevision,
tsSerialNumber,
tsSubscriberNumber);
int a = sizeof(tsSerialNumber);
int b = sizeof(tsSubscriberNumber);
int Length =0;
while(1)
{
Result.IMSI[Length++] = (char)*tsSubscriberNumber++;
if((char)*tsSubscriberNumber<=0 )
break;
}
int Len = 0;
while(1)
{
Result.IMEI[Len++] = (char)*tsSerialNumber++;
if( (char)*tsSerialNumber<=0)
break;
}
}
}
delete [] pLineGeneralInfoBytes;
if( strlen(Result.IMEI) > 15)
{
memcpy(Result.IMEI,(Result.IMEI) + (strlen(Result.IMEI) - 15),15);
Result.IMEI[15] = '\0';
}
if( strlen(Result.IMSI) > 15)
memcpy(Result.IMSI,(Result.IMSI) + (strlen(Result.IMSI) - 15),15);
return Result;
}
void DevInfo::GetTAPIErrorMsg(TCHAR *szMsg, int nSize, DWORD dwError)
{
LPTSTR lpBuffer = 0;
DWORD dwRet = 0;
dwRet = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,TAPIERROR_FORMATMESSAGE(dwError),MAKELANGID(LANG_NEUTRAL, LANG_NEUTRAL),
(LPTSTR) &lpBuffer,0,NULL);
memset(szMsg,0,nSize);
if (lpBuffer && dwRet)
{
_tcscpy(szMsg,lpBuffer);
LocalFree(lpBuffer);
}
else
{
_stprintf(szMsg,L"Unknown Error: 0x%X",dwError);
}
}
用法示例:
DevInfo imei;
imei.Init();
Info e = imei.GetGeneralInfo();