We went to Singapore for mobility Metro training during the Spring Festival. Many MVPs and MCT members from across Asia attended the training. The purpose of this training is to quickly promote the latest technologies developed by Windows Mobile 6.0 and Visual Studio 2008 to the world. We will also launch a series of mobility Metro training in China.
A total of four people went to Singapore for training this time. A joke: "Brother Two, your meat is more expensive than the meat of the master ", this training gave a nickname of "passing through. To get a full picture of mobility Metro training, wait until the training program is published.
However, I can also write some interesting knowledge points into a blog here, so that you can enjoy the neighborhood.
Restart a Windows Mobile Device
In the forum, many friends once asked how to restart Windows Mobile devices in applications. Today we will look at how to implement this function in Visual Studio 2008.
Open Visual Studio 2008, select the "file" menu, and choose "New"-"project ". The new project dialog box is displayed.
In project types, select the smart device item in Visual Basic. Ah, it may be strange to read from my blog that I started to use Visual Basic to write programs. Let's talk about it later.
We can only select the smart device project. the. NET Framework drop-down menu in the upper right corner is useless for the Windows Mobile program. Change the project name to "Reset Pocket PC" and click OK.
In the smart device dialog box, select target platform as "Windows Mobile 6 Professional SDK". Note that the Windows Mobile 6 SDK is not installed by default in Visual Studio 2008, therefore, you must install it separately.
Then select the. net cf version. We select. Net CF 3.5. It should be noted that. Net CF does not have 3.0. This is to be consistent with. NET Framework, so. Net CF 2.0 is later than. Net CF 3.5.
Finally, we choose to create the device application.
After entering the IDE editing interface, we found that the IDE environment has not changed much. We directly add two menu items "reset" and "exit" in the following menu ".
Double-click the exit menu to go to the code editing page. In addition to the menuitemdetail click function, right-click the function. In the right-click menu, select insert snippet ". Code snippet is a very useful feature in Visual Studio. It saves some useful code snippets as code snippet, so you do not need to go through the previous code when you need to use it.
The code that we want to use to restart the windows mobile device is also a code snippet in VB. NET, which is why we choose to use VB. NET.
After you select insert snippet, a list is displayed. Choose "smart devices"-"device operating system"-"reset the device ".
After code snippet is inserted, the VB. NET code is as follows. By viewing the code, we can see that the function is implemented by calling the kerneliocontrol API function. The kerneliocontrol function is used to control general I/O. We can use this function to implement many functions, such as obtaining device information, file systems, and power management.
VB. NET sample code
Private Declare FunctionKerneliocontrolLib "Coredll. dll"(ByvalDwiocontrolcodeAs Integer,ByvalLpinbufAsIntptr,ByvalNinbufsizeAs Integer,ByvalLpoutbufAsIntptr,ByvalNoutbufsizeAs Integer,ByrefLpbytesreturnedAs Integer)As Integer
Private FunctionCtl_code (ByvalDevicetypeAs Integer,ByvalFuncAs Integer,ByvalMethodAs Integer,ByvalAccessAs Integer)As Integer
Return(Devicetype <16)Or(Access <14)Or(Func <2)OrMethod
End Function
Private FunctionResetpocketpc ()As Integer
ConstFile_device_halAs Integer= & H101
ConstMethod_bufferedAs Integer= 0
ConstFile_any_accessAs Integer= 0
DimBytesreturnedAs Integer= 0
DimIoctl_hal_rebootAs Integer
Ioctl_hal_reboot = ctl_code (file_device_hal, 15, method_buffered, file_any_access)
ReturnKerneliocontrol (ioctl_hal_reboot, intptr. Zero, 0, intptr. Zero, 0, bytesreturned)
End Function
After the function is created, call the resetpocketpc method in the call method of the menu.
Private SubMenuitem1_click (ByvalSenderAsSystem. object,ByvalEAsSystem. eventargs)HandlesMenuitem1.click
Resetpocketpc ()
End Sub
After compilation and running, the program running effect is as follows. When we click the reset menu, the windows mobile device will restart. If Visual Studio uses the debug mode, debugging is terminated.
Later, we can see the Windows Mobile Device restart interface. The VB. NET code can be implemented normally. Next, the C # programmer will ask whether the C # code snippet provides the same functions. Unfortunately, in C #, There is no code snippet that supports restart. We need to implement the Code in C # by ourselves. Fortunately, it is not complicated to translate the code from VB. NET to C. C # sample 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);
}
C # code execution can also implement the same function.
From: http://dev.10086.cn/cmdn/wiki/index.php? Doc-view-2784.html