-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystemCommandPromptLauncher.cs
More file actions
99 lines (80 loc) · 3.11 KB
/
SystemCommandPromptLauncher.cs
File metadata and controls
99 lines (80 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace SYSTEMCommandPrompt
{
class SystemCommandPromptLauncher
{
TextWriter _logOutput;
public SystemCommandPromptLauncher(TextWriter logOutput)
{
_logOutput = logOutput;
}
void LogMessage(string message)
{
_logOutput.WriteLine(message);
}
void LogMessage(string format, params object[] args)
{
_logOutput.WriteLine(format, args);
}
public void LaunchIt()
{
Hashtable install_state = new Hashtable();
Installer uninstaller = null;
try
{
string pid_str = Process.GetCurrentProcess().Id.ToString();
string service_name = "SYSTEMCommandPrompt_" + pid_str;
string event_name = "Global\\" + service_name;
TransactedInstaller master_installer = new TransactedInstaller();
ServiceInstaller svcinst = new ServiceInstaller();
// don't set svcinst.Parent
svcinst.Description = "Temporary service, should be safe to delete.";
svcinst.DisplayName = "Temporary service (should be safe to delete)";
svcinst.ServiceName = service_name;
svcinst.StartType = ServiceStartMode.Manual;
ServiceProcessInstaller spi = new ServiceProcessInstaller();
// don't set spi.Parent
spi.Account = ServiceAccount.LocalSystem;
master_installer.Installers.AddRange(new Installer[] {
spi,
svcinst
});
master_installer.Context = new InstallContext();
master_installer.Context.Parameters["assemblypath"] = string.Format("\"{0}\" -service {1} {2}",
typeof(Form1).Assembly.Location,
service_name,
pid_str);
LogMessage("Creating service: {0} running as {1}", service_name, spi.Account);
master_installer.Install(install_state);
uninstaller = master_installer;
using (EventWaitHandle ready_signal = new EventWaitHandle(false, EventResetMode.AutoReset, event_name))
{
using (ServiceController sc = new ServiceController(service_name))
{
LogMessage("Starting service");
sc.Start();
}
LogMessage("Waiting for service to acknowledge...");
ready_signal.WaitOne(120000);
}
}
finally
{
if (uninstaller != null)
{
LogMessage("Deleting service...");
uninstaller.Uninstall(install_state);
}
}
}
}
}