I have to prepare a tool which like teamviewer(.com) will have a question to run or install.
When the user chooses install i need to copy that same exe into the target folder and ceate a shortcut..
I want to force elevated rights during the install.
To elaborate, i have 'dl.exe' with these two options.
On install option it calls itself with the 'install' option and having elevated rights (UAC prompt and so on).
That copy installs itself in the pf folder or wherever the user wants and creates a shortcut.
For the instant run scenario we can not have the exe shows the UAC so the manifest for elevated rights is not implemented.
To keep the dl small i am putting it all into one exe.
Any idea to run the copy with the elevated rights forced?
I only found a shortcut to exe option which may work..
Doesn't Teamviewer install a service anyway?
Installing a service will solve all your rights problems, as it runs in system context.
Details on what i mean see the last part in this article:
"How to circumvent all security in Windows". (http://www.brianbondy.com/blog/id/100/understanding-windows-at-a-deeper-level-sessions-window-stations-and-desktops)
I have read several topics today how to deal with this and sometimes i get silly solutions like alter HKLM registry settings.
Dudes... i can't since i am not in an elevation modus... yet...
I am also not installing a service, i am merely want to install the currently running exe but with full UAC and admin rights behaviour.
Install in this case is copying the same exe in the pf folder and creating a shortcut to it.
Since the app itself is not elevated at that time i must make it behave like it's elevated.
An optional solution is to extract a setup which the correct manifest but that's not so handy.
I'll have that as option..
Edwin,
How about ShellExecuteEx with the RunAs verb...
Pierre
'rc file: RC file, just for the theme, not mandatory
'rc file: #include "resource.h"
'rc file:
'rc file: #define IDR_MANIFEST 1 // 1 for a EXE, 2 for a DLL
'rc file: #define RT_MANIFEST 24
'rc file:
'rc file: IDR_MANIFEST RT_MANIFEST MOVEABLE PURE
'rc file: {
'rc file: "<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">
'rc file: <assemblyIdentity
'rc file: name=""Microsoft.Windows.Shell.shell32""
'rc file: processorArchitecture=""x86""
'rc file: version=""1.50.00.01""
'rc file: type=""win32""/>
'rc file: <description>Windows Shell</description>
'rc file: <dependency>
'rc file: <dependentAssembly>
'rc file: <assemblyIdentity
'rc file: type=""win32""
'rc file: name=""Microsoft.Windows.Common-Controls""
'rc file: version=""6.0.0.0""
'rc file: processorArchitecture=""x86""
'rc file: publicKeyToken=""6595b64144ccf1df""
'rc file: language=""*""
'rc file: />
'rc file: </dependentAssembly>
'rc file: </dependency>
'rc file: <asmv3:trustInfo xmlns:asmv3=""urn:schemas-microsoft-com:asm.v3"">
'rc file: <asmv3:security>
'rc file: <asmv3:requestedPrivileges>
'rc file: <asmv3:requestedExecutionLevel
'rc file: level=""asInvoker""
'rc file: uiAccess=""false"" />
'rc file: </asmv3:requestedPrivileges>
'rc file: </asmv3:security>
'rc file: </asmv3:trustInfo>
'rc file: </assembly>"
'rc file: }
#COMPILE EXE '#Win 8.04#
#DIM ALL
#REGISTER NONE
#INCLUDE "Win32Api.inc"
#RESOURCE "Elevation01.pbr" 'Not mandatory
GLOBAL hDlg AS DWORD
%Label = 101
%ButtonRestart = 201
%ButtonExit = 202
%BCM_FIRST = &h1600 'Normal button
%BCM_SETSHIELD = %BCM_FIRST + &h000C 'Elevated button
%BCM_GETIDEALSIZE = %BCM_FIRST + &h0001
%BCM_SETNOTE = %BCM_FIRST + &H0009
DECLARE FUNCTION IsUserAnAdmin LIB "Shell32.DLL" ALIAS "IsUserAnAdmin" () AS LONG
'______________________________________________________________________________
FUNCTION ExeName(Action AS LONG) AS STRING 'Return the ExeName of this APP or ExeName of calling app if DLL
LOCAL FileName AS ASCIIZ * %Max_Path
LOCAL PathFileLen AS LONG
LOCAL FileExtLen AS LONG
LOCAL DotPos AS LONG
LOCAL SlashPos AS LONG
PathFileLen = GetModuleFileName(0, FileName, %Max_Path)
SlashPos = INSTR(-1, FileName, "\")
DotPos = INSTR(-1, FileName, ".")
IF DotPos < SlashPos THEN DotPos = 0
FileExtLen = PathFileLen - SlashPos
SELECT CASE Action
CASE 0 '"C:\Subdir\File.exe" - Path, filename and extention
FUNCTION = LEFT$(FileName, PathFileLen)
CASE 1 'C:\Subdir\File - Path and filename, no extention, dot excluded
IF DotPos THEN
FUNCTION = LEFT$(FileName, DotPos - 1)
ELSE
FUNCTION = LEFT$(FileName, PathFileLen)
END IF
CASE 2 'C:\Subdir\ - Path only, including last backslash
FUNCTION = LEFT$(FileName, SlashPos)
CASE 3 'File.exe - Filename and Extention
FUNCTION = MID$(FileName, SlashPos + 1, FileExtLen)
CASE 4 'File - Filename only no Extention
IF DotPos THEN
FUNCTION = MID$(FileName, SlashPos + 1, FileExtLen - (PathFileLen - DotPos) - 1)
ELSE
FUNCTION = MID$(FileName, SlashPos + 1, FileExtLen)
END IF
END SELECT
END FUNCTION
'______________________________________________________________________________
FUNCTION IsUserAdmin() AS LONG 'Thank to Neil Hosgood
LOCAL NtAuthority AS SID_IDENTIFIER_AUTHORITY
LOCAL AdministratorsGroup AS LONG
LOCAL IsMember AS LONG
'Shell32.DLL's IsUserAnAdmin may not be there in future release!
NtAuthority.value(5) = 5 '%SECURITY_NT_AUTHORITY
IF AllocateAndInitializeSid(NtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, _
%DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, AdministratorsGroup) THEN
IF CheckTokenMembership(%NULL, BYVAL AdministratorsGroup, IsMember) THEN
FUNCTION = IsMember
END IF
FreeSid(BYVAL AdministratorsGroup)
END IF
END FUNCTION
'______________________________________________________________________________
CALLBACK FUNCTION DlgProc
LOCAL lpFile AS ASCIIZ * %MAX_PATH
LOCAL lpDirectory AS ASCIIZ * %MAX_PATH
LOCAL lpVerb AS ASCIIZ * %MAX_PATH
LOCAL lpParameters AS ASCIIZ * 50
LOCAL SEI AS ShellExecuteInfo
SELECT CASE CBMSG
CASE %WM_INITDIALOG
IF IsUserAdmin THEN
CONTROL SET TEXT CBHNDL, %Label, "User is admin"
CONTROL SET TEXT CBHNDL, %ButtonRestart , "Restart with current elevation"
ELSE
CONTROL SET TEXT CBHNDL, %Label, "User is not admin"
CONTROL SET TEXT CBHNDL, %ButtonRestart , "Restart with Admin elevation"
END IF
CASE %WM_COMMAND
SELECT CASE LOWRD(CBWPARAM)
CASE %ButtonRestart
IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
IF IsUserAdmin THEN
lpVerb = "open"
ELSE
lpVerb = "runas"
END IF
lpFile = ExeName(0)
lpDirectory = ExeName(2)
SEI.cbSize = SIZEOF(SEI)
SEI.fmask = %SEE_MASK_NOCLOSEPROCESS OR %SEE_MASK_FLAG_DDEWAIT
SEI.hWnd = %HWND_DESKTOP
SEI.lpVerb = VARPTR(lpVerb)
SEI.LpFile = VARPTR(lpFile)
SEI.lpParameters = VARPTR(lpParameters)
SEI.lpDirectory = VARPTR(lpDirectory)
SEI.nShow = %SW_SHOWNORMAL
SEI.hInstApp = 0
SEI.lpIdList = %NULL
SEI.lpClass = %NULL
SEI.hkeyClass = %NULL
SEI.dwHotKey = %NULL
SEI.hProcess = 0
IF ShellExecuteEx(SEI) THEN
DIALOG END CBHNDL, 0
END IF
END IF
CASE %ButtonExit
IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
DIALOG END CBHNDL, 0
END IF
END SELECT
END SELECT
END FUNCTION
'______________________________________________________________________________
FUNCTION PBMAIN()
DIALOG NEW %HWND_DESKTOP, "Admin elevation", , , 200, 80, _
%WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_SYSMENU OR %WS_THICKFRAME, 0 TO hDlg
SetClassLong hDlg, %GCL_HICON, LoadIcon(BYVAL %NULL, BYVAL %IDI_INFORMATION) 'Set a nice dialog icon
CONTROL ADD LABEL, hDlg, %Label, "", 10, 10, 180, 10, %SS_CENTER OR %SS_LEFT, %WS_EX_LEFT
CONTROL ADD BUTTON, hDlg, %ButtonRestart , "", 25, 25, 150, 20, _
%BS_CENTER OR %BS_FLAT OR %BS_VCENTER OR %WS_TABSTOP, %WS_EX_LEFT
SendMessage(GetDlgItem(hDlg, %ButtonRestart), %BCM_SETSHIELD, 0, %TRUE) 'Decorate the button, need Manifest
CONTROL ADD BUTTON, hDlg, %ButtonExit, "Exit", 140, 55, 35, 15
CONTROL SET FOCUS hDlg, %ButtonRestart
DIALOG SHOW MODAL hDlg CALL DlgProc
END FUNCTION
'______________________________________________________________________________
'
Also...
Shortcut creation and query (http://www.powerbasic.com/support/pbforums/showthread.php?t=25075)
About the ShortCut RusAs flag (http://www.powerbasic.com/support/pbforums/showpost.php?p=350378&postcount=16)
-
>How about ShellExecuteEx with the RunAs verb...
NICE!
Tested but will test more!
Seems to work as i wanted it!
Thanks..
Anyway, i moved to c# for this tool :)
ProcessStartInfo si = new ProcessStartInfo();
si.Verb = "runas";
si.UseShellExecute = true;
(some more blabla settings..)
Process p = Process.Start(si);
p.WaitForInputIdle();
p.WaitForExit();