重启WindowsMobile设备(VB/C#)
在论坛中,很多朋友都曾经问过如何在应用程序中重启Windows Mobile设备的问题。今天我们来看如何在Visual Studio 2008中来实现这个功能。 打开Visual Studio 2008,选择“File”菜单,“New”-“Project”。我们会看到下面New Project对话框。
我们只能选择Smart Device Project,右上角的.NET Framework下拉菜单对Windows Mobile程序来说没什么用。将项目名称改为“Reset Pocket PC”,点OK。 在Smart Device的对话框里,我们将Target platform选为“Windows Mobile 6 Professional SDK”,需要说明的是,Windows Mobile 6的SDK在Visual Studio 2008中并不是默认安装,所以需要您单独安装。 然后选择.NET CF的版本,我们选择.NET CF 3.5。需要说明的是,.NET CF是没有3.0的。这是为了和.NET Framework保持一致,所以.NET CF 2.0之后就是.NET CF 3.5。 最后,我们选择创建Device Application。
双击“Exit”菜单,进入代码编辑页面。我们在MenuItem1_Click函数之外,点击鼠标右键。在右键菜单中,我们选择“Insert Snippet”。Code Snippet是Visual Studio中非常有用的一个功能,将一些有用的代码片段保存为Code Snippet,在需要使用的时候就不需要到处翻以前的代码了。 而我们想使用的重启Windows Mobile设备的代码也是VB.NET中的代码片段,这也是我们为什么选择使用VB.NET的原因。
VB.NET示例代码 Private Declare Function KernelIoControl Lib "coredll.dll" (ByVal dwIoControlCode As Integer,ByVal lpInBuf As IntPtr,ByVal nInBufSize As Integer,ByVal lpOutBuf As IntPtr,ByVal nOutBufSize As Integer,ByRef lpBytesReturned As Integer) As Integer Private Function CTL_CODE(ByVal DeviceType As Integer,ByVal Func As Integer,ByVal Method As Integer,ByVal Access As Integer) As Integer Return (DeviceType << 16) Or (Access << 14) Or (Func << 2) Or Method End Function Private Function ResetPocketPC() As Integer Const FILE_DEVICE_HAL As Integer = &H101 Const METHOD_BUFFERED As Integer = 0 Const FILE_ANY_ACCESS As Integer = 0 Dim bytesReturned As Integer = 0 Dim IOCTL_HAL_REBOOT As Integer IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL,15,METHOD_BUFFERED,FILE_ANY_ACCESS) Return KernelIoControl(IOCTL_HAL_REBOOT,IntPtr.Zero,bytesReturned) End Function 在函数创建完成后,我们在菜单的调用方法中调用ResetPocketPC方法。 Private Sub MenuItem1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MenuItem1.Click ResetPocketPC() End Sub
稍后,我们可以看到Windows Mobile设备重启的界面。VB.NET的代码可以正常实现,接下来,C#程序员会问,C#的Code Snippet是否也提供相同的功能。遗憾的是,C#中,并没有支持重启的代码片段。我们需要自己实现C#中的代码,好在将这段代码从VB.NET“翻译”成C#并不复杂。C#代码示例如下: [DllImport("coredll.dll")] public static extern int KernelIoControl(int dwIoControlCode,IntPtr lpInBuf,int nInBufSize,IntPtr lpOutBuf,int nOutBufSize,ref int lpBytesReturned); private int CTL_CODE(int DeviceType,int Func,int Method,int Access) { return (DeviceType << 16) | (Access << 14) | (Func << 2) | Method; } private int ResetPocketPC() { const int FILE_DEVICE_HAL = 0x101; const int METHOD_BUFFERED = 0; const int FILE_ANY_ACCESS = 0; int bytesReturned = 0; int IOCTL_HAL_REBOOT; IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL,FILE_ANY_ACCESS); return KernelIoControl(IOCTL_HAL_REBOOT,ref bytesReturned); } C#代码运行也可以实现相同的功能。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |