数据下载完成后,我要控制应用程序立即重启,但是不知道如何实现重启应用程序的功能。
请懂的帮忙,谢谢·
------解决方案--------------------
可以再创建一个辅助进程(assistant process)来完成这件事吧。
在辅助进程中完成 关闭和创建 业务进程的功能。
TerminateProcess // 关闭进程
CreateProcess // 创建进程
这是WinCE的API,c#.net也许有自己的关闭和创建进程的封装函数接口。
------解决方案--------------------
VC的代码
- C/C++ code
1、重启按钮事件中添加代码: g_bIsRunAgain=true;//控制是否重新运行的变量 this->SendMessage(WM_CLOSE); 2、在OnClose()消息处理中加入代码: if(g_bIsRunAgain) { char pBuf[MAX_PATH]; //存放路径的变量 GetCurrentDirectory(MAX_PATH,pBuf); //获取程序的当前目录 strcat(pBuf,""); strcat(pBuf,AfxGetApp()->m_pszExeName); strcat(pBuf,".exe"); CString strPath= (CString) pBuf; STARTUPINFO StartInfo; PROCESS_INFORMATION procStruct; memset(&StartInfo, 0, sizeof(STARTUPINFO)); StartInfo.cb = sizeof(STARTUPINFO); ::CreateProcess( (LPCTSTR) strPath, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartInfo, &procStruct); } CDialog::OnClose();
------解决方案--------------------
- VB.NET code
' WINCE 的关机与重启 -VB2005版 ,这是我现在用的模块,可以直接调用。Module cqwince 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, ByVal lpBytesReturned As Integer) As Integer Private Declare Sub SetCleanRebootFlag Lib "coredll.dll" () Private Declare Sub keybd_event Lib "coredll.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Private Const KEYEVENTF_KEYUP As Integer = 2 'As Byte Private Const FILE_DEVICE_HAL As Integer = &H101 Private Const FILE_DEVICE_CONSOLE As Integer = &H102 Private Const FILE_DEVICE_PSL As Integer = &H103 Private Const METHOD_BUFFERED As Integer = 0 Private Const METHOD_IN_DIRECT As Integer = 1 Private Const METHOD_OUT_DIRECT As Integer = 2 Private Const METHOD_NEITHER As Integer = 3 Private Const FILE_ANY_ACCESS As Integer = 0 Private Const FILE_READ_ACCESS As Integer = &H1 Private Const FILE_WRITE_ACCESS As Integer = &H2 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 'CTL_CODE Sub WarmBoot() Dim bytesReturned As Integer = 0 Dim IOCTL_HAL_REBOOT As Integer = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS) KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned) End Sub Sub ColdBoot() Dim bytesReturned As Integer = 0 Dim IOCTL_HAL_REBOOT As Integer = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS) SetCleanRebootFlag() 'JJJ KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned) End Sub 'TestProc Sub HardReset() Dim IOCTL_HAL_REBOOT As Integer = &H101003C Dim bytesReturned As Integer = 0 SetCleanRebootFlag() ' KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned) '一般 ' KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 4, IntPtr.Zero, 1024, bytesReturned) ' 热启动 KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 4, IntPtr.Zero, 1024, bytesReturned) ' 冷启动 End Sub 'HardReset Sub hardHibernate() Dim IOCTL_HAL_SHUTDOWN As Integer = &H1012000 '休眠 Dim bytesReturned As Integer = 0 Dim VK_OFF As Byte = &HDF KernelIoControl(IOCTL_HAL_SHUTDOWN, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned) '---按键的一个连续动作 keybd_event(VK_OFF, 0, 0, 0) keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0) '---按键的一个连续动作 End SubEnd Module