获取IMSI

using System;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Tapi;
using System.Runtime.InteropServices;

namespace test
{
     public struct SimInfo
    {
        public string Manufacturer;
        public string Model;
        public string Revision;
        public string IMEI;
        public string IMSI;
    }

    /// <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 SimInfo GetGeneralInfo(Line line)
        {
            int subscsize = 0;
            int subscoffset = 0;
            SimInfo lgi = new SimInfo();
            byte[] buffer = new byte[512];
            BitConverter.GetBytes(512).CopyTo(buffer, 0);

            if (lineGetGeneralInfo(line.hLine, buffer) != 0)
            {
                throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error(), "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
            }
           
            subscsize = BitConverter.ToInt32(buffer, 12);
            subscoffset = BitConverter.ToInt32(buffer, 16);
            lgi.Manufacturer = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
            lgi.Manufacturer = lgi.Manufacturer.Replace("\0", "");

            subscsize = BitConverter.ToInt32(buffer, 20);
            subscoffset = BitConverter.ToInt32(buffer, 24);
            lgi.Model = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
            lgi.Model = lgi.Model.Replace("\0", "");

            subscsize = BitConverter.ToInt32(buffer, 28);
            subscoffset = BitConverter.ToInt32(buffer, 32);
            lgi.Revision = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
            lgi.Revision = lgi.Revision.Replace("\0", "");

            subscsize = BitConverter.ToInt32(buffer, 36);
            subscoffset = BitConverter.ToInt32(buffer, 40);
            lgi.IMEI = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
            lgi.IMEI = lgi.IMEI.Replace("\0", "");

            subscsize = BitConverter.ToInt32(buffer, 44);
            subscoffset = BitConverter.ToInt32(buffer, 48);
            lgi.IMSI = System.Text.Encoding.Unicode.GetString(buffer, subscoffset, subscsize).ToString();
            lgi.IMSI = lgi.IMSI.Replace("\0", "");
            return lgi;
        }
        /// <summary>
        /// 获取sim卡的IMSI
        /// </summary>
        /// <returns></returns>
        public static string GetIMSINumber()
        {
            string result = "";
            try
            {
                Tapi tapi = new Tapi();
                tapi.Initialize();
                Line line = tapi.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
                ControlTapi ctapi = new ControlTapi();
                SimInfo gi = ctapi.GetGeneralInfo(line);

                result = gi.IMSI;
                line.Dispose();
                tapi.Shutdown();

            }
            catch
            {
                result = "";
            }

            return result;

        }

        /// <summary>
        /// 获取IMEI的号码
        /// </summary>
        /// <returns></returns>
        public static string GetIMEINumber()
        {
            string result = "";
            try
            {
                Tapi tapi = new Tapi();
                tapi.Initialize();
                Line line = tapi.CreateLine(0, LINEMEDIAMODE.INTERACTIVEVOICE, OpenNETCF.Tapi.LINECALLPRIVILEGE.MONITOR);
                ControlTapi ctapi = new ControlTapi();
                SimInfo gi = ctapi.GetGeneralInfo(line);
                result = gi.IMEI;
                line.Dispose();
                tapi.Shutdown();

            }
            catch
            {
                result = "";
            }

            return result;
        }
    }

    public class ControlCell
    {
        public static string GetCellNumber()
        {
            return string.Empty;
        }
 
    }
}




TapiLab  源于网络,可以上网找找。

你可能感兴趣的:(IMSI)