0

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?

hypheni
  • 756
  • 3
  • 17
  • 37

1 Answers1

1

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.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Well, for the first point. It can be possible that user start the installer after a long period he rebooted the windows. So in that case it will report a false thing. And yes the second point is very useful. Let me give it a try. – hypheni May 18 '15 at 17:19
  • Yes I guess that is true. Determining the date the system was started is harder to do: http://stackoverflow.com/questions/26074360/hot-to-get-windows-boottime/ – Anders May 18 '15 at 19:12
  • I think Volatile is the appropiate option. Other left option is RunOnce. As my installer will always asks for admin rights I don't think that would be a problem too. But yeah I will go for Volatile option until I found something which can stop me. Lets see. – hypheni May 18 '15 at 19:23
  • I think no-one cares about Win95/98/SE anymore :) – Slappy May 19 '15 at 05:02
  • @Anders: Volatile solution seems to be not working with NSIS system plugin call. Key created by NSIS is not getting deleted by windows on next startup, but I tried with Win32API and c++ application which is doing the job fine. I guess some thing going wrong with parameter sections. I tried both with "i ${REG_OPTION_VOLATILE}" and "i1" but same result. – hypheni May 19 '15 at 14:08
  • @Anders: BTW. What did you exactly meant by 'The RunOnce key only applies to Administrators' ? Without admin right one cannot right any places of HKLM? Whats special for RunOnce? – hypheni May 19 '15 at 15:44
  • On most newer versions of Windows, only Administrators will execute the RunOnce keys. – Anders May 20 '15 at 14:56
  • @Anders: Thats not my problem as my installer will always run as admin. But the problem in which now I am is, RunOnce entries are getting cleared even if I do a logoff-logon instead of full reboot. – hypheni May 21 '15 at 10:22
  • @Anders: One more thing. Do you any idea why your code is returning error code 6 for RegCreateKeyEx ? – hypheni May 21 '15 at 18:06
  • Works for me, did you run my exact code or change it in any way? Do you get any compiler warnings? Error code 6 is invalid handle, make sure HKEY_LOCAL_MACHINE is defined correctly... – Anders May 21 '15 at 18:41
  • @Anders: Finally it worked when I inlcluded WinCore.nsh header. But don't know how HKLM always works in my script without including this header. – hypheni May 22 '15 at 07:49
  • ${HKEY_LOCAL_MACHINE} is a define, HKLM is a compiler keyword. You should always read the compiler warnings, they are there for a reason... – Anders May 22 '15 at 14:41
  • @Anders: I also do. I keep 0 warnings always be it C/C++/NSIS. Somehow in my case warning was not reported. – hypheni May 22 '15 at 20:53