总而言之,我需要检查和/或阻止用户在安装程序运行时决定关闭计算机。现在我已经研究了一段时间,并想出了以下内容:
${If} ${AtMostWinXP}
System::Call `kernel32::GetModuleHandle(i0)i.r3`
System::Call `user32::CreateWindowEx(i0,t"STATIC",t"MyApp",i0,i0,i0,i0,i0,i$HWNDPARENT,i0,ir3,i0)i.r1`
${ElseIf} ${AtLeastVista}
System::Call `user32::ShutdownBlockReasonCreate(ir1,w"MyApp is running and still needs to clean up before shutting down!")i.r0`
${EndIf}
但是上面的代码段不起作用。我错过了什么吗?我尝试过使用:
System::Call `kernel32::GetModuleHandle(i0)i.r3`
System::Call `user32::CreateWindowEx(i0,t"STATIC",t"MyApp",i0,i0,i0,i0,i0,i$HWNDPARENT,i0,ir3,i0)i.r1`
System::Call `user32::ShutdownBlockReasonCreate(ir1,w"MyApp is running and still needs to clean up before shutting down!")i.r0`
由于前两个调用适用于Windows XP及更早版本,第三个调用适用于Windows Vista或更高版本,但被Windows XP及更早版本忽略,我相信(我没有证据支持此理论)。这也没有用。
另外,我可以使用user32::ShutdownBlockReasonCreate(ir1,w"$(PreventShutdown)")i.r0
而不是使用上面代码段中的整个字符串来获得不同的语言支持,对吗?
答案 0 :(得分:1)
您的Windows XP代码毫无意义,窗口需要处理WM_QUERYENDSESSION
来阻止关机。幸运的是,NSIS已经为您处理了WM_QUERYENDSESSION
。
对Vista及更高版本使用类似的东西:
LoadLanguageFile "${NSISDIR}\Contrib\Language Files\English.nlf"
LangString BlockReason ${LANG_ENGLISH} "Installer blah blah"
LoadLanguageFile "${NSISDIR}\Contrib\Language Files\Swedish.nlf"
LangString BlockReason ${LANG_SWEDISH} "Installer bork bork"
!include nsDialogs.nsh ; For WS_CHILD
!define /ifndef WS_POPUP 0x80000000
!include LogicLib.nsh
Function CreateShutdownBlockReason
StrCpy $1 $hwndParent
${If} $1 Z= 0 ; $hwndParent is 0, create a new window for silent installers
System::Call 'USER32::CreateWindowEx(i0, t "STATIC", t "$(^Name)", i ${WS_CHILD}|${WS_POPUP}, i0, i0, i0, i0, p r1, i0, i0, i0)p.r1'
${EndIf}
System::Call 'USER32::ShutdownBlockReasonCreate(p r1, w "$(BlockReason)")'
FunctionEnd
Function .onInit
IfSilent 0 +2
Call CreateShutdownBlockReason ; .onGuiInit is not executed in silent installers
FunctionEnd
Function .onGuiInit
Call CreateShutdownBlockReason
FunctionEnd
我不确定静音安装程序是否能够阻止关机,但这是没有插件的最佳选择。