Windows Mobile读取SIM卡联系人

最近一个项目,需要给用户在项目中增加一个联系人导入的小组建,Windows Mobile自身系统所带的联系人很容易读取,微软已经封装了相应的类库,对于SIM卡的联系人,需要自己进行封装。

写了个类,已经可以读取SIM联系人的手机号码和联系人姓名了,但在我的双网双待的机器上,怎么也读不出来,毕竟像我这样BT的机器并不多,代码全部粘贴出来,以供需要之人参考。

    /// <summary>
    /// SIM卡联系人操作类
    /// </summary>
    public class SIMContactManage
    {
        private const Int64 S_OK = 0x00000000;
        public const int SIM_CAPSTYPE_ALL = 0x3F; // 所有联系人
        public const int SIM_PBSTORAGE_SIM = 0x10; //
        public const int SIM_SMSSTORAGE_SIM = 0x2; //

        [StructLayout(LayoutKind.Sequential)]
        public struct SIMPHONEBOOKENTRY
        {
            public uint cbSize; //
            public uint dwParams; //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string lpszAddress; // 联系人电话
            public uint dwAddressType; //
            public uint dwNumPlan; //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string lpszText; // 联系人姓名
        }

        [DllImport("cellcore.dll")]
        public static extern int SimInitialize(uint dwFlags,
        int lpfnCallBack, uint dwParam, ref int lphSim);
        [DllImport("cellcore.dll")]
        public static extern int SimGetPhonebookStatus(int hSim,
        uint dwLocation, ref uint lpdwUsed, ref uint lpdwTotal);
        [DllImport("cellcore.dll")]
        public static extern int SimReadPhonebookEntry(int hSim, uint dwLocation, uint dwIndex, ref SIMPHONEBOOKENTRY entry);
        [DllImport("cellcore.dll", SetLastError = true)]
        public static extern int SimDeinitialize(int hSim);

        /// <summary>
        /// 获取SIM卡联系人信息
        /// </summary>
        /// <returns></returns>
        public static List<string[]> GetSIMContactList()
        {
            int hSim = 0;
            List<string[]> list = new List<string[]>();
            try
            {
                int result = SimInitialize(0, 0, 0, ref hSim);
                if (result != 0)
                    throw new Exception("SIM打卡失败,请检测SIM是否安装!");
                uint uiUsed = 0;
                uint uiTotal = 0;
                result = SimGetPhonebookStatus(hSim, SIM_PBSTORAGE_SIM, ref uiUsed, ref uiTotal);


                for (int i = 1; i <= uiUsed; i++)
                {
                    SIMPHONEBOOKENTRY entry = new SIMPHONEBOOKENTRY();
                    entry.cbSize = (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
                    result = SimReadPhonebookEntry(hSim, SIM_PBSTORAGE_SIM, (uint)i, ref entry);
                    list.Add(new string[2] {entry.lpszText.Trim(),entry.lpszAddress.Trim() });
                }
                return list;

            }
            catch
            {
                throw;
            }
            finally
            {
                SimDeinitialize(hSim);
               
            }
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值