none
Pointers and fixed size buffers only be used in unsafe context .

    Question

  • Hi ,

     

          For getting mobile number in windows mobile emulators i wrote the following code .but it giving error like "Pointers and fixed size buffers only be used in unsafe context".Please suggest how to Correct this .And suggest it's the code is working for to get the mobile number from windows mobile emulators .

     

                Byte[] buffer = new Byte[516];

     

                fixed (byte* pAddr = buffer)

                {

                    IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr);

                    if (res != IntPtr.Zero)

                        throw new Exception("Could not get phone number from SIM");

                    byte* pCurrent = pAddr;

                    phoneaddr.AddressType =

                      (AddressType)Marshal.ReadInt32((IntPtr)pCurrent);

                    pCurrent += Marshal.SizeOf(phoneaddr.AddressType);

                    phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent);

                }

    Wednesday, April 20, 2011 6:53 AM

Answers

  •  Microsoft has an article describing how to access SIM information in C# and VB.NET using those APIs here: http://msdn.microsoft.com/en-us/library/aa446543.aspx

    The article does say "This function is declared unsafe as we're directly reading from memory from managed code", but they leave out the actual function declaration. You just have to add the "unsafe" keyword to your function declaration as shown here: http://msdn.microsoft.com/en-us/library/chfa2zb8%28v=vs.71%29.aspx

    -PaulH

    Wednesday, April 20, 2011 2:31 PM