Launch program functions with amdministrative privlages.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to figure out how to elevate privileges when my program executes code through command prompt. My program will already prompt for administrator access when it is launched. This is the code from my manifest file that will allow it to prompt for
UAC admin permissions/credentials (to elevate permissions)
VB .Net 2010
<div style="color:black; background-color:white
<pre><?xml version=<span style="color:#a31515 "1.0" encoding=<span style="color:#a31515 "utf-8"?>
<asmv1:<span style="color:blue assembly manifestVersion=<span style="color:#a31515 "1.0" xmlns=<span style="color:#a31515 "urn:schemas-microsoft-com:asm.v1" xmlns:asmv1=<span style="color:#a31515 "urn:schemas-microsoft-com:asm.v1" xmlns:asmv2=<span style="color:#a31515 "urn:schemas-microsoft-com:asm.v2" xmlns:xsi=<span style="color:#a31515 "http://www.w3.org/2001/XMLSchema-instance">
<trustInfo xmlns=<span style="color:#a31515 "urn:schemas-microsoft-com:asm.v2">
<security>

<applicationRequestMinimum>
<PermissionSet Unrestricted=<span style="color:#a31515 "true" ID=<span style="color:#a31515 "Custom" SameSite=<span style="color:#a31515 "site" />
<defaultAssemblyRequest permissionSetReference=<span style="color:#a31515 "Custom" />
</applicationRequestMinimum>

<requestedPrivileges xmlns=<span style="color:#a31515 "urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level=<span style="color:#a31515 "requireAdministrator" uiAccess=<span style="color:#a31515 "false" />
</requestedPrivileges>

</security>
</trustInfo>
</asmv1:<span style="color:blue assembly>

[/code]


When this runs, it does have rights to run most of its function as administrator. Yet I have one button that executes commands through cmd and currently only launches as a standard command prompt. This is my code for that button:

<div style="color:black; background-color:white
<pre> <span style="color:blue Private <span style="color:blue Sub btnOffice2010Act_Click(<span style="color:blue ByVal sender <span style="color:blue As System.Object, <span style="color:blue ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue Handles btnOffice2010Act.Click
<span style="color:blue Dim Command1 <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim Command2 <span style="color:blue As <span style="color:blue String

<span style="color:blue If rad64bit.Checked = <span style="color:blue True <span style="color:blue Then
progFilePath = Environment.ExpandEnvironmentVariables(<span style="color:#a31515 "%ProgramFiles% (x86)")
<span style="color:blue ElseIf rad32bit.Checked = <span style="color:blue True <span style="color:blue Then
progFilePath = Environment.ExpandEnvironmentVariables(<span style="color:#a31515 "%ProgramFiles%")
<span style="color:blue Else
MsgBox(<span style="color:#a31515 "Please select 32-bit or 64-bit OS", , <span style="color:#a31515 "Error")
<span style="color:blue Exit <span style="color:blue Sub
<span style="color:blue End <span style="color:blue If

Command1 = <span style="color:#a31515 "cscript """ & progFilePath & <span style="color:#a31515 "Microsoft OfficeOffice14ospp.vbs"" /sethst:kms.xxxxxx.com"
Command2 = <span style="color:#a31515 "cscript """ & progFilePath & <span style="color:#a31515 "Microsoft OfficeOffice14ospp.vbs"" /act"

<span style="color:green Runs command in commmand prompt and /k will keep window open.
Shell(<span style="color:#a31515 "cmd /q /k" & Command1 & <span style="color:#a31515 " && " & Command2 & <span style="color:#a31515 "&&pause", AppWinStyle.NormalFocus)
<span style="color:blue End <span style="color:blue Sub
[/code]


This buttons function is to activate Office through a KMS server. The code has been tested and work properly from a batch file. Yet it only works when run as administrator, but through my program it will not run the code as administrator.
Adding runas in the Shell() command will not work, since I dont want it to be specific to one user. Runas will require me to enter a password, but the only password prompt I would want to see (if any) would be from UAC
My questions is, how do I do at least one of the following:<br/>
1) Have my program execute the code as an administrator<br/>
2) Have my program execute the code in an elevated permission command prompt.
Any help would be greatly appreciated.
Thanks in advance.

View the full article
 
Back
Top