none
Writing to SIM card nightmare RRS feed

  • Question

  • I hope someone can help me, because I'm finally losing my mind!

     

    I'm trying to write new contacts (name and number) to the SIM card in Windows Mobile 5 using C#. My code executes without any errors/exceptions, but it is just simply not writing the number to the SIM card.

     

    At one stage I thought that maybe I created the wrong kind of Project, but other functions in my application includes reading the SIM card and making phone calls, and that all works fine. It's just writing to the SIM that does not work.

     

    Scenario:

    I am using Visual Studio 2005.

    Created a project for C# --> Smart Device --> Windows Mobile 5.0 Pocket PC

    Using emulator:Windows Mobile 5.0 Pocket PC phone

     

    <code to follow>

    Create a SIM wrapper that includes some statics, struct and API calls

    Call the wrapper from a function.

    The function executes without any errors, but it just simply does not write the values to the SIM.

     

    Can anyone spot my mistake?

     

    Thanks!!!

     

    public class SimCardWrapper
    {
     public const int SIM_CAPSTYPE_ALL = 0x3F;       // All device cabability types
     public const int SIM_PBSTORAGE_SIM = 0x10;      // General SIM Storage
     public const int SIM_SMSSTORAGE_SIM = 0x2;      // SIM storage location


     [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 SimDeinitialize(int hSim);

     [DllImport("cellcore.dll")]
     public static extern int SimGetPhonebookStatus(int hSim, uint dwLocation, ref uint lpdwUsed, ref uint lpdwTotal);


     [DllImport("cellcore.dll")]
     public static extern IntPtr SimWritePhonebookEntry(int hSim, uint dwLocation, uint dwIndex, ref SimPhoneBookEntry lpPhonebookEntry);

    }

     


    public void TestWrite()
    {
     uint phoneUsed = 0;
     uint phoneTotal = 0;
     int hSim = 0;


     SimCardWrapper.SimInitialize(0, 0, 0, ref hSim);

     SimCardWrapper.SimGetPhonebookStatus(hSim, SimCardWrapper.SIM_PBSTORAGE_SIM, ref phoneUsed, ref phoneTotal);

     SimCardWrapper.SimPhoneBookEntry phonebookEntry = new SimCardWrapper.SimPhoneBookEntry();

     

     phonebookEntry.cbSize = 1040;
     phonebookEntry.dwAddressType = 0;
     phonebookEntry.dwNumPlan = 1;
     phonebookEntry.dwParams = 15;
     phonebookEntry.lpszAddress = "123456";
     phonebookEntry.lpszText = "TestName TestSurname";

     SimCardWrapper.SimWritePhonebookEntry(hSim, SimCardWrapper.SIM_PBSTORAGE_SIM, phoneUsed, ref phonebookEntry);

     

    //I played around with the phoneUsed variable, making it more and less, that instructs at what index to insert, but nothing works. I even tried the value 0xffffffff which is supposed to automatically insert it at the next available index, but nothing works


     SimCardWrapper.SimDeinitialize(hSim);

    }

       

     


     

     

     

    Thursday, August 16, 2007 4:05 PM

Answers

  • Are you sure that there are no errors? You don´t check the Returncodes in your testcode.

    You should be aware that "phoneUsed==0" will return SIM_E_INVALIDINDEX when you call SimWritePhonebookEntry.

    You can try to put the dwNumPlan to SIM_NUMPLAN_UNKNOWN.

    I also had problems with the lpszText and multiple words.

    I changed your code to that:

     

    phonebookEntry.cbSize = 1040;

    phonebookEntry.dwAddressType = 0;

    phonebookEntry.dwNumPlan = 0;

    phonebookEntry.dwParams = 15;

    phonebookEntry.lpszAddress = "1666911";

    phonebookEntry.lpszText = "test";

    phoneUsed = phoneUsed + 1;

     

    and it worked.

    I´m not that good in managed code, so i don´t know why it does not work with "test test". Maybe some string converting fails.

    I had no problems using native code, expect of the definition of the Number Plan and Adress Type values so it must be some P/ivoke or converting issue.

    Monday, August 20, 2007 7:04 AM