📄 sim programming with the _net compact framework.mht
字号:
<H2 class=3DdtH1><A name=3Dsim_programming_topic3><!----></A>Calling =
Windows API=20
Using Compact Framework</H2>
<P>With the <A id=3Dctl00_rs1_mainContentContainer_ctl02=20
onclick=3D"javascript:Track('ctl00_rs1_mainContentContainer_ctl00|ctl00_r=
s1_mainContentContainer_ctl02',this);"=20
href=3D"http://msdn.microsoft.com/vstudio/device/smartdev.asp">SDE =
(Smart Device=20
Extensions) and Compact Framework</A> we can make Microsoft=C2=AE =
Windows=C2=AE API calls,=20
such as accessing the SIM Manager API using the Interop(erability) =
Services.</P>
<H2 class=3DdtH1><A name=3Dsim_programming_topic4><!----></A>SIM =
Anyplace=20
Sample</H2>
<P>This is a sample application for the Pocket PC Phone created with =
Microsoft=20
Visual Studio=C2=AE .NET, C#, SDE and .NET CF. It shows how to access =
the SIM card=20
using the SIM Manager API. The application consists of one form:</P>
<P>The use of this sample is just to get some general information from =
the SIM=20
card by tapping the Get SIM Information button, but by using the =
structure of=20
the sample it can be extended to include more of the SIM Manager API=20
functionality.</P>
<H2 class=3DdtH1><A name=3Dsim_programming_topic5><!----></A>Code =
Walkthrough</H2>
<P>To use the Interop Services in the Compact Framework, the following =
code was=20
added:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl03_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl03');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl03 =
space=3D"preserve">using System.Runtime.InteropServices;
</PRE></DIV>
<P>A class (SIMWrap) was created to hold the prototypes for the Windows =
APIs,=20
and the ones needed in the sample are:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl04_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl04');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl04 =
space=3D"preserve">[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);
</PRE></DIV>
<P>Each prototype has a DllImport attribute pointing to the DLL (Dynamic =
Link=20
Library) where the function is implemented (in this case the =
cellcore.dll). When=20
creating these prototypes, the Visual Studio .NET help file has a nice =
table=20
with type mappings (in the index, look for "platform invoke" and "data =
types").=20
For example, the native type "DWORD" maps to the managed type "UInt32" =
that is a=20
"unit" in C#.</P>
<P>The call to get the capabilities of the SIM card (SimGetDevCaps) =
includes a=20
structure passed by reference (lpSimCaps) as the last parameter. The =
native=20
(C++) structure looks like this:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl05_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl05');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl05 =
space=3D"preserve">typedef struct simcaps_tag {
DWORD cbSize;
DWORD dwParams;
DWORD dwPBStorages;
DWORD dwMinPBIndex;
DWORD dwMaxPBIndex;
DWORD dwMaxPBEAddressLength;
DWORD dwMaxPBETextLength;
DWORD dwLockFacilities;
DWORD dwReadMsgStorages;
DWORD dwWriteMsgStorages;
DWORD dwNumLockingPwdLengths;
SIMLOCKINGPWDLENGTH rgLockingPwdLengths[SIM_NUMLOCKFACILITIES];
} SIMCAPS, FAR *LPSIMCAPS;
</PRE></DIV>
<P>And as SIM_NUMLOCKFACILITIES equals 10, the Compact Framework version =
of the=20
same structure looks like this:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl06_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl06');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl06 =
space=3D"preserve">[StructLayout(LayoutKind.Sequential)]
public struct SimCaps=20
{
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;
}
</PRE></DIV>
<P>Note that even if the MarchalAsAttribute class is not supported in =
the=20
Compact Framework (beta 1), the same structure almost can be =
accomplished by=20
adding the array as members with an additional suffix (0-9). Actually, =
those=20
members are another structure (SimLockingPwdLength).</P>
<P>When the declarations are in place, the code behind the Get SIM =
Information=20
button looks like this:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl07_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl07');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl07 =
space=3D"preserve">SimWrap.SimCaps simCaps =3D new SimWrap.SimCaps();
int hSim =3D 0;
uint phoneUsed =3D 0;
uint phoneTotal =3D 0;
uint smsUsed =3D 0;
uint smsTotal =3D 0;
// Empty ListView
lvwItems.Items.Clear();
// Start SIM Manager session (get handle)
SimWrap.SimInitialize(0, 0, 0, ref hSim);
// Get Phonebook status (used, total)
SimWrap.SimGetPhonebookStatus(hSim, SimWrap.SIM_PBSTORAGE_SIM,
ref phoneUsed, ref phoneTotal);
AddToListView("Total phonebook size:", phoneTotal.ToString());
AddToListView("Phonebook entries:", phoneUsed.ToString());
// Get SIM capabilities
SimWrap.SimGetDevCaps(hSim, SimWrap.SIM_CAPSTYPE_ALL, ref simCaps);
AddToListView("Max. length of name:",
simCaps.dwMaxPBETextLength.ToString());
AddToListView("Max. length of phone number:",
simCaps.dwMaxPBEAddressLength.ToString());
// Get Messages (SMS) status (used, total)
SimWrap.SimGetSmsStorageStatus(hSim, SimWrap.SIM_SMSSTORAGE_SIM,
ref smsUsed, ref smsTotal);
AddToListView("SMS message storage capacity:", smsTotal.ToString());
AddToListView("SMS messages:", smsUsed.ToString());
// End SIM Manager session
SimWrap.SimDeinitialize(hSim);
</PRE></DIV>
<P>After the ListView is empty and the SIM Manager handle (hSim) is =
retrieved=20
(SimInitialize), each of the static methods on the SIM Wrapper class =
(SimWrap)=20
is called resulting in an API call. The variables passed by reference =
get the=20
return values and are added to the ListView using the code:</P>
<DIV class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl08_>
<DIV class=3DCodeSnippetTitleBar>
<DIV class=3DCodeDisplayLanguage></DIV>
<DIV class=3DCopyCodeButton><A class=3DcopyCode=20
href=3D"javascript:CopyCode('ctl00_rs1_mainContentContainer_ctl08');"><IM=
G=20
height=3D9=20
src=3D"http://i.msdn.microsoft.com/Platform/Controls/CodeSnippet/resource=
s/copy_off.gif"=20
align=3Dmiddle border=3D0> Copy Code</A></DIV></DIV><PRE =
class=3DlibCScode id=3Dctl00_rs1_mainContentContainer_ctl08 =
space=3D"preserve">private void AddToListView(string Item, string Value)
{
ListViewItem lvi =3D new ListViewItem(Item);
lvi.SubItems.Add(Value);
lvwItems.Items.Add(lvi);
}
</PRE></DIV>
<P>Finally, the SIM Manager handle (hSim) is released with the call to=20
SimDeinitialize.</P>
<H2 class=3DdtH1><A =
name=3Dsim_programming_topic6><!----></A>Conclusion</H2>
<P>As the Pocket PC Phone is a premier citizen of the world of mobile =
phones, we=20
can take advantage of its capabilities, like SIM cards, when we create =
great,=20
connected applications. With the SIM Manager API, the Interop Services =
of the=20
Compact Framework, and the .NET development environment, we have the =
tools to=20
get going.</P></DIV></DIV></DIV></DIV>
<DIV style=3D"DISPLAY: block; OVERFLOW: hidden; POSITION: relative">
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
<TBODY>
<TR>
<TD class=3DMTPS_FooterFade =
id=3Dctl00_rs1_mtpsFooter_FooterFadeCell>
<DIV class=3DMTPS_FooterLinks =
id=3Dctl00_rs1_mtpsFooter_SiteLinks><A=20
id=3Dctl00_rs1_mtpsFooter_LocalFooterLink0 title=3D"Manage Your =
Profile"=20
href=3D"http://go.microsoft.com/?linkid=3D317027">Manage Your=20
Profile</A> | <A =
id=3Dctl00_rs1_mtpsFooter_LocalFooterLink1=20
title=3DLegal=20
href=3D"http://www.microsoft.com/legal/">Legal</A> | <A=20
id=3Dctl00_rs1_mtpsFooter_LocalFooterLink2 title=3D"Contact Us"=20
href=3D"http://go.microsoft.com/?linkid=3D2028439">Contact=20
Us</A> | <A id=3Dctl00_rs1_mtpsFooter_LocalFooterLink3=20
title=3D"MSDN Flash Newsletter" =
href=3D"http://msdn.microsoft.com/flash/">MSDN=20
Flash Newsletter</A> </DIV><SPAN class=3DMTPS_FooterCopyright=20
id=3Dctl00_rs1_mtpsFooter_MSFT_copyright=20
title=3D"=C2=A9 2008 Microsoft Corporation. All rights =
reserved.">=C2=A9 2008=20
Microsoft Corporation. All rights reserved.</SPAN> <A=20
class=3DMTPS_FooterLinks id=3Dctl00_rs1_mtpsFooter_MSFT_Terms=20
title=3D"Terms of Use" =
href=3D"http://msdn.microsoft.com/cc300389.aspx">Terms=20
of Use</A> | <A class=3DMTPS_FooterLinks=20
id=3Dctl00_rs1_mtpsFooter_MSFT_Trademarks title=3DTrademarks=20
=
href=3D"http://www.microsoft.com/library/toolbar/3.0/trademarks/en-us.msp=
x">Trademarks</A>=20
| <A class=3DMTPS_FooterLinks=20
id=3Dctl00_rs1_mtpsFooter_MSFT_PrivacyStatement title=3D"Privacy =
Statement"=20
href=3D"http://www.microsoft.com/info/privacy.mspx">Privacy =
Statement</A>=20
</TD>
<TD id=3Dctl00_rs1_mtpsFooter_FooterLogoCell><IMG =
class=3DMTPS_FooterLogo=20
id=3Dctl00_rs1_mtpsFooter_MSFT_LOGO title=3D"Microsoft =
Corporation"=20
style=3D"BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; =
BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"=20
alt=3D"Microsoft Corporation"=20
=
src=3D"http://i.msdn.microsoft.com/Platform/Controls/Footer/resources/msl=
ogo.gif">=20
</TD></TR></TBODY></TABLE></DIV></DIV></DIV></DIV>
<DIV style=3D"DISPLAY: none"><IMG height=3D0 alt=3D"Page view tracker" =
hspace=3D0=20
src=3D"http://c.microsoft.com/trans_pixel.asp?source=3Dmsdn&TYPE=3DPV=
&uri=3D%2fen-us%2flibrary(d%3ddefault)%2fms839358(l%3den-us%2cv%3dMSD=
N.10).aspx&p=3D_en-us_library(d=3Ddefault)_ms839358(l=3Den-us,v=3DMSD=
N.10).aspx&prd=3Dwindowsmobile"=20
width=3D0 border=3D0></DIV>
<SCRIPT=20
src=3D"http://i2.msdn.microsoft.com/Platform/Controls/WebTrends/resources=
/msdn.wtnvr-bn1822.3.js"=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -