获取sim卡的唯一标识--imsi号

现在,手机号码大部分都不存在sim卡上了,所以有的sim卡是取不到手机号的,但是,可以取到一个imsi号,这个号是一个15位的数字,可以跟据这个号来做移动开发的身份验证,取法如下: 

        //获取sim卡的imsi号,做为该用户的唯一标识
        public string getMobileImsi()
        {
            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 = "";
            }
            //返回sim卡的imsi
            return result;
        }
        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>
            public 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;

            }
        }
        public struct GeneralInfo
        {
            public string Manufacturer;
            public string Model;
            public string Revision;
            public string SerialNumber;
            public string SubscriberNumber;
        }

注意:其中用到一个TapiLib.dll包,大家可以去网上找一下,下载下来,由于这里不能上传附件(我没有找到),见谅!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值