Resolved Return the location of the InstallUtil.exe - Installing a service.

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
25
Location
Leeds, UK
Programming Experience
5-10
Hi again,

Visual Studio Community Edition 2019.
.Net Framework 4.6

I have written a simple Windows service. I also have a separate interface to, install the service, set the initial variables for the service and uninstall the service. Install and uninstall both make use of the InstalUtil.exe included in the .Net framework.

On my development computer all my code works fine. However, installion could occur on old pc's running Windows Vista SP2, Windows Server 2012 onwards upto the latest from Microsoft. There will also be x32 and x64 builds of my project.

Currently, I have this:-

VB.Net 4.6:
'Install the service.
            Dim appPath As String = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe"
            Dim binPath As String = TxbxProgramPath.Text + "calibre-service.exe"
            Dim installUtil As New Process()
            Dim installStartInfo As New ProcessStartInfo With {
            .FileName = appPath,
            .Arguments = binPath,
            .Verb = "runas",
            .CreateNoWindow = True,
            .RedirectStandardOutput = True,
            .UseShellExecute = False
        }
            installUtil.StartInfo = installStartInfo
            installUtil.Start()

Is there a way to obtain the correct path to the correct InstallUtil.exe at runtime?
Is the path to InstallUtil.exe only ever going to be in two directories depending on if x32 or x64.

Hope someone can please advise and thanks for reading.

Benshaws.
 
Yes, that's it, but you can use %windir% in place of c:\windows.
 
On second thought %windir% won't work with UseShellExecute=false, but you can get it with Environment class:
VB.NET:
Dim windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
 
There's is another small problem however, Use UseShellExecute=false and Verb=runas doesn't go together. Elevation requires shellexecute.
 
Hi JohnH (again),

Please forgive me - I have read your replies and was going to post a reply in the morning (UK time) and as it late at night "I have had a small drink". But your last post intrigued me. My form load has the following:-

VB.Net 6.4:
'Determine if the interface has administer ACL's.
            Dim windowsID As WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim principle As New WindowsPrincipal(windowsID)
            If principle.IsInRole(WindowsBuiltInRole.Administrator) = False Then
                MessageBox.Show("You must run this application as administrator." + vbCr + vbCr +
                                "Administrator permissions are required to read and write" + vbCr +
                                "registry keys as well as to be able to install and" + vbCr +
                                "uninstall the Calibre Content Server Service." + vbCr + vbCr +
                                "To run as an administrator, right click the" + vbCr +
                                "calibre-service-interface.exe and choose from the menu" + vbCr +
                                "Run as administrator.", "Administrator Rights Required",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                'Terminate the program.
                Application.Exit()
            End If

Is there a better way i should be doing this? Using a .msi file (Microsoft Installer File) I get a "Do you want this ??? to make changes to your computer" warning. I would be happy with that approach.

I was going to use %windir% anyway being self-taught, I started out with MS batch files as "coding". The %EnviromentVariables% are very well understood be me. I will look in to your suggestion.

My original question. On a x64 machine with a x64 binary the "hard coded path" would work. But what about a x32 on a x64 machine or a x32 on a x32 machine. Is there only two paths to InstallUtile.exe? (assuming my app knew the %windir%)

I'll post again "sober" but thank you very much for the replies to date.
 
If your application that starts the process is running elevated then the process will be elevated too. (Verb has no effect and you should remove it)
only two paths to InstallUtile.exe?
For .Net Framework 4-4.8 yes.
 
Solution
JohnH,

I assumed both elevated privileges and "runas" were required. I'll correct it.

Thanks for clarifying the file paths for me.

I'll mark this as resolved now.

Benshaws.
 
If you need to start an elevated process from a non-elevated process you use the "runas" verb (and UseShellExecute=true)

Elevation is required for InstallUtil, yes.
 
"To run as an administrator, right click the" + vbCr + "calibre-service-interface.exe and choose from the menu" + vbCr + "Run as administrator."
Is there a better way i should be doing this?
If this app should always run elevated you can go into Application tab of project properties and click the "View Windows Settings", and change requestedExecutionLevel from "asInvoker" to "requireAdministrator". User will then always get the UAC prompt when starting the app and doesn't need to do this manually.
 
If this app should always run elevated you can go into Application tab of project properties and click the "View Windows Settings", and change requestedExecutionLevel from "asInvoker" to "requireAdministrator". User will then always get the UAC prompt when starting the app and doesn't need to do this manually.


Thanks for this. Probably what I should have researched in the first place.

Yes the form always requires elevated privladges. The form is used to install the service, uninstall, and write/modify a registry key - the listening port for the service.
 

Latest posts

Back
Top