-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCom0comSetup.cs
More file actions
147 lines (133 loc) · 4.78 KB
/
Com0comSetup.cs
File metadata and controls
147 lines (133 loc) · 4.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
namespace MainPower.Com0com.Redirector
{
public static class Com0comSetup
{
private static string _com0comSetupc = @"C:\Program Files (x86)\com0com\setupc.exe";
private static string _com0comSetupg = @"C:\Program Files (x86)\com0com\setupg.exe";
/// <summary>
/// Get all com0com Port Pairs currently installed in the system.
/// </summary>
/// <returns></returns>
public static ObservableCollection<Com0comPortPair> GetPortPairs()
{
ObservableCollection<Com0comPortPair> ports = new ObservableCollection<Com0comPortPair>();
//get the output from setupc --detail-prms list
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = _com0comSetupc,
Arguments = "--detail-prms list",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
try
{
string line = proc.StandardOutput.ReadLine();
//get port number
Regex regex = new Regex(@"(?<=CNC[A,B])\d+(?=\s)");
int portnum = int.Parse(regex.Match(line).Value);
Com0comPortPair pair = ports.FirstOrDefault(d => d.PairNumber == portnum);
if (pair == null)
{
pair = new Com0comPortPair(portnum);
ports.Add(pair);
}
regex = new Regex(@"(?<=CNC)[A,B](?=\d+\s)");
string portAB = regex.Match(line).Value;
if (portAB == "A")
{
pair.PortConfigStringA = line;
}
else if (portAB == "B")
{
pair.PortConfigStringB = line;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
return ports;
}
public static bool CreatePortPair(string portNameA)
{
if (UacHelper.IsUacEnabled)
if (!UacHelper.IsProcessElevated)
return false;
if (!UacHelper.IsAdministrator())
return false;
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(_com0comSetupc),
FileName = _com0comSetupc,
Arguments = portNameA == "-" ? "install - -" : "install PortName=" + portNameA + " -",
UseShellExecute = true,
CreateNoWindow = false,
Verb = "runas"
}
};
proc.Start();
//TODO: add a timeout here
while (!proc.HasExited) { }
return proc.ExitCode == 0;
}
public static void LaunchSetupg()
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(_com0comSetupc),
FileName = _com0comSetupg,
UseShellExecute = true,
CreateNoWindow = false,
Verb = "runas"
}
};
proc.Start();
}
public static bool DeletePortPair(int n)
{
if (UacHelper.IsUacEnabled)
if (!UacHelper.IsProcessElevated)
return false;
if (!UacHelper.IsAdministrator())
return false;
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(_com0comSetupc),
FileName = _com0comSetupc,
Arguments = "remove " + n.ToString(),
UseShellExecute = true,
CreateNoWindow = false,
Verb = "runas"
}
};
proc.Start();
//TODO: add a timeout here
while (!proc.HasExited) { }
return proc.ExitCode == 0;
}
}
}