简体   繁体   中英

Detecting last system restart from NSIS script

Well, as the title suggests, it's all about checking last system reboot time and depending on that, executing some tasks.

My situation is, I will be asking user to restart the system on certain condition during the installation. Now clicking on 'OK' installer will be closed and user will have to manually restart the system.

Now say, user run the installer without restarting the system again. Now installer should report an error and ask for the restart.

Possible workaround thought so far.

  1. Checking and storing system time and compare both of them. This method will get failed if the user runs the installer after a long time of system reboot.
  2. Set some invalid values(need the value name only) in registry RunOnce and check for that value on installer startup. As values from RunOnce gets deleted automatically by Windows, if installer found the entry to be there, its obvious that user didn't restarted the system.

Any better ideas?

The RunOnce key only applies to Administrators so you have to keep that in the back of your mind.

You can check how long the system has been running:

System::Call 'kernel32::GetTickCount64()l.r0'
StrCmp $0 error 0 +2
System::Call 'kernel32::GetTickCount()i.r0'

$0 will contain the number of milliseconds since last boot. (GetTickCount64 is Vista+ and older systems will roll-over to 0 after about 45 days)

In .onInit you read the previous value from the registry (if any) and compare, if the current value is larger than the previous value then the system has not been rebooted.

Another option is to write a value to a (unique) volatile registry key, these keys only exist in memory and are lost on reboots:

!include WinCore.nsh
!include LogicLib.nsh
!ifndef REG_OPTION_VOLATILE
!define REG_OPTION_VOLATILE 1
!endif
System::Call 'advapi32::RegCreateKeyEx(i ${HKEY_LOCAL_MACHINE}, t "Software\Volatile\{f255ae7a-fd7d-11e4-a322-1697f925ec7b}", i0, i0, i ${REG_OPTION_VOLATILE}, i ${GENERIC_WRITE}, i0, *i.r1, *i)i.r0'
${If} $0 = 0
        System::Call 'advapi32::RegCloseKey(ir1)'
        WriteRegDWORD HKLM "Software\Volatile\{f255ae7a-fd7d-11e4-a322-1697f925ec7b}" NoReboot 1
${EndIf}

Volatile keys are not supported on Windows 95/98/ME.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM