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 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:
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:
With the infrastructure code out of the way, we're now ready to execute the sequence to retrieve the 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.
|