从MS .NET CF版访问电话API(完整版) (转载)
以前查找了好些在.net CF框架上調(diào)用設(shè)備本地API函數(shù)讀取手機(jī)SIM卡的資料,但各種資料都少了些描述,特別是少了一些引用的結(jié)構(gòu)(struct),造成了資料中的原代碼不能直接運(yùn)行,讓很多學(xué)習(xí)的朋友遇到很多麻煩,花費(fèi)了多余的時(shí)間,走了一些彎路。 我也遇到了類似的問題,通過補(bǔ)充了一些缺失的代碼后,使程序可以正常的讀取SIM卡了。 SIM 卡 Pocket PC Phone 的內(nèi)置電話功能使它成為移動(dòng)電話家族的重要成員,在創(chuàng)建新的連接應(yīng)用程序時(shí),可以利用該功能。與所有移動(dòng)電話一樣,Pocket PC 電話也需要用戶身份模塊 (SIM) 卡才能撥打電話(緊急電話除外,撥打緊急電話時(shí)無需 SIM 卡)。例如,連接應(yīng)用程序可以通過在啟動(dòng)連接之前檢查用戶的個(gè)人識(shí)別碼 (PIN) 代碼來利用 SIM 卡的安全功能。 SIM 卡可以存儲(chǔ)移動(dòng)電話的電話號(hào)碼以及提供此號(hào)碼的操作員的身份標(biāo)識(shí)。另外,它還可以存儲(chǔ)附加有姓名的電話號(hào)碼以及多條短消息服務(wù)的消息。SIM 卡不僅具有存儲(chǔ)器,還具有使卡可以進(jìn)行安全和加密處理的中央處理器。通常情況下,您需要使用 PIN 代碼“登錄”到 SIM 卡。 SIM 管理器 API 在 Pocket PC Phone 中,您可以使用一系列 Windows CE API 調(diào)用(統(tǒng)稱為 SIM 管理器)來訪問 SIM 卡上的信息。 會(huì)話以調(diào)用 SimInitialize 開始,這將返回一個(gè) SIM 句柄,此后,在調(diào)用 SIM 管理器 API 函數(shù)時(shí)均需要此句柄。通過將此句柄傳遞給 SimDeinitialize 來終止會(huì)話。 使用 Compact Framework 調(diào)用 Windows API 使用 SDE (Smart Device Extensions) 和 Compact Framework,可以進(jìn)行 Microsoft? Windows? API 調(diào)用,例如使用 Interop(erability) 服務(wù)訪問 SIM 管理器 API。 SIM Anyplace 示例 該示例是使用 Microsoft Visual Studio? .NET、C#、SDE 和 .NET CF 創(chuàng)建的 Pocket PC Phone 的示例應(yīng)用程序。它展示了如何使用 SIM 管理器 API 訪問 SIM 卡。該應(yīng)用程序包含一個(gè)窗體: 此示例的用途僅限于通過敲擊“獲取 SIM 信息”按鈕從 SIM 卡獲取一般信息。但是,通過使用此示例的結(jié)構(gòu)可以將其用途擴(kuò)展至包含更多的 SIM 管理器 API 功能。 代碼演練 要使用 Compact Framework 的 Interop 服務(wù),需要添加以下代碼: using System.Runtime.InteropServices; 創(chuàng)建類 SIMWrap 來存儲(chǔ) Windows API 的原型,該示例需要以下原型: 、
?
代碼 public const int SIM_CAPSTYPE_ALL = 0x3F; // 所有聯(lián)系人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;
}
//很多文章都缺失的結(jié)構(gòu)
[StructLayout(LayoutKind.Sequential)]
public struct SimLockingPwdLength
{
public uint dwFacility;
public uint dwPasswordLength;
}
//很多文章都缺失的結(jié)構(gòu)
[StructLayout(LayoutKind.Sequential)]
public struct SIMPHONEBOOKENTRY
{
public uint cbSize; //
public uint dwParams; //
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszAddress; // 聯(lián)系人電話
public uint dwAddressType; //
public uint dwNumPlan; //
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string lpszText; // 聯(lián)系人姓名
}
/// <summary>
/// 獲取SIM卡聯(lián)系人信息
/// </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打卡失敗,請(qǐng)檢測(cè)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);
}
}
?
?
聲明適當(dāng)時(shí),“獲取 SIM 信息”按鈕所表示的代碼如下所示: dataGrid1為列表控件
?
代碼 List<string[]> list = SIMWrap.GetSIMContactList();DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("姓名", typeof(string)));
dt.Columns.Add(new DataColumn("號(hào)碼", 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;
?
?
小結(jié) 由于 Pocket PC Phone 是移動(dòng)電話家族的重要成員,因此在創(chuàng)建大型的連接應(yīng)用程序時(shí)可以利用其功能,例如 SIM 卡。使用 SIM 管理器 API、Compact Framework 的 Interop 服務(wù)以及 .NET 開發(fā)環(huán)境,可以獲得實(shí)現(xiàn)這一功能的工具。
轉(zhuǎn)載于:https://www.cnblogs.com/Wolves/archive/2010/12/03/1895533.html
總結(jié)
以上是生活随笔為你收集整理的从MS .NET CF版访问电话API(完整版) (转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度宣布与天津市进行大模型战略合作 共建
- 下一篇: 一加 Ace 2 手机开始推送安卓 13