none
Accessing SIM using C# SIM Manager API

    Question

  • I hope that if anyone have done this before, please help me!!! I am a newbie in Visual C# Window Mobile 6.0 SDK, apparently trying to write to the SIM. I tried using SIMGetRecordInfo, SIMReadRecord, SIMWriteRecord, etc but I still have problem after I follow a similar example given in MSDN. 

     

     

    Wednesday, April 23, 2008 9:10 AM

Answers

  • Hi Larry,

    Sorry for my late reply. yes,my name is Guang-Ming, please don't worry about it.

     

    First, you should make sure your code is right, I tried a sample, it works fine in my local:

    Code Snippet

    [DllImport("sms.dll")]

    private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimDeinitialize(IntPtr hSim );

     

    public static string GetServiceProvider()

    {

    IntPtr hSim, res;

    res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim);

    if (res != IntPtr.Zero)

    throw new Exception("Could not initialize SIM");

    SimRecord rec = new SimRecord();

    rec.cbSize = (IntPtr)Marshal.SizeOf(rec);

    res = SimGetRecordInfo(hSim, (IntPtr)SERVICE_PROVIDER, ref rec);

    if (res != IntPtr.Zero)

    throw new Exception("Could not read the service provider information from the SIM");

    byte[] bData = new byte[(int)rec.dwSize + 1];

    IntPtr dwBytesRead = IntPtr.Zero;

    res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead);

    if (res != IntPtr.Zero)

    throw new Exception("Could not read the service provider from the SIM");

    byte[] bScrubbed = new byte[(int)dwBytesRead];

    int nPos = 0;

    // Scrub the non-ascii characters

    for (int i = 0; i < (int)dwBytesRead; i ++)

    {

    if (((int)bData[i] > 19) && ((int)bData[i] < 125))

    {

    bScrubbed[nPos] = bData[i];

    nPos++;

    }

    }

    SimDeinitialize(hSim);

    return Encoding.ASCII.GetString(bScrubbed, 0, bScrubbed.Length);

    }

     

     

    Please check whether your code is right, in fact you can find completed sampe in MSDN:

    Accessing Phone APIs from the Microsoft .NET Compact Framework

     

    Secondly, yes,SimReadRecord and other API are trusted API, if your device has security restrict. you should sign your application with privileged certificate. If for test only, you can use SDK development certificate, which in pathe of D:\Program Files\Windows Mobile 6 SDK\Tools\Security\SDK Development Certificates , if for product software, you should use Mobile2Market certificate.

    For how to sign your application,please click the link below:

    http://msdn2.microsoft.com/en-us/library/bb278107.aspx

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, April 29, 2008 7:02 AM
    Moderator
  • Hi Guang-Ming,

     

    I tried with another location, it seems to give me better response. I supposed the problem might be 'resolved' for the time being, except that I can't retrieve the service provider. But thank for your help these 2 weeks.

     

    Regards,

    Larry

    Monday, May 05, 2008 2:53 AM

All replies

  • Hi Larry,

    You can use SimReadPhonebookEntry,SimGetPhonebookStatus API to get SIM contact. There is a sample here. But please pay attention to that Sim contacts index is from 1, not from 0.

     

    If it doesn't meet your requirement, please let me know what's your problem with detail information.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Friday, April 25, 2008 5:40 AM
    Moderator
  • Hi Ming Bian,

     

    Actually, I am trying to use SimGetRecordInfo, SimReadRecord, SimWriteRecord to have some access to the SIM. I am just wondering if it's possible. Example, if i change certain details/info other than phonebook contacts stored in SIM, is it possible? Do I need to deal with privileged cert since they are all privileged functions? If it is possible, how should I go about it? I tried a basic SimGetRecordInfo using C#, initially I got a return value of -2147024809 and have already resolved but now, I got a return value of -2013265814. I was wondering if there's any standard reference I can lookout for the WM6 API?

     

    Regards,

    Larry

    Monday, April 28, 2008 6:17 AM
  • Hey,

     

    I am very sorry. I think your name shoud be Guang-Ming right? I am so sorry about it.

     

    Regards,

    Larry

     

    Monday, April 28, 2008 11:58 PM
  • Hi Larry,

    Sorry for my late reply. yes,my name is Guang-Ming, please don't worry about it.

     

    First, you should make sure your code is right, I tried a sample, it works fine in my local:

    Code Snippet

    [DllImport("sms.dll")]

    private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);

    [DllImport("cellcore.dll")]

    private static extern IntPtr SimDeinitialize(IntPtr hSim );

     

    public static string GetServiceProvider()

    {

    IntPtr hSim, res;

    res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out hSim);

    if (res != IntPtr.Zero)

    throw new Exception("Could not initialize SIM");

    SimRecord rec = new SimRecord();

    rec.cbSize = (IntPtr)Marshal.SizeOf(rec);

    res = SimGetRecordInfo(hSim, (IntPtr)SERVICE_PROVIDER, ref rec);

    if (res != IntPtr.Zero)

    throw new Exception("Could not read the service provider information from the SIM");

    byte[] bData = new byte[(int)rec.dwSize + 1];

    IntPtr dwBytesRead = IntPtr.Zero;

    res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER, rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref dwBytesRead);

    if (res != IntPtr.Zero)

    throw new Exception("Could not read the service provider from the SIM");

    byte[] bScrubbed = new byte[(int)dwBytesRead];

    int nPos = 0;

    // Scrub the non-ascii characters

    for (int i = 0; i < (int)dwBytesRead; i ++)

    {

    if (((int)bData[i] > 19) && ((int)bData[i] < 125))

    {

    bScrubbed[nPos] = bData[i];

    nPos++;

    }

    }

    SimDeinitialize(hSim);

    return Encoding.ASCII.GetString(bScrubbed, 0, bScrubbed.Length);

    }

     

     

    Please check whether your code is right, in fact you can find completed sampe in MSDN:

    Accessing Phone APIs from the Microsoft .NET Compact Framework

     

    Secondly, yes,SimReadRecord and other API are trusted API, if your device has security restrict. you should sign your application with privileged certificate. If for test only, you can use SDK development certificate, which in pathe of D:\Program Files\Windows Mobile 6 SDK\Tools\Security\SDK Development Certificates , if for product software, you should use Mobile2Market certificate.

    For how to sign your application,please click the link below:

    http://msdn2.microsoft.com/en-us/library/bb278107.aspx

     

    Best regards,

    Guang-Ming Bian - MSFT

    Tuesday, April 29, 2008 7:02 AM
    Moderator
  • Hi Guang-Ming,

     

    Thank you for your reply. I have tried the above code, which I done a minor modification without the phone call part but just to retrieve the string from the public function GetServiceProvider but I got an error message. I believe that likely, it might be because the return value of the SimGetRecordInfo is a negative. For the certificate, I am using it for testing and research purposes, hence SDK development certifcates should be enough as mentioned by you. But the problem now is that, after I copy the sample cert to " My Windos Mobile-based device", using Security Configuration Manager, I see that "TEST USE ONLY- Sample Priviledged Root for Windows Mobile SDK" appears. However, probably because I am using Visual Studio 2008, I have problem using the sign file. Apparently, I am kinda stuck. I am wondering if it's due to the privilege level that I am having this problem. Please advise me. Thanks and really sorry to have bothered you these few days.

     

    Regards,

    Larry 

     

    Wednesday, April 30, 2008 2:35 AM
  • Hi Larry,

    When it appear "TEST USE ONLY- Sample Priviledged Root for Windows Mobile SDK", it show you have installed the certificate successfully. Do you sign your application, which need following steps:

     

    Firstly, To add the development certificates to your desktop computer's certificate store , do following steps:

    1. Click Start | Run.

    2. Type C:\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Tools\sdksamplePrivDeveloper.pfx, and then click OK. The Certificate Import Wizard should appear.

    3. Click Next. The path to the certificate should be filled in for the File name.

    4. Click Next.

    5. Click Next (you do not need a password).

    6. Click Next. You should leave the selected default options as they are.

    7. Click Finish.

    8. In the Security Warning message, click Yes.

    9. In the Certificate Import Wizard success message, click OK.

    10. Repeat steps 1 to 11 for SDKSampleUnPrivDeveloper.pfx.

    Secondly, do following steps to sign your application.

    1. Right click your application,Select "Property"
    2. Select "Device" tab
    3. Check "sign the project output with certificate", click "Select certificate..."
    4. Select your sdk privileged certificate

     

    VS2008 is not the problem, it is same to vs2005.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Wednesday, April 30, 2008 5:37 AM
    Moderator
  • Hi Guang-Ming,

     

    Thank you for your reply. I have tried those methods you mentioned earlier on. The Phone API sample code that I d/l from microsoft website, has GetServiceProvider Function. And I modify the code in the main form.

     

    private void Form1_Load(object sender, System.EventArgs e)

    {

    textBox1.Text = Microsoft.Wireless.Sim.GetServiceProvider();

    }

     

    And while running with the device, error message out "Exception was unhandled", I check my res value is -2147467259. The following is the part when my program stops. 

     

    res = SimGetRecordInfo(hSim, (IntPtr)SERVICE_PROVIDER, ref rec);

    if (res != IntPtr.Zero)

    throw new Exception("Could not read the service provider information from the SIM");

     

    I am just wondering what could have gone wrong? Once again, I would like to apologize for having to bother you these days.

     

    Regards,

    Larry

    Friday, May 02, 2008 3:01 AM
  • Hi Larry,

    The error seems you can't sign your application correctly, please check your right click your execute file in desktop debug folder, if you sign it, it will have a tab named "digital signituals", click it, will show certificate name.

     

    Please check it, if you don't see the tab, please sign the application. For how to sign it, I have mentioned in my previous post.

     

    Best regards,

    Guang-Ming Bian - MSFT

    Friday, May 02, 2008 10:07 AM
    Moderator
  • Hi Guang-Ming,

     

    I have checked and found that there is a tab "digital signature" and it does show the certificate name. Could be the case that certain phone does not allow the access to which service provider?

     

    Regards,

    Larry

    Sunday, May 04, 2008 11:57 PM
  • Hi Guang-Ming,

     

    I tried with another location, it seems to give me better response. I supposed the problem might be 'resolved' for the time being, except that I can't retrieve the service provider. But thank for your help these 2 weeks.

     

    Regards,

    Larry

    Monday, May 05, 2008 2:53 AM
  • Hi Larry,

    Did you ever get to solve this? I'm having the exact same problem with function SIMwriteRecord (readRecord seems to work fine for most files, though): I get error 0x80004005 (--2147467259) for every file in the card. I've tried everything Guang-Ming suggested, my application seems to be correctly signed, but still no results.

     

    Regards,

    Carolina.

     

    Monday, September 08, 2008 3:30 PM
  • I finally figured it out! The error occurs when you try to read/write a file with access policy set to something other than PIN 1: in my case, the files were protected by an ADM key. I think there was some function in the SIM Manager API to verify keys, I'll look into that...

    Regards,

    Monday, September 08, 2008 4:06 PM
  •  Hi Friends.

            My name is Ankur Ranpariya, basically I am Windows Application Developer, and I am developing Application using C# or VB.net as per client requirements.

            Now I want to develop a Windows application which will communicate directly SIM Cards and SIM Provider Services Like Messenger, Bill Payments etc. but i want to do it from Windows application, from few day I am trying to do it using Nokia 5200 Handset and Nokia PC Suite API but, I can't. When tried to search about it MSDN I just find it using Compact Framework and  Windows CE based Device.

            As per my requirecment I want to do it from Windows Application not using Windows CE Appliction.

           If Any one have idea regarding that then so please help me..... if any special device require to develop it with windows Appliction like SIM router, any spesific type of Handset or any other device then please tell me....


    Thanks gu
    Monday, May 11, 2009 9:53 AM
  • salut je suis michel et débutant  j'ai une préoccupation je veux faire programmer une carte sim de téléphone donc recuperer les infos comme l'annuere dans la sim avec java avec blackberry mon e-mail est artistesoft@hotmail.com

    X-ML

    Saturday, August 24, 2013 5:14 PM
  • hi I'm Michael and I'm starting a concern I want to program a sim card phone so as to retrieve information in annuere sim with java with my blackberry email is artistesoft@hotmail.com

    X-ML

    Saturday, August 24, 2013 5:15 PM