windows mobile取手机号的实例

调用:

string strNumber = Microsoft.Wireless.Sim.GetPhoneNumber().Address;

 

Phone.cs

 

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Microsoft.Wireless
{
 /// <summary>
 /// Identifies the phone number type specified.
 /// </summary>
 public enum AddressType
 {
  /// <summary>Unknown phone number type.</summary>
  Unknown,
  /// <summary>International phone number.</summary>
  International,
  /// <summary>National phone number.</summary>
  National,
  /// <summary>Network-specific phone number.</summary>
  NetworkSpecific,
  /// <summary>Subscriber phone number.</summary>
  Subscriber,
  /// <summary>Alphanumeric phone number.</summary>
  Alphanumeric,
  /// <summary>Abbreviated phone number.</summary>
  Abbreviated
 }

 /// <summary>
 /// Information about the phone number.
 /// </summary>
 public struct PhoneAddress
 {
  /// <summary>The address type.</summary>
  public AddressType AddressType;
  /// <summary>The phone number in string format.</summary>
  public String Address;
 }

 /// <summary>
 /// Phone control.
 /// </summary>
 public class Phone
 {
  private static long PMCF_DEFAULT    = 0x00000001;
  private static long PMCF_PROMPTBEFORECALLING = 0x00000002;

  private struct PhoneMakeCallInfo
  {
   public IntPtr cbSize;
   public IntPtr dwFlags;
   public IntPtr pszDestAddress;
   public IntPtr pszAppName;
   public IntPtr pszCalledParty;
   public IntPtr pszComment;
  }

  [DllImport("phone.dll")]
  private static extern IntPtr PhoneMakeCall(ref PhoneMakeCallInfo ppmci);

  /// <summary>
  /// Dials the specified phone number.
  /// </summary>
  /// <param name="PhoneNumber">Phone number to dial.</param>
  public static void MakeCall(string PhoneNumber)
  {
   MakeCall(PhoneNumber, false);
  }

  /// <summary>
  /// Dials the specified phone number.
  /// </summary>
  /// <param name="PhoneNumber">Phone number to dial.</param>
  /// <param name="PromptBeforeCall">Prompts the user before the call is placed.</param>
  unsafe public static void MakeCall(string PhoneNumber, bool PromptBeforeCall)
  {
   IntPtr res;

   PhoneNumber += '/0';
   char[] cPhoneNumber = PhoneNumber.ToCharArray();

   fixed (char* pAddr = cPhoneNumber)
   {
    PhoneMakeCallInfo info = new PhoneMakeCallInfo();
    info.cbSize = (IntPtr)Marshal.SizeOf(info);
    info.pszDestAddress = (IntPtr)pAddr;
    
    if (PromptBeforeCall)
    {
     info.dwFlags = (IntPtr)PMCF_PROMPTBEFORECALLING;
    }
    else
    {
     info.dwFlags = (IntPtr)PMCF_DEFAULT;
    }

    res = PhoneMakeCall(ref info);
   }
  }
 }

 /// <summary>
 /// Reads information from the Subscriber Identity Module (SIM)
 /// </summary>
 public class Sim
 {
  private static long SERVICE_PROVIDER = 0x00006F46;

  [StructLayout(LayoutKind.Sequential)]
  private struct SimRecord
  {
   public IntPtr cbSize;
   public IntPtr dwParams;
   public IntPtr dwRecordType;
   public IntPtr dwItemCount;
   public IntPtr dwSize;
  }

  [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 );

  /// <summary>
  /// Gets the phone number from the SIM.
  /// </summary>
  /// <returns>PhoneAddress structure with phone number.</returns>
  unsafe public static PhoneAddress GetPhoneNumber()
  {
   PhoneAddress phoneaddr = new PhoneAddress();

   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);
   }

   return phoneaddr;
  }

  /// <summary>
  /// Gets the current wireless carriers network name from the SIM.
  /// </summary>
  /// <returns>The carrier description.</returns>
  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);
  }
 }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值