Windows mobile 下讀取手機SIM卡資訊

來源:互聯網
上載者:User
最近在忙一個移動警務的項目,需要擷取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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.