请懂的帮忙,谢谢·
8 个解决方案
#1
可以再创建一个辅助进程(assistant process)来完成这件事吧。
在辅助进程中完成 关闭和创建 业务进程的功能。
TerminateProcess // 关闭进程
CreateProcess // 创建进程
这是WinCE的API,c#.net也许有自己的关闭和创建进程的封装函数接口。
在辅助进程中完成 关闭和创建 业务进程的功能。
TerminateProcess // 关闭进程
CreateProcess // 创建进程
这是WinCE的API,c#.net也许有自己的关闭和创建进程的封装函数接口。
#2
VC的代码
C#的调用API也可以实现的
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();
C#的调用API也可以实现的
#3
' 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 Sub
End Module
#4
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
// WINCE 的关机与重启 -C#版
static class cqwince
{
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, int lpBytesReturned);
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern void SetCleanRebootFlag();
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern void keybd_event(byte bVk, byte bScan, long dwFlags, long dwExtraInfo);
//As Byte
private const int KEYEVENTF_KEYUP = 2;
private const int FILE_DEVICE_HAL = 0x101;
private const int FILE_DEVICE_CONSOLE = 0x102;
private const int FILE_DEVICE_PSL = 0x103;
private const int METHOD_BUFFERED = 0;
private const int METHOD_IN_DIRECT = 1;
private const int METHOD_OUT_DIRECT = 2;
private const int METHOD_NEITHER = 3;
private const int FILE_ANY_ACCESS = 0;
private const int FILE_READ_ACCESS = 0x1;
private const int FILE_WRITE_ACCESS = 0x2;
private static int CTL_CODE(int DeviceType, int func, int Method, int Access)
{
return (DeviceType << 16) | (Access << 14) | (func << 2) | Method;
}
//CTL_CODE
public static void WarmBoot()
{
int bytesReturned = 0;
int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
}
public static void ColdBoot()
{
int bytesReturned = 0;
int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
SetCleanRebootFlag();
//JJJ
KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
}
//TestProc
public static void HardReset()
{
int IOCTL_HAL_REBOOT = 0x101003c;
int bytesReturned = 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);
// 冷启动
}
//HardReset
public static void hardHibernate()
{
int IOCTL_HAL_SHUTDOWN = 0x1012000;
//休眠
int bytesReturned = 0;
byte VK_OFF = 0xdf;
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);
//---按键的一个连续动作
}
}
#5
三个DLLImport报错
错误
1“System.Runtime.InteropServices.CharSet”并不包含“Ansi”的定义错误 2“System.Runtime.InteropServices.DllImportAttribute”并不包含“ExactSpelling”的定
#6
vbnet 版的是调试过的,你看哪里翻译错了,自己改。
#7
调用 WINCE API接口 即可
#8
#1
可以再创建一个辅助进程(assistant process)来完成这件事吧。
在辅助进程中完成 关闭和创建 业务进程的功能。
TerminateProcess // 关闭进程
CreateProcess // 创建进程
这是WinCE的API,c#.net也许有自己的关闭和创建进程的封装函数接口。
在辅助进程中完成 关闭和创建 业务进程的功能。
TerminateProcess // 关闭进程
CreateProcess // 创建进程
这是WinCE的API,c#.net也许有自己的关闭和创建进程的封装函数接口。
#2
VC的代码
C#的调用API也可以实现的
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();
C#的调用API也可以实现的
#3
' 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 Sub
End Module
#4
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
// WINCE 的关机与重启 -C#版
static class cqwince
{
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, int lpBytesReturned);
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern void SetCleanRebootFlag();
[DllImport("coredll.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern void keybd_event(byte bVk, byte bScan, long dwFlags, long dwExtraInfo);
//As Byte
private const int KEYEVENTF_KEYUP = 2;
private const int FILE_DEVICE_HAL = 0x101;
private const int FILE_DEVICE_CONSOLE = 0x102;
private const int FILE_DEVICE_PSL = 0x103;
private const int METHOD_BUFFERED = 0;
private const int METHOD_IN_DIRECT = 1;
private const int METHOD_OUT_DIRECT = 2;
private const int METHOD_NEITHER = 3;
private const int FILE_ANY_ACCESS = 0;
private const int FILE_READ_ACCESS = 0x1;
private const int FILE_WRITE_ACCESS = 0x2;
private static int CTL_CODE(int DeviceType, int func, int Method, int Access)
{
return (DeviceType << 16) | (Access << 14) | (func << 2) | Method;
}
//CTL_CODE
public static void WarmBoot()
{
int bytesReturned = 0;
int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
}
public static void ColdBoot()
{
int bytesReturned = 0;
int IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS);
SetCleanRebootFlag();
//JJJ
KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned);
}
//TestProc
public static void HardReset()
{
int IOCTL_HAL_REBOOT = 0x101003c;
int bytesReturned = 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);
// 冷启动
}
//HardReset
public static void hardHibernate()
{
int IOCTL_HAL_SHUTDOWN = 0x1012000;
//休眠
int bytesReturned = 0;
byte VK_OFF = 0xdf;
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);
//---按键的一个连续动作
}
}
#5
三个DLLImport报错
错误
1“System.Runtime.InteropServices.CharSet”并不包含“Ansi”的定义错误 2“System.Runtime.InteropServices.DllImportAttribute”并不包含“ExactSpelling”的定
#6
vbnet 版的是调试过的,你看哪里翻译错了,自己改。
#7
调用 WINCE API接口 即可