星月蓝天

黑色的夜

导航

Windows mobile 下读取手机SIM卡信息 c#改善

Posted on 2011-09-05 17:51  星月  阅读(496)  评论(0编辑  收藏  举报

参考网上有其他写的c#代码有点问题。
添加部分,测试真机环境mobile 6.1
原网址:http://www.watch-life.net/windows-mobile/windows-mobile-sim.html

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 manusize = BitConverter.ToInt32(buffer, 12);
            int manuoffset = BitConverter.ToInt32(buffer, 16);
            lgi.Manufacturer = System.Text.Encoding.Unicode.GetString(buffer, manuoffset, manusize);
            lgi.Manufacturer = lgi.Manufacturer.Substring( 0 , lgi.Manufacturer.IndexOf( Convert.ToChar( 0 ) ) );

            int modelsize = BitConverter.ToInt32(buffer, 20);
            int modeloffset = BitConverter.ToInt32(buffer, 24);
            lgi.Model = System.Text.Encoding.Unicode.GetString(buffer, modeloffset, modelsize);
            lgi.Model = lgi.Model.Substring(0, lgi.Model.IndexOf(Convert.ToChar(0)));

            int revsize = BitConverter.ToInt32(buffer, 28);
            int revoffset = BitConverter.ToInt32(buffer, 32);
            lgi.Revision = System.Text.Encoding.Unicode.GetString(buffer, revoffset, revsize);
            lgi.Revision = lgi.Revision.Substring( 0 , lgi.Revision.IndexOf( Convert.ToChar( 0 ) ) );

            int serialsize = BitConverter.ToInt32(buffer, 36);
            int serialoffset = BitConverter.ToInt32(buffer, 40);
            lgi.SerialNumber = System.Text.Encoding.Unicode.GetString(buffer, serialoffset, serialsize);
            lgi.SerialNumber = lgi.SerialNumber.Substring( 0 , lgi.SerialNumber.IndexOf( Convert.ToChar( 0 ) ) );

            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;
        }
    }