Microsoft  CSIP   WindowsMobile   MEDC   合作伙伴   体验中心 设为首页 加到收藏夹

 
登录       点击换一张     注册新用户 找回密码

HOW-TO: Retrieve the ICC-ID using the SIM Manager APIs
2008年07月21日02:09   编辑:Neil Cowburn 来源: 博客 浏览: 352

Today, we had a question in the forums about how to retrieve the ICCID from the SIM card in a Windows Mobile device.

A SIM card comprises of a microcontroller, ROM storage for programs and EEPROM storage for various bits of information like the IMSI, network-specific subscriber data and security information. The layout of the SIM file system on the EEPROM is fairly basic, consisting of a master file storing two dedicated files, each of which contain elementary files. These elementary files hold the GSM data, with each elementary file containing just one record. A record in this instance could be a phone book or the IMSI. One such record is the ICCID (also known as the SIM Serial Number, SSN) and you may know it as is the 19-digit number printed on the plastic housing surrounding the SIM module.

I did a little bit of research this evening and it turns out it's not too difficult to retrieve this number and you can use the SIM Manager API to do it. The 3 functions in the SIM Manager API that we are interested in are SimInitialize, SimDeinitialize, and SimReadRecord.

SECURITY NOTE

SimReadRecord is a privileged function. You either need to lower the security configuration on your device to Prompt One Tier or lower using the Device Security Manager, or sign your code with a privileged certificate.

The P/Invoke prototypes for these functions are pretty self-explanatory and don't require any complex marshaling. Here they are in all their glory:

  1. <DllImport("cellcore.dll")> _   
  2. Shared Function SimInitialize( _   
  3.         ByVal dwFlags As Integer, _   
  4.         ByVal lpfnCallback As IntPtr, _   
  5.         ByVal dwParam As Integer, _   
  6.         ByRef lphSim As IntPtr) As Integer  
  7. End Function  
  8.   
  9. <DllImport("cellcore.dll")> _   
  10. Shared Function SimDeinitialize( _   
  11.         ByVal hSim As IntPtr) As Integer  
  12. End Function  
  13.   
  14. <DllImport("cellcore.dll")> _   
  15. Shared Function SimReadRecord( _   
  16.         ByVal hSim As IntPtr, _   
  17.         ByVal dwAddress As Integer, _   
  18.         ByVal dwRecordType As Integer, _   
  19.         ByVal dwIndex As Integer, _   
  20.         ByVal lpData() As Byte, _   
  21.         ByVal dwBufferSize As Integer, _   
  22.         ByRef dwSize As IntegerAs Integer  
  23. End Function  

The key to retrieving the ICCID is the call to SimReadRecord. This will allow us to retrieve a elementary file from the SIM's EEPROM. SimReadRecord takes an address of the record we want to read. This address is defined as EF_ICCID below:

  1. Dim EF_ICCID As Integer = &H2FE2   
  2. Dim SIM_RECORDTYPE_TRANSPARENT As Integer = 1  

With the infrastructure code out of the way, we're now ready to execute the sequence to retrieve the ICCID.

  1. Dim hSim As IntPtr   
  2. Dim iccid(9) As Byte  
  3.   
  4. SimInitialize(0, IntPtr.Zero, 0, hSim)   
  5.   
  6. SimReadRecord(hSim, _   
  7.               EF_ICCID, _   
  8.               SIM_RECORDTYPE_TRANSPARENT, _   
  9.               0, _   
  10.               iccid, _   
  11.               iccid.Length, _   
  12.               0)   
  13.   
  14. SimDeinitialize(hSim)   
  15.   
  16. Dim SimSerialNumber As String = FormatAsSimString(iccid)  

FormatAsSimString is a method that I wrote to convert the ICCID byte array into the same format as printed on the SIM card. SimSerialNumber will be formatted string that looks something like this:

111111 22222 3333 4444

If you compare the bytes in the ICCID array to the digits printed on the SIM card, you'll note they don't quite match up. This is because the ICCID array is an array of 4-bit unsigned integers in little endian order. For example, if the first 6 digits of your ICCID is 894412, they will appear as 0x98, 0x44, 0x21 in the byte array. The helper method below will convert a pair of 4-bit integers (i.e, a byte) into a string.

  1. Shared Function ConvertInt4PairToString(ByVal byteValue As ByteAs String  
  2.     Return ((byteValue << 4) >> 4) & (byteValue >> 4)   
  3. End Function  
发表评论
评论标题 :
评论内容 :

    查看评论

请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。 本站拥有管理笔名和留言的一切权利。
相关文章推荐
· WM6应用层代码检测POWER键RESUME动作 2008-7-21  Walzer
· Windows Mobile 6 Professional Emulator中串口设置问题 2008-7-21  浪子
· Windows Mobile 6 多普达P660银色闪亮登场 2008-7-16  WM移动解决方案联盟
· 微软PDC大会将谈Windows 7、Windows Mobile 2008-7-9  匿名美国当地时间周二公开demo(示范)的Windows 7 时间很短,但还不错,只是开发人员若真想实际感受一下,可能还得等到10月份在旧金山举行的开发者大会才行。
· Windows Mobile下的MMS开发 2008-7-8  cppguy

  

论坛推荐  
热点活动
更多»

© CSIP 信息产业部软件与集成电路促进中心 All Rights Reserved 版权所有 京ICP备06020771号
联系电话:010-63951881-8003 王先生        邮件:wangj@csip.org.cn