Click here to Skip to main content
Click here to Skip to main content

Restart widnows mobile device

, 4 Apr 2008
Rate this:
Please Sign up or sign in to vote.
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)

About the Author

tommykkk

Unknown
No Biography provided

Comments and Discussions

 
GeneralThis method can mangle your network config Pinmemberinvaders@earthling.net4-Jul-08 6:08 
Hey, just a heads up in case you are using this on a lot of different phones, I have used the method you detailed but unfortunately on several WM5 & WM6 smartphones this method often shuts down the device in a very unfriendly way and it can actually prevent the device from connecting to the carrier network after restart. A hard reset is sometimes necessary to recover when a smartphone device is shutdown that way. Another method that ensures that the device shuts down cleanly is:
 
[DllImport("aygshell.dll")]
public static extern bool ExitWindowsEx(uint uFlags, int dwReserved);
 
public const uint EWX_REBOOT = 0x00000002;
public const uint EWX_POWEROFF = 0x00000008;

public static void ResetTheDevice()
{
ExitWindowsEx(EWX_REBOOT,0);
}
 
Hope that prevents some frustration for you in the future, it took me a while to find the problem was with the kernel io reset method.
 
Good Luck,
Vern Germano

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 5 Apr 2008
Article Copyright 2008 by tommykkk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid