从MS .NET CF版访问电话API(完整版) (转载)
以前查找了好些在.net CF框架上調用設備本地API函數讀取手機SIM卡的資料,但各種資料都少了些描述,特別是少了一些引用的結構(struct),造成了資料中的原代碼不能直接運行,讓很多學習的朋友遇到很多麻煩,花費了多余的時間,走了一些彎路。 我也遇到了類似的問題,通過補充了一些缺失的代碼后,使程序可以正常的讀取SIM卡了。 SIM 卡 Pocket PC Phone 的內置電話功能使它成為移動電話家族的重要成員,在創建新的連接應用程序時,可以利用該功能。與所有移動電話一樣,Pocket PC 電話也需要用戶身份模塊 (SIM) 卡才能撥打電話(緊急電話除外,撥打緊急電話時無需 SIM 卡)。例如,連接應用程序可以通過在啟動連接之前檢查用戶的個人識別碼 (PIN) 代碼來利用 SIM 卡的安全功能。 SIM 卡可以存儲移動電話的電話號碼以及提供此號碼的操作員的身份標識。另外,它還可以存儲附加有姓名的電話號碼以及多條短消息服務的消息。SIM 卡不僅具有存儲器,還具有使卡可以進行安全和加密處理的中央處理器。通常情況下,您需要使用 PIN 代碼“登錄”到 SIM 卡。 SIM 管理器 API 在 Pocket PC Phone 中,您可以使用一系列 Windows CE API 調用(統稱為 SIM 管理器)來訪問 SIM 卡上的信息。 會話以調用 SimInitialize 開始,這將返回一個 SIM 句柄,此后,在調用 SIM 管理器 API 函數時均需要此句柄。通過將此句柄傳遞給 SimDeinitialize 來終止會話。 使用 Compact Framework 調用 Windows API 使用 SDE (Smart Device Extensions) 和 Compact Framework,可以進行 Microsoft? Windows? API 調用,例如使用 Interop(erability) 服務訪問 SIM 管理器 API。 SIM Anyplace 示例 該示例是使用 Microsoft Visual Studio? .NET、C#、SDE 和 .NET CF 創建的 Pocket PC Phone 的示例應用程序。它展示了如何使用 SIM 管理器 API 訪問 SIM 卡。該應用程序包含一個窗體: 此示例的用途僅限于通過敲擊“獲取 SIM 信息”按鈕從 SIM 卡獲取一般信息。但是,通過使用此示例的結構可以將其用途擴展至包含更多的 SIM 管理器 API 功能。 代碼演練 要使用 Compact Framework 的 Interop 服務,需要添加以下代碼: using System.Runtime.InteropServices; 創建類 SIMWrap 來存儲 Windows API 的原型,該示例需要以下原型: 、
?
代碼 public const int SIM_CAPSTYPE_ALL = 0x3F; // 所有聯系人public const int SIM_PBSTORAGE_SIM = 0x10; //
public const int SIM_SMSSTORAGE_SIM = 0x2; //
[DllImport("cellcore.dll")]
public static extern int SimInitialize(uint dwFlags,
int lpfnCallBack, uint dwParam, ref int lphSim);
[DllImport("cellcore.dll")]
public static extern int SimGetPhonebookStatus(int hSim,
uint dwLocation, ref uint lpdwUsed, ref uint lpdwTotal);
[DllImport("cellcore.dll")]
public static extern int SimGetDevCaps(int hSim,
uint dwCapsType, ref SimCaps lpSimCaps);
[DllImport("cellcore.dll")]
public static extern int SimGetSmsStorageStatus(int hSim,
uint dwStorage, ref uint lpdwUsed, ref uint lpdwTotal);
[DllImport("cellcore.dll")]
public static extern int SimDeinitialize(int hSim);
[DllImport("cellcore.dll")]
public static extern int SimReadPhonebookEntry(int hSim, uint dwLocation, uint dwIndex, ref SIMPHONEBOOKENTRY entry);
[StructLayout(LayoutKind.Sequential)]
public struct SimCaps
{
public uint cbSize;
public uint dwParams;
public uint dwPBStorages;
public uint dwMinPBIndex;
public uint dwMaxPBIndex;
public uint dwMaxPBEAddressLength;
public uint dwMaxPBETextLength;
public uint dwLockFacilities;
public uint dwReadMsgStorages;
public uint dwWriteMsgStorages;
public uint dwNumLockingPwdLengths;
public SimLockingPwdLength rgLockingPwdLengths0;
public SimLockingPwdLength rgLockingPwdLengths1;
public SimLockingPwdLength rgLockingPwdLengths2;
public SimLockingPwdLength rgLockingPwdLengths3;
public SimLockingPwdLength rgLockingPwdLengths4;
public SimLockingPwdLength rgLockingPwdLengths5;
public SimLockingPwdLength rgLockingPwdLengths6;
public SimLockingPwdLength rgLockingPwdLengths7;
public SimLockingPwdLength rgLockingPwdLengths8;
public SimLockingPwdLength rgLockingPwdLengths9;
}
//很多文章都缺失的結構
[StructLayout(LayoutKind.Sequential)]
public struct SimLockingPwdLength
{
public uint dwFacility;
public uint dwPasswordLength;
}
//很多文章都缺失的結構
[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; // 聯系人姓名
}
/// <summary>
/// 獲取SIM卡聯系人信息
/// </summary>
/// <returns></returns>
public static List<string[]> GetSIMContactList()
{
int hSim = 0;
List<string[]> list = new List<string[]>();
try
{
int result = SimInitialize(0, 0, 0, ref hSim);
if (result != 0)
throw new Exception("SIM打卡失敗,請檢測SIM是否安裝!");
uint uiUsed = 0;
uint uiTotal = 0;
result = SimGetPhonebookStatus(hSim, SIM_PBSTORAGE_SIM, ref uiUsed, ref uiTotal);
for (int i = 1; i <= uiUsed; i++)
{
SIMPHONEBOOKENTRY entry = new SIMPHONEBOOKENTRY();
entry.cbSize = (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
result = SimReadPhonebookEntry(hSim, SIM_PBSTORAGE_SIM, (uint)i, ref entry);
list.Add(new string[2] { entry.lpszText.Trim(), entry.lpszAddress.Trim() });
}
return list;
}
catch
{
throw;
}
finally
{
SimDeinitialize(hSim);
}
}
?
?
聲明適當時,“獲取 SIM 信息”按鈕所表示的代碼如下所示: dataGrid1為列表控件
?
代碼 List<string[]> list = SIMWrap.GetSIMContactList();DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("姓名", typeof(string)));
dt.Columns.Add(new DataColumn("號碼", typeof(string)));
string[] str;
for(int i = 0; i < list.Count; i++)
{
str = list[i];
DataRow dr = dt.NewRow();
dr[0] = str[0].ToString();
dr[1] = str[1].ToString();
dt.Rows.Add(dr);
}
dataGrid1.DataSource = dt;
?
?
小結 由于 Pocket PC Phone 是移動電話家族的重要成員,因此在創建大型的連接應用程序時可以利用其功能,例如 SIM 卡。使用 SIM 管理器 API、Compact Framework 的 Interop 服務以及 .NET 開發環境,可以獲得實現這一功能的工具。
轉載于:https://www.cnblogs.com/Wolves/archive/2010/12/03/1895533.html
總結
以上是生活随笔為你收集整理的从MS .NET CF版访问电话API(完整版) (转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度宣布与天津市进行大模型战略合作 共建
- 下一篇: 《Advanced .NET Debug