📄 phonedailing.cs
字号:
//SIM_RECORDTYPE_CYCLIC 循环记录集,每个记录具有相同长度。
//SIM_RECORDTYPE_LINEAR 线性记录集,每个记录具有相同长度。
//SIM_RECORDTYPE_MASTER 每个 SIM 都有一个主记录,实际上是主节点。
//SIM_RECORDTYPE_DEDICATED 实际上是作为其他记录的上级记录的“目录”文件。
//dwItemCount 是记录中的项数。dwSize 是记录中每项的大小。接下来,我们将有许多利用了 DLLImport 的函数声明。
//[DllImport("sms.dll")]
//private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);
//检索 SIM 所有者的电话号码。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr
// lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);
//此函数是调用任何 SIM 访问函数所必需的。如果执行成功,则返回一个指向 HSIM 句柄的指针,以便在随后的调用中使用。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr
// dwAddress, ref SimRecord lpSimRecordInfo);
//此函数接受从 SimInitialize 返回的 HSIM 句柄以及一个 SIM 地址 (dwAddress) 和一个 SIM 记录结构,并用请求的信息填充此结构。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr
// dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData,
// IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);
//此函数接受从 SimInitialize 返回的 HSIM 句柄,以及一个 SIM 地址 (dwAddress)、一个 dwRecordType(请参见上表)、dwIndex(如果使用 SIM_RECORDTYPE_CYCLIC 或 SIM_RECORDTYPE_LINEAR)、lpData(指向数据缓冲区)、dwBufferSize(表示缓冲区大小)以及 lpdwBytesRead(表示要读取的字节数)。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimDeinitialize(IntPtr hSim );
//此函数发布在 SimInitialize 中创建的 HSIM 句柄的资源。
//我们将实现两种重要的电话 API 调用:获取当前 SIM 所有者的电话号码,并获取 SIM 当前访问的服务提供程序的名称。
//在 GetPhoneNumber 中,我们所做的只是定义一个大型字节缓冲区并将其传入 SmsGetPhoneNumber。
//public unsafe string 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("无法从 SIM 中获取电话号码");
// MessageBox.Show("无法从 SIM 中获取电话号码");
// //然后获取返回的 PhoneAddress 类型和 PhoneAddress 电话号码。
// byte* pCurrent = pAddr;
// phoneaddr.AddressType =
// (AddressType)Marshal.ReadInt32((IntPtr)pCurrent);
// pCurrent += Marshal.SizeOf(phoneaddr.AddressType);
// phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent);
// }
//}
//此函数声明为 unsafe,因为我们是直接从内存中的托管代码读取。
//在下一个函数 GetServiceProvider 中,我们首先初始化 SIM
// res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out
// hSim);
// if (res != IntPtr.Zero)
// throw new Exception("无法初始化 SIM");
//然后声明一个 SIM 记录结构实例,并请求在调用 SimGetRecordInfo 时检索包含 SERVICE_PROVIDER 的值的 SimRecord 的句柄。
// 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("无法从 SIM 中读取服务提供程序的信息");
//然后,我们分配一个字节数组,用于存储 SIM 记录(包含 SERVICE_PROVIDER 详细信息)的内容。
// byte[] bData = new byte[(int)rec.dwSize + 1];
// IntPtr dwBytesRead = IntPtr.Zero;
//然后请求读取 SERVICE_PROVIDER 记录的内容。
// res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER,
// rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref
// dwBytesRead);
// if (res != IntPtr.Zero)
// throw new Exception("无法从 SIM 中读取服务提供程序");
//然后查看返回的结构,并删除在显示最终结果时可能会破坏代码的任何非标准 ASCII 字符。
// byte[] bScrubbed = new byte[(int)dwBytesRead];
// int nPos = 0;
// // 删除非 ASCII 字符
// for (int i = 0; i < (int)dwBytesRead; i ++)
// {
// if (((int)bData[i] > 19) && ((int)bData[i] < 125))
// {
// bScrubbed[nPos] = bData[i];
// nPos++;
// }
// }
//然后取消初始化 SIM 并释放其资源。
// SimDeinitialize(hSim);
//最后,返回“已删除”字节缓冲区的字符串表示形式。
//使用 VB.NET 访问 SIM 信息
//初始类声明与 C# 中的声明相同:
// Private Shared SERVICE_PROVIDER As Long = &H6F46
// <StructLayout(LayoutKind.Sequential)> _
// Public Structure SimRecord
// Public cbSize As IntPtr
// Public dwParams As IntPtr
// Public dwRecordType As IntPtr
// Public dwItemCount As IntPtr
// Public dwSize As IntPtr
// End Structure
// <System.Runtime.InteropServices.DllImport("sms.dll")> _
//Private Shared Function SmsGetPhoneNumber(ByVal psmsaAddress As IntPtr) As
// IntPtr
// End Function
// <System.Runtime.InteropServices.DllImport("cellcore.dll")> _
//Private Shared Function SimInitialize(ByVal dwFlags As IntPtr, ByVal
// lpfnCallBack As IntPtr, ByVal dwParam As IntPtr, ByRef lphSim As IntPtr)
// As IntPtr
// End Function
// <System.Runtime.InteropServices.DllImport("cellcore.dll")> _
//Private Shared Function SimGetRecordInfo(ByVal hSim As IntPtr, ByVal
// dwAddress As IntPtr, ByRef lpSimRecordInfo As SimRecord) As IntPtr
// End Function
// <System.Runtime.InteropServices.DllImport("cellcore.dll")> _
//Private Shared Function SimReadRecord(ByVal hSim As IntPtr, ByVal
// dwAddress As IntPtr, ByVal dwRecordType As IntPtr, _
//ByVal dwIndex As IntPtr, ByVal lpData() As Byte, ByVal dwBufferSize As
// IntPtr, ByRef lpdwBytesRead As IntPtr) As IntPtr
// End Function
// <System.Runtime.InteropServices.DllImport("cellcore.dll")> _
//Private Shared Function SimDeinitialize(ByVal hSim As IntPtr) As IntPtr
// End Function
//GetPhoneNumber 函数相当于 C# 实现。首先创建缓冲区空间,并使用 P/Invoke 调用 SmsGetPhoneNumber 函数。
// Dim phoneaddr As PhoneAddress = New PhoneAddress
// Dim buffer(512) As Byte
// Dim pAddr() As Byte = buffer
// Dim ipAddr As IntPtr = Marshal.AllocHLocal(pAddr.Length)
// Dim res As IntPtr = IntPtr.Zero
// Try
// res = SmsGetPhoneNumber(ipAddr)
// Catch ex As Exception
// MessageBox.Show(ex.Message)
// End Try
// If (res.ToInt32 <> 0) Then
// Throw New Exception("无法从 SIM 中获取电话号码")
// End If
//然后从返回的结构中获取地址类型信息。
// phoneaddr.AddressType =
// System.Runtime.InteropServices.Marshal.ReadInt32(ipAddr)
//将返回的电话号码缓冲区转换为一个字符串,然后返回完整的 PhoneAddress 结构。
//GetServiceProvider 函数的情况也与 C# 版本中的情况非常相似。
// Dim hSim, res As IntPtr
// hSim = IntPtr.Zero
// Dim temp As Long
// res = SimInitialize(IntPtr.Zero, Nothing, IntPtr.Zero, hSim)
// If (res.ToInt32 <> 0) Then
// Throw New Exception("无法初始化 SIM。")
// End If
//首先初始化 SIM,以便从中检索数据。
// Dim rec As SimRecord = New SimRecord
// rec.cbSize =
// Marshal.AllocHLocal(System.Runtime.InteropServices
// .Marshal.SizeOf(temp))
// rec.cbSize = IntPtr.op_Explicit(System.Runtime.InteropServices
// .Marshal.SizeOf(rec))
//创建一个新的 SimRecord 结构实例且只设置 cbSize 成员(并用 SimRecord 结构的大小填充该成员)。
// res = SimGetRecordInfo(hSim, IntPtr.op_Explicit(SERVICE_PROVIDER), rec)
// If (res.ToInt32 <> 0) Then
// Throw New Exception("无法从 SMS 中读取服务提供程序的
//信息。")
// End If
//调用 SimGetRecordInfo,获取包含 SERVICE_PROVIDER 数据的 SIM 记录的句柄。
// Dim bData((rec.dwSize).ToInt32 + 1) As Byte
// Dim dwBytesRead As IntPtr = IntPtr.Zero
// res = SimReadRecord(hSim, IntPtr.op_Explicit(SERVICE_PROVIDER),
// rec.dwRecordType, IntPtr.Zero, bData,
// IntPtr.op_Explicit(bData.Length), dwBytesRead)
// If (res.ToInt32 <> 0) Then
// Throw New Exception("无法从 SMS 中读取服务提供程序。")
//End If
//然后,正如在 C# 代码中执行的操作一样,必须从产生的字节缓冲区中删除任何非 ASCII 字符,然后转换为字符串并返回该字符串值。
// Dim bScrubbed(dwBytesRead.ToInt32) As Byte
// Dim nPos As Int32 = 0
// Dim i As Int32
// '删除非 ASCII 字符
// For i = 0 To dwBytesRead.ToInt32
// If bData(i) > 19 And bData(i) < 125 Then
// bScrubbed(nPos) = bData(i)
// nPos = nPos + 1
// End If
// Next i
// SimDeinitialize(hSim)
// Return System.Text.ASCIIEncoding.ASCII.GetString(bScrubbed, 0,
// bScrubbed.Length)
//代码使用
//要使用 C# 从代码中创建电话呼叫,请调用:
//Microsoft.Wireless.Phone.MakeCall("电话号码");
//要使用 C# 从代码中获取 SIM 用户的电话号码,请调用:
//Microsoft.Wireless.Sim.GetPhoneNumber()(该函数返回一个字符串)
//要使用 C# 从代码中获取 SIM 用户的服务提供程序,请调用:
//Microsoft.Wireless.Sim.GetServiceProvider()(该函数返回一个字符串)
//要使用 VB.NET 从代码中创建电话呼叫,请调用:
//Phone.MakeCall("电话号码");
//要使用 VB.NET 从代码中获取 SIM 用户的电话号码,请调用:
//Sim.GetPhoneNumber()(该函数返回一个字符串)
//要使用 VB.NET 从代码中获取 SIM 用户的服务提供程序,请调用:
//Sim.GetServiceProvider()(该函数返回一个字符串)
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -