Windows mobile 下讀取手機SIM卡資訊

freshairpeng發表於2009-03-22
最近在忙一個移動警務的專案,需要獲取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;
    }
 
    ///
    /// Tapi控制類
    ///

    public class ControlTapi
    {
 
        [DllImport("cellcore.dll")]
        private static extern int lineGetGeneralInfo(IntPtr hLigne,byte[]lpLineGeneralInfo );
 
        ///
        /// 呼叫cellcore.dll獲取sim卡的綜合資訊
        ///

        ///
        ///
        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;
 
        }
 
       
 
        ///
        /// 獲取sim卡的IMSI
        ///

        ///
        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;
 
        }
 
        ///
        /// 獲取IMEI的號碼
        ///

        ///
        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://files.cnblogs.com/xjb/TapiLib.rar
4、參考資料:
http://hucgy.bokee.com/3328836.html
 

本文首發地址:http://www.watch-life.net/windows-mobile/windows-mobile-sim.html

----------------------------------------------

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14766526/viewspace-573378/,如需轉載,請註明出處,否則將追究法律責任。

相關文章