?? phonedailing.cs
字號(hào):
//SIM_RECORDTYPE_CYCLIC 循環(huán)記錄集,每個(gè)記錄具有相同長(zhǎng)度。
//SIM_RECORDTYPE_LINEAR 線(xiàn)性記錄集,每個(gè)記錄具有相同長(zhǎng)度。
//SIM_RECORDTYPE_MASTER 每個(gè) SIM 都有一個(gè)主記錄,實(shí)際上是主節(jié)點(diǎn)。
//SIM_RECORDTYPE_DEDICATED 實(shí)際上是作為其他記錄的上級(jí)記錄的“目錄”文件。
//dwItemCount 是記錄中的項(xiàng)數(shù)。dwSize 是記錄中每項(xiàng)的大小。接下來(lái),我們將有許多利用了 DLLImport 的函數(shù)聲明。
//[DllImport("sms.dll")]
//private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);
//檢索 SIM 所有者的電話(huà)號(hào)碼。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr
// lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);
//此函數(shù)是調(diào)用任何 SIM 訪(fǎng)問(wèn)函數(shù)所必需的。如果執(zhí)行成功,則返回一個(gè)指向 HSIM 句柄的指針,以便在隨后的調(diào)用中使用。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr
// dwAddress, ref SimRecord lpSimRecordInfo);
//此函數(shù)接受從 SimInitialize 返回的 HSIM 句柄以及一個(gè) SIM 地址 (dwAddress) 和一個(gè) SIM 記錄結(jié)構(gòu),并用請(qǐng)求的信息填充此結(jié)構(gòu)。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr
// dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData,
// IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);
//此函數(shù)接受從 SimInitialize 返回的 HSIM 句柄,以及一個(gè) SIM 地址 (dwAddress)、一個(gè) dwRecordType(請(qǐng)參見(jiàn)上表)、dwIndex(如果使用 SIM_RECORDTYPE_CYCLIC 或 SIM_RECORDTYPE_LINEAR)、lpData(指向數(shù)據(jù)緩沖區(qū))、dwBufferSize(表示緩沖區(qū)大小)以及 lpdwBytesRead(表示要讀取的字節(jié)數(shù))。
// [DllImport("cellcore.dll")]
// private static extern IntPtr SimDeinitialize(IntPtr hSim );
//此函數(shù)發(fā)布在 SimInitialize 中創(chuàng)建的 HSIM 句柄的資源。
//我們將實(shí)現(xiàn)兩種重要的電話(huà) API 調(diào)用:獲取當(dāng)前 SIM 所有者的電話(huà)號(hào)碼,并獲取 SIM 當(dāng)前訪(fǎng)問(wèn)的服務(wù)提供程序的名稱(chēng)。
//在 GetPhoneNumber 中,我們所做的只是定義一個(gè)大型字節(jié)緩沖區(qū)并將其傳入 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("無(wú)法從 SIM 中獲取電話(huà)號(hào)碼");
// MessageBox.Show("無(wú)法從 SIM 中獲取電話(huà)號(hào)碼");
// //然后獲取返回的 PhoneAddress 類(lèi)型和 PhoneAddress 電話(huà)號(hào)碼。
// byte* pCurrent = pAddr;
// phoneaddr.AddressType =
// (AddressType)Marshal.ReadInt32((IntPtr)pCurrent);
// pCurrent += Marshal.SizeOf(phoneaddr.AddressType);
// phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent);
// }
//}
//此函數(shù)聲明為 unsafe,因?yàn)槲覀兪侵苯訌膬?nèi)存中的托管代碼讀取。
//在下一個(gè)函數(shù) GetServiceProvider 中,我們首先初始化 SIM
// res = SimInitialize(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out
// hSim);
// if (res != IntPtr.Zero)
// throw new Exception("無(wú)法初始化 SIM");
//然后聲明一個(gè) SIM 記錄結(jié)構(gòu)實(shí)例,并請(qǐng)求在調(diào)用 SimGetRecordInfo 時(shí)檢索包含 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("無(wú)法從 SIM 中讀取服務(wù)提供程序的信息");
//然后,我們分配一個(gè)字節(jié)數(shù)組,用于存儲(chǔ) SIM 記錄(包含 SERVICE_PROVIDER 詳細(xì)信息)的內(nèi)容。
// byte[] bData = new byte[(int)rec.dwSize + 1];
// IntPtr dwBytesRead = IntPtr.Zero;
//然后請(qǐng)求讀取 SERVICE_PROVIDER 記錄的內(nèi)容。
// res = SimReadRecord(hSim, (IntPtr)SERVICE_PROVIDER,
// rec.dwRecordType, IntPtr.Zero, bData, (IntPtr)bData.Length, ref
// dwBytesRead);
// if (res != IntPtr.Zero)
// throw new Exception("無(wú)法從 SIM 中讀取服務(wù)提供程序");
//然后查看返回的結(jié)構(gòu),并刪除在顯示最終結(jié)果時(shí)可能會(huì)破壞代碼的任何非標(biāo)準(zhǔn) 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);
//最后,返回“已刪除”字節(jié)緩沖區(qū)的字符串表示形式。
//使用 VB.NET 訪(fǎng)問(wèn) SIM 信息
//初始類(lèi)聲明與 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 函數(shù)相當(dāng)于 C# 實(shí)現(xiàn)。首先創(chuàng)建緩沖區(qū)空間,并使用 P/Invoke 調(diào)用 SmsGetPhoneNumber 函數(shù)。
// 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("無(wú)法從 SIM 中獲取電話(huà)號(hào)碼")
// End If
//然后從返回的結(jié)構(gòu)中獲取地址類(lèi)型信息。
// phoneaddr.AddressType =
// System.Runtime.InteropServices.Marshal.ReadInt32(ipAddr)
//將返回的電話(huà)號(hào)碼緩沖區(qū)轉(zhuǎn)換為一個(gè)字符串,然后返回完整的 PhoneAddress 結(jié)構(gòu)。
//GetServiceProvider 函數(shù)的情況也與 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("無(wú)法初始化 SIM。")
// End If
//首先初始化 SIM,以便從中檢索數(shù)據(jù)。
// 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))
//創(chuàng)建一個(gè)新的 SimRecord 結(jié)構(gòu)實(shí)例且只設(shè)置 cbSize 成員(并用 SimRecord 結(jié)構(gòu)的大小填充該成員)。
// res = SimGetRecordInfo(hSim, IntPtr.op_Explicit(SERVICE_PROVIDER), rec)
// If (res.ToInt32 <> 0) Then
// Throw New Exception("無(wú)法從 SMS 中讀取服務(wù)提供程序的
//信息。")
// End If
//調(diào)用 SimGetRecordInfo,獲取包含 SERVICE_PROVIDER 數(shù)據(jù)的 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("無(wú)法從 SMS 中讀取服務(wù)提供程序。")
//End If
//然后,正如在 C# 代碼中執(zhí)行的操作一樣,必須從產(chǎn)生的字節(jié)緩沖區(qū)中刪除任何非 ASCII 字符,然后轉(zhuǎn)換為字符串并返回該字符串值。
// 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# 從代碼中創(chuàng)建電話(huà)呼叫,請(qǐng)調(diào)用:
//Microsoft.Wireless.Phone.MakeCall("電話(huà)號(hào)碼");
//要使用 C# 從代碼中獲取 SIM 用戶(hù)的電話(huà)號(hào)碼,請(qǐng)調(diào)用:
//Microsoft.Wireless.Sim.GetPhoneNumber()(該函數(shù)返回一個(gè)字符串)
//要使用 C# 從代碼中獲取 SIM 用戶(hù)的服務(wù)提供程序,請(qǐng)調(diào)用:
//Microsoft.Wireless.Sim.GetServiceProvider()(該函數(shù)返回一個(gè)字符串)
//要使用 VB.NET 從代碼中創(chuàng)建電話(huà)呼叫,請(qǐng)調(diào)用:
//Phone.MakeCall("電話(huà)號(hào)碼");
//要使用 VB.NET 從代碼中獲取 SIM 用戶(hù)的電話(huà)號(hào)碼,請(qǐng)調(diào)用:
//Sim.GetPhoneNumber()(該函數(shù)返回一個(gè)字符串)
//要使用 VB.NET 從代碼中獲取 SIM 用戶(hù)的服務(wù)提供程序,請(qǐng)調(diào)用:
//Sim.GetServiceProvider()(該函數(shù)返回一個(gè)字符串)
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -