一.原理
使用了Pocket PC SDK的EXTAPI
实现过程如下:
1.首先加如头文件:
#include "tapi.h"
#include "extapi.h"
#define TAPI_API_HIGH_VERSION 0x00020000
#define EXT_API_LOW_VERSION 0x00010000
#define EXT_API_HIGH_VERSION 0x00010000
2.引入库文件cellcore.lib
3.输入代码即可:
BOOL GetDeviceSIMInfo(CString &strIMEI,CString &strIMSI)
{
BOOL bSus = FALSE; //返回结果是否正确
TCHAR szIMEI[MAX_PATH]; //保存设备的IMEI
TCHAR szIMSI[MAX_PATH]; //保存设备的IMSI
//初始化数组
memset(szIMEI,0,sizeof(strIMEI));
memset(szIMSI,0,sizeof(szIMSI));
LINEGENERALINFO *lineGeneralInfo; //保存设备序列号的信息的结构体
HLINEAPP hLineApp = 0;
HLINE hLine = 0;
DWORD dwNumDevs;
DWORD dwAPIVersion = TAPI_API_HIGH_VERSION;
DWORD dwExtVersion = 0;
DWORD dwDeviceID;
DWORD dwMediaMode = LINEMEDIAMODE_DATAMODEM | LINEMEDIAMODE_INTERACTIVEVOICE;
LONG tapiresult;
DWORD temp;
LINEINITIALIZEEXPARAMS lineInitializeExParams;
lineGeneralInfo = (LINEGENERALINFO*)malloc(1024);
lineGeneralInfo->dwTotalSize = sizeof(LINEGENERALINFO);
lineInitializeExParams.dwTotalSize = sizeof(lineInitializeExParams);
lineInitializeExParams.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT; //The application desires to use the Event Handle event notification mechanism
tapiresult = lineInitializeEx(&hLineApp, 0, 0,L"SimTry", &dwNumDevs, &dwAPIVersion,&lineInitializeExParams); //returns 0 (SUCCESS)
lineGeneralInfo = (LINEGENERALINFO*)malloc(sizeof(LINEGENERALINFO));
lineGeneralInfo->dwTotalSize = sizeof(LINEGENERALINFO);
for (dwDeviceID = 0; dwDeviceID < dwNumDevs;dwDeviceID++)
{
tapiresult = lineNegotiateExtVersion(hLineApp, dwDeviceID, dwAPIVersion, EXT_API_LOW_VERSION,
EXT_API_HIGH_VERSION, &dwExtVersion); //returns 0 (SUCCESS)
tapiresult = lineOpen(hLineApp, dwDeviceID,&hLine, dwAPIVersion, 0, 0,
LINECALLPRIVILEGE_OWNER, dwMediaMode, 0); //returns 0 (SUCCESS)
tapiresult = lineGetGeneralInfo(hLine, lineGeneralInfo); //returns 0 (SUCCESS)
if((tapiresult == 0) && (lineGeneralInfo->dwNeededSize > lineGeneralInfo->dwTotalSize))
{
//重新获取最新的值
temp = lineGeneralInfo->dwNeededSize;
free(lineGeneralInfo);
lineGeneralInfo = (LINEGENERALINFO*)malloc(temp);
lineGeneralInfo->dwTotalSize = temp;
tapiresult = lineGetGeneralInfo(hLine, lineGeneralInfo);
//已将结果拿到
//IMEI
lstrcpy(szIMEI, (TCHAR*)((char*)lineGeneralInfo
+ lineGeneralInfo->dwSerialNumberOffset));
//IMSI
if(lineGeneralInfo->dwSubscriberNumberSize > 2)
{
for(int j=0;jdwSubscriberNumberSize/2;j++)
szIMSI[j] = *((unsigned short *)(lineGeneralInfo) + j + lineGeneralInfo->dwSubscriberNumberOffset/2);
}
bSus = TRUE;
break;
}
}
if(bSus)
{
strIMEI = CString(szIMEI);
strIMSI = CString(szIMSI);
}
//回收资源
free(lineGeneralInfo);
if(hLine)
lineClose(hLine);
if(hLineApp)
lineShutdown(hLineApp);
return bSus;
}