简体   繁体   中英

How to return to command prompt after a silent NSIS install

I have a requirement for my silent installer to return to the command prompt after successful completion. Currently the best I can do is get to a blinking underscore. I must then manually hit the enter key.

Thank you.

NSIS installers are GUI applications and cmd.exe should not wait for them at all.

RequestExecutionLevel user
Outfile "Test.exe"
SilentInstall silent
Name "Test"

Section
System::Call 'user32::MessageBeep(i0)'
Sleep 3333
System::Call 'user32::MessageBeep(i0)'
SectionEnd

This little sample returns control to the console immediately without having to press anything. (Both test and call test , start /WAIT test will wait of course). Compiled with NSIS 2.46 and 3.0 trunk and tested in cmd.exe on Win8.

Edit:

You failed to mention that you are writing to stdout from NSIS. Doing that is a bit of a hack since the installer is not a console application. Because cmd.exe thinks it is running a GUI application it does not wait and then when the GUI application calls AttachConsole you end up with two applications thinking they are in control of stdout and things no longer work 100% correctly.

If you execute yourapp.exe&dir /S /B c:\\ the things written to stdout by yourapp.exe is going to get lost somewhere in the output from dir! In your case the prompt (usually current-directory + > ) is actually printed but it happens before yourapp.exe's output so that's why it looks like you just have a blinking cursor.

I was unable to come up with a solution that actually does what you want:

SilentInstall silent

Function WriteCommandLine 
System::Call 'kernel32::GetStdHandle(i -11)i.r0'
System::Call 'kernel32::AttachConsole(i -1)'
StrCmp $0 0 "" +2
System::Call 'kernel32::GetStdHandle(i -11)i.r0'
FileWrite $0 $R1
FileWrite $0 $\n
/* Does not work: 
System::Call 'kernel32::FreeConsole()'
*/
/* This ugly hack does not work:
ExpandEnvStrings $0 "%COMSPEC%"
StrCmp $0 "" "" +2
StrCpy $0 cmd.exe
Exec '"$0" /c prompt' 
*/
FunctionEnd

Function .onInit
strcpy $R1 "Hello World"
call WriteCommandLine
FunctionEnd

If your installer is always silent you could use !packhdr if you can find a tool that can change IMAGE_SUBSYSTEM_WINDOWS_GUI to IMAGE_SUBSYSTEM_WINDOWS_CUI in a PE file...

Are you calling the installer yourself, and do you want to wait for the installer to finish?

In that case, call the following:

start /wait YourInstaller.exe /S

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