因为要在WINCE上面开发一个系统,监测数据。长时间运行下去容易导致死机。所以设定定时重启。认真分析与查阅相关资料后,发现下面几种方法:
一、使用SetSystemPowerState,通过测试后发现该方法重启的效果并不理想,有点像WINDOWS的注销而不是断电重启。
#ifndef POWER_STATE_RESET |
#define POWER_STATE_RESET DWORD(0x00800000) |
typedef BOOL (*SetSystemPowerState)( |
HINSTANCE hModule = LoadLibrary(_T( "Coredll.dll" )); |
SetSystemPowerState f = (SetSystemPowerState)::GetProcAddress(hModule, TEXT( "SetSystemPowerState" )); |
f(NULL, POWER_STATE_RESET, 0); |
二、使用看门狗,因为看门狗必须定时喂食,所以我在调用时不给它喂食就会造成系统重启。我用的EMD9263的板子,重启代码如下:
#define AT91SAM9263_ENABLE_WATCHDOG 2400 |
#define AT91SAM9263_DISABLE_WATCHDOG 2401 |
#define AT91SAM9263_CLEAR_WATCHDOG 2402 |
#define FILE_DEVICE_DATALINK 0x00000005 |
#define METHOD_BUFFERED 0 |
#define FILE_WRITE_ACCESS ( 0x0002 ) // file & pipe |
#define CTL_CODE( DeviceType, Function, Method, Access ) ( \ |
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \ |
#define IOCTL_ENABLE_WATCHDOG CTL_CODE(FILE_DEVICE_DATALINK, AT91SAM9263_ENABLE_WATCHDOG, METHOD_BUFFERED, FILE_WRITE_ACCESS) |
#define IOCTL_DISABLE_WATCHDOG CTL_CODE(FILE_DEVICE_DATALINK, AT91SAM9263_DISABLE_WATCHDOG, METHOD_BUFFERED, FILE_WRITE_ACCESS) |
#define IOCTL_CLEAR_WATCHDOG CTL_CODE(FILE_DEVICE_DATALINK, AT91SAM9263_CLEAR_WATCHDOG, METHOD_BUFFERED, FILE_WRITE_ACCESS) |
HANDLE hFileWatchdog= CreateFile(_T( "DOG1:" ), |
GENERIC_READ | GENERIC_WRITE, |
if (hFileWatchdog != INVALID_HANDLE_VALUE) |
BOOL ret=::DeviceIoControl(hFileWatchdog,IOCTL_ENABLE_WATCHDOG,NULL,0,NULL,0,&dwReturn,NULL); |
::CloseHandle(hFileWatchdog); |
在实际运用中,我用的是第二种方法。第一种方法可以适当使用。