获取sim卡信息出现异常-无法找到 PInvoke DLL“cellcore.dll“
win ce 获取sim卡信息出现异常-无法找到 PInvoke DLL“cellcore.dll“ Common c=new Common();//封装sim相关信息的类 txtIMEI.Text = c.GetAllSection();//获取sim卡所有信息
问题:在调用方法 GetAllSection 获取sim卡信息时,执行到方法下面这句出现 无法找到 PInvoke DLL“cellcore.dll“的异常: res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim);
/***************这是我参考网上的方法*****************/
/* * 初始化Sim卡列表,并返回一个可以操作的句柄lphSim * 开门用的,而且把门把手(lphSim)交给你掌握 * */ [DllImport("cellcore.dll")] private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);
/* * 关闭Sim卡列表 * 出门后一定要记得不要留任何把柄在门把手上 * */ [DllImport("cellcore.dll")] private static extern IntPtr SimDeinitialize(IntPtr hSim);
/* * 读取指定的记录 * 进门后翻东西,hSim,门把手记得留在外面,否则让人锁在屋里 * dwAddress,要在哪拿东西,dwRecordType,拿什么东西,dwIndex,从哪开始拿 * lpData,自己的兜,dwBufferSize,兜的大小,lpdwBytesRead,实际上拿了多少东西 * */ [DllImport("cellcore.dll")] private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);
/* * 获取Sim卡指定记录的信息(数据结构) * 计划一下应该拿什么东西,用些门砖。hSim,门把手,不要丢 * dwAddress,东西放在哪;lpSimRecordInfo,门砖是要回收地(沾满钻石的羊肉啊) * */ [DllImport("cellcore.dll")] private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);
//定义常量 const int EF_ICCID = 0x2FE2; //ICCID号码在Sim卡的存储地址 const int SIM_RECORDTYPE_TRANSPARENT = 0x00000001; //只获取单个记录
[StructLayout(LayoutKind.Sequential)]//Sim卡记录的数据结构 private struct SimRecord { public IntPtr cbSize; public IntPtr dwParams; public IntPtr dwRecordType; public IntPtr dwItemCount; public IntPtr dwSize; }
[StructLayout(LayoutKind.Sequential)] private struct EF_Content { public int dwAddress; public string Description;
public EF_Content(int addr, string desc) { dwAddress = addr; Description = desc; } }
private EF_Content[] efs = { new EF_Content(0x2FE2, "ICC identification"), new EF_Content(0x6F05, "Language preference"), new EF_Content(0x6F07, "IMSI"), new EF_Content(0x6F20, "Ciphering key Kc"), new EF_Content(0x6F30, "PLMN selector"), new EF_Content(0x6F31, "HPLMN search period"), new EF_Content(0x6F37, "ACM maximum value"), new EF_Content(0x6F38, "SIM service table"), new EF_Content(0x6F39, "Accumulated call meter"), new EF_Content(0x6F3E, "Group identifier level 1"), new EF_Content(0x6F3F, "Group identifier level 2"), new EF_Content(0x6F41, "PUCT"), new EF_Content(0x6F45, "CBMI"), new EF_Content(0x6F46, "Service provider name"), new EF_Content(0x6F74, "BCCH"), new EF_Content(0x6F78, "Access control class"), new EF_Content(0x6F7B, "Forbidden PLMNs"), new EF_Content(0x6F7E, "Location information"), new EF_Content(0x6FAD, "Administrative data"), new EF_Content(0x6FAE, "Phase identification"), new EF_Content(0x6F3A, "Abbreviated dialling numbers"), new EF_Content(0x6F3B, "Fixed dialling numbers"), new EF_Content(0x6F3C, "Short messages"), new EF_Content(0x6F3D, "Capability configuration parameters"), new EF_Content(0x6F40, "MSISDN storage"), new EF_Content(0x6F42, "SMS parameters"), new EF_Content(0x6F43, "SMS status"), new EF_Content(0x6F44, "Last number dialled"), new EF_Content(0x6F4A, "Extension 1"), new EF_Content(0x6F4B, "Extension 2") // And so on…. };
/// <summary> /// 获取 Sim 卡中的信息。 /// </summary> /// <returns> 按行返回所有能够获取的 Sim 卡信息,详细请参考《GSM 标准 5.0(11.11)》。</returns> public string GetAllSection() { StringBuilder sb = new StringBuilder(); try { IntPtr res, hSim; res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim); if (res != IntPtr.Zero) throw new Exception("Can not Initialize Sim(" + res + ").");
for (int j = 0; j < efs.Length; j++) { try { SimRecord rec = new SimRecord(); rec.cbSize = (IntPtr)Marshal.SizeOf(rec); res = SimGetRecordInfo(hSim, (IntPtr)efs[j].dwAddress, ref rec); if (res != IntPtr.Zero) throw new Exception("Could not read \"" + efs[j].Description + "\"’s record information from the SIM(" + res + ")."); byte[] bData = new byte[(int)rec.dwSize + 1]; IntPtr dwBytesRead = IntPtr.Zero; res = SimReadRecord(hSim, (IntPtr)efs[j].dwAddress, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead); if (res != IntPtr.Zero) throw new Exception("Could not read \"" + efs[j].Description + "\"’s content from the SIM(" + res + ")");
string str = ""; for (int i = 0; i < bData.Length - 1; i++) { str = str + bData[i].ToString("X2");//ReversByte(bData[i]); } str = efs[j].Description + "(len: " + dwBytesRead.ToInt32() + "): " + str + "\r\n"; sb.Append(str); } catch (Exception ex) { sb.Append(ex.Message + "\r\n"); } Application.DoEvents(); } SimDeinitialize(hSim); } catch (Exception ex) { throw ex;// ex.Message; } return sb.ToString(); }
[DllImport("coredll.dll")] private extern static int GetDeviceUniqueID([In, Out] byte[] appdata, int cbApplictionData, int dwDeviceIDVersion, [In, Out] byte[] deviceIDOuput, out uint pcbDeviceIDOutput);
/// <summary> /// 获取15位的设备ID /// </summary> /// <param name="AppString"></param> /// <returns></returns> public static string GetDeviceID() { string AppString = "MyAppString"; byte[] AppData = new byte[AppString.Length]; for (int count = 0; count < AppString.Length; count++) AppData[count] = (byte)AppString[count]; int appDataSize = AppData.Length; byte[] DeviceOutput = new byte[20]; uint SizeOut = 20; GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, out SizeOut);
StringBuilder deviceID = new StringBuilder(); for (int i = 0; i < DeviceOutput.Length; i++) { deviceID = deviceID.Append(string.Format("{0}", DeviceOutput[i])); }
string idStr = deviceID.ToString(); if (idStr.Length > 15) { idStr = idStr.Substring(0, 15); } return idStr; }
|