Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Restart widnows mobile device

5.00/5 (2 votes)
4 Apr 2008CPOL2 min read 1  
How restart Windows Mobile device in application.

Introduction

How restart Windows Mobile device in application.Today, we use Visual Studio 2008 to achieve this function..

Background

Development Net CF application, meets frequently question.

Using the code

Open Visual Studio 2008, choice “File” menu, “New” - “Project”.We will see New Project dialog box.

In Project types, we choose in Visual Basic Smart Device item.(also choose in Visual C# Smart Device item).

In the Smart Device dialog box, we choose Target platform for “Windows Mobile 6 Professional SDK”,must be explained that the Windows Mobile 6 SDK in Visual Studio 2008 is not in the default installation, so you need a separate installation.

Then chooses .NET CF the edition, we choose .NET CF 3.5.What needs to explain, .NET CF does not have 3.0.This is to maintain consistent with .NET Framework, after therefore .NET CF 2.0, is .NET CF 3.5.

Finally, we choose create Device Application.

After entering the IDE editio ui, we discovered that the IDE environment has not had the too big change.We increase two menu items directly in the following menu “Reset” and “Exit”.
The double click “Exit” the menu, enters the code page.We outside the MenuItem1_Click function, click on the mouse right key.In the pop-up menu, we chose "Insert Snippet." Code Snippet is Visual Studio a very useful function, some useful code fragment preservation is Code Snippet, in needs to use time did not need everywhere to turn the beforehand code.We want to use restart Windows Mobile device's code are also in the VB.NET code fragments.

Select Insert Snippet, there is a list, we successive choice "Smart Devices" - "Device Operating System" - "Reset the Device."

Insert the Code Snippet, VB.NET code below. Show code, we can see that the function is called to realize KernelIoControl API function. KernelIoControl function is used to achieve the common IO control, we can achieve the adoption of the function of many features, such as access to equipment information, file systems, and power management.

VB.NET Demo code:

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, 0, IntPtr.Zero, 0,
bytesReturned)

End Function

After the function foundation completes,we call in the menu method called ResetPocketPC methods.

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

        ResetPocketPC()

    End Sub

After compilation run, program run effect like figure above.When we click on the Reset menu, Windows Mobile device will restart.

viusal c# demo code:

[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, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);

            return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned);

        }


History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)