最近在忙一個移動警務的項目,需要擷取SIM卡的資訊,來做身份的驗證。考慮到擷取:國際行動裝置識別碼(IMEI:International Mobile Equipment Identification Number)和國際移動使用者識別碼(IMSI:International Mobile Subscriber Identification Number),讀取這兩個號碼用到TAPI的lineGetGeneralInfo()函。在新版的OpenNetCF裡沒有發現對這個函數的封裝(也許我沒找到),於是到網上找了找,找到一個以前版本OpenNetCF裡的:TapiLib.dll,包含對Windows ce phone api 的封裝(TAPI),綜合網上的一些資料,實現代碼如下:
public struct GeneralInfo
{
public string Manufacturer;
public string Model;
public string Revision;
public string SerialNumber;
public string SubscriberNumber;
} /// <summary>
/// Tapi控制類
/// </summary>
public class ControlTapi
{ [DllImport("cellcore.dll")]
private static extern int lineGetGeneralInfo(IntPtr hLigne,byte[]lpLineGeneralInfo ); /// <summary>
/// 調用cellcore.dll擷取sim卡的綜合資訊
/// </summary>
/// <param name="l"></param>
/// <returns></returns>
private GeneralInfo GetGeneralInfo(Line l)
{
GeneralInfo lgi = new GeneralInfo();
byte[] buffer = new byte[512];
BitConverter.GetBytes(512).CopyTo(buffer, 0); if (lineGetGeneralInfo(l.hLine, buffer) != 0)
{
throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
} int subscsize = BitConverter.ToInt32(buffer, 44);
int subscoffset = BitConverter.ToInt32(buffer, 48);
lgi.SubscriberNumber = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
lgi.SubscriberNumber = lgi.SubscriberNumber.Replace("/0", "");
return lgi; } /// <summary>
/// 擷取sim卡的IMSI
/// </summary>
/// <returns></returns>
public static string GetIMSINumber()
{
string result = "";
try
{
Tapi t = new Tapi();
t.Initialize();
Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
ControlTapi ctapi = new ControlTapi();
GeneralInfo gi = ctapi.GetGeneralInfo(l);
result = gi.SubscriberNumber;
l.Dispose();
t.Shutdown(); }
catch// (Exception ex)
{
result = "";
} return result; } /// <summary>
/// 擷取IMEI的號碼
/// </summary>
/// <returns></returns>
public static string GetIMEINumber()
{
string result = "";
try
{
Tapi t = new Tapi();
t.Initialize();
Line l = t.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
ControlTapi ctapi = new ControlTapi();
GeneralInfo gi = ctapi.GetGeneralInfo(l);
result = gi.SerialNumber;
l.Dispose();
t.Shutdown(); }
catch// (Exception ex)
{
result = "";
} return result;
}
}
vb 的代碼你可以去看看這裡:http://www.peterfoot.net/RetrieveIMEIThroughTAPI.aspx
另:
1、環境:在vs2005+windows mobile 5.0 +多普達818測試通過。
2、
關於擷取SIM卡的本機號碼,你可以用:http://www.microsoft.com/china/msdn/archives/library/dnnetcomp/html/netcfPhoneAPI.asp,這裡 提供的方法,不過這個方法需要安全認證,比較麻煩,具體認證的方式見:http://www.microsoft.com/china/MSDN/library/Mobility/pocketpc/2k3smartphonesecurity.mspx?pf=true。
3、TapiLib.dll的:http://www.cnblogs.com/Files/xjb/TapiLib.rar
4、參考資料:
http://hucgy.bokee.com/3328836.html