Old 7th August 2018, 22:29   #1
ikreb
Junior Member
 
Join Date: Aug 2018
Posts: 4
Want use only SimpleSC with admin rights

Hello,

i have a nsis file with the setting

code:
RequestExecutionLevel user


but I want to start a windows service. I use NSIS Simple Service Plugin for it.

So I want that the UAC request will come only if I try to start a service. Is this possible? And if could you give me a hint, please?
ikreb is offline   Reply With Quote
Old 7th August 2018, 23:23   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,926
A process cannot elevate itself after it has been started.

The easy way is to create another mini installer that does the elevated operation. You must start it with ExecShell[Wait] when required.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 13th August 2018, 13:57   #3
ikreb
Junior Member
 
Join Date: Aug 2018
Posts: 4
But How do I get the return code then?

code:

ExecShell "open" "file.exe" SW_HIDE

ikreb is offline   Reply With Quote
Old 13th August 2018, 15:02   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,926
https://stackoverflow.com/a/51823952/3501

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 13th August 2018, 15:54   #5
ikreb
Junior Member
 
Join Date: Aug 2018
Posts: 4
I will show more code. Because the return code looks very randomly.

This is my nsis file which I want to execute with admin permissions.

code:



;======================================================
; General

Name "Start service wlansvc"
OutFile "start_wlansvc.exe"

RequestExecutionLevel admin

;======================================================
; Functions

Function .onInit
; SilentInstall silent

; start wlansvc
SimpleSC::StartService "wlansvc" "" 30

FunctionEnd

;======================================================
; Sections

Section
SetAutoClose true

SectionEnd



And then I use this in my main nsis file

code:

StrCpy $1 "start_wlansvc.exe" ; File to execute
System::Call '*(&l4, i ${SEE_MASK_NOCLOSEPROCESS}, p$HWNDPARENT, p0, tr1, p0, p0, p5, p0, p0, p0, p0, p0, p0, p)p.r1'
System::Call 'SHELL32::ShellExecuteEx(t)i.r0 (pr1)' ; (t) is a hint for A/W detection
${If} $0 <> 0
System::Call '*$1(i, i, p, p, p, p, p, p, p, p, p, p, p, p, p.r0)'
System::Call 'KERNEL32::WaitForSingleObject(p r0, i ${INFINITE})'
System::Call 'KERNEL32::GetExitCodeProcess(p r0 s,*i.r0)'
System::Call 'KERNEL32::CloseHandle(p s)'
DetailPrint ExitCode=$0
${EndIf}
MessageBox MB_OK $0
MessageBox MB_OK $1
System::Free $1



I think the problem is that the first nsis file returns the wrong return code. I tried this, but doesn't helps

code:

SimpleSC::StartService "wlansvc" "" 30

Pop $0
Pop $1
${If} $0 == 0
${If} $1 == 0
SetErrorLevel 4
${EndIf}
${Else}
SetErrorLevel 4
${EndIf}

ikreb is offline   Reply With Quote
Old 13th August 2018, 16:08   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,926
The ShellExecute code works correctly and is not random as far as I can tell:

PHP Code:
Section
System
::Call '*(&l4, i ${SEE_MASK_NOCLOSEPROCESS}, p$HWNDPARENT, p0, t "cmd.exe", t "/c exit /b 666", p0, p5, p0, p0, p0, p0, p0, p0, p)p.r1'
System::Call 'SHELL32::ShellExecuteEx(t)i.r0 (pr1)' ; (tis a hint for A/W detection
${If} $<> 0
    System
::Call '*$1(i, i, p, p, p, p, p, p, p, p, p, p, p, p, p.r0)'
    
System::Call 'KERNEL32::WaitForSingleObject(p r0, i ${INFINITE})'
    
System::Call 'KERNEL32::GetExitCodeProcess(p r0 s,*i.r0)'
    
System::Call 'KERNEL32::CloseHandle(p s)'
    
MessageBox mb_ok ExitCode=$666 every time
${EndIf}
System::Free $1
SectionEnd 
$1 will be "random" be cause it is a memory address but you don't need to look at it, just free it.

You can use Process Monitor from SysInternals to verify the exit code of start_wlansvc.exe.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 13th August 2018, 16:22   #7
ikreb
Junior Member
 
Join Date: Aug 2018
Posts: 4
Yes, this works with cmd.exe, but not with the nsis exe file "start_wlansvc.exe" which I showed at my last post.
ikreb is offline   Reply With Quote
Old 13th August 2018, 17:17   #8
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 4,926
PHP Code:
Section
SetErrorLevel 4
GetErrorLevel 
$0
MessageBox mb_ok ErrorLevel
=$0
Quit
SectionEnd 
Process exit code is 4, confirmed by Process Monitor.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Reply
Go Back   Winamp & SHOUTcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump