Skip to content

Commit 11a2ddd

Browse files
committed
portability fixes:
- store settings in registry; - store .sys file in temp folder;
1 parent d45f711 commit 11a2ddd

6 files changed

Lines changed: 84 additions & 140 deletions

File tree

.editorconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ dotnet_style_qualification_for_method = false:suggestion
3838
dotnet_style_qualification_for_event = false:suggestion
3939

4040
# Types: use keywords instead of BCL types, and permit var only when the type is clear
41-
csharp_style_var_for_built_in_types = false:suggestion
42-
csharp_style_var_when_type_is_apparent = false:none
43-
csharp_style_var_elsewhere = false:suggestion
41+
csharp_style_var_for_built_in_types = true:suggestion
42+
csharp_style_var_when_type_is_apparent = true:suggestion
43+
csharp_style_var_elsewhere = true:suggestion
4444
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
4545
dotnet_style_predefined_type_for_member_access = true:suggestion
4646

GUI/MainForm.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public partial class MainForm : Form {
4848
private readonly UserOption logSensors;
4949
private readonly UserRadioGroup loggingInterval;
5050
private readonly Logger logger;
51+
private readonly string configFilePath;
5152

5253
private bool selectionDragging = false;
5354
//private readonly Timer checkUpdatesTimer;
@@ -59,7 +60,9 @@ public MainForm() {
5960
Icon = Icon.ExtractAssociatedIcon(Updater.CurrentFileLocation);
6061

6162
settings = new PersistentSettings();
62-
settings.Load(Application.ExecutablePath);
63+
64+
configFilePath = Path.ChangeExtension(Application.ExecutablePath, ".config");
65+
settings.Load(configFilePath);
6366

6467
unitManager = new UnitManager(settings);
6568

@@ -383,16 +386,14 @@ private void SaveConfiguration() {
383386
settings.SetValue("listenerPort", server.ListenerPort);
384387
}
385388

386-
string fileName = Path.ChangeExtension(
387-
Application.ExecutablePath, ".config");
388389
try {
389-
settings.Save(fileName);
390+
settings.Save(configFilePath);
390391
} catch (UnauthorizedAccessException) {
391-
MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
392+
MessageBox.Show("Access to the path '" + configFilePath + "' is denied. " +
392393
"The current settings could not be saved.",
393394
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
394395
} catch (IOException) {
395-
MessageBox.Show("The path '" + fileName + "' is not writeable. " +
396+
MessageBox.Show("The path '" + configFilePath + "' is not writeable. " +
396397
"The current settings could not be saved.",
397398
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
398399
}

Hardware/Ring0.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ private static IOControlCode
2323
IOControlCode.Access.Any),
2424
IOCTL_OLS_READ_MSR = new IOControlCode(OLS_TYPE, 0x821,
2525
IOControlCode.Access.Any),
26-
IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822,
26+
IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822,
2727
IOControlCode.Access.Any),
2828
IOCTL_OLS_READ_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x833,
2929
IOControlCode.Access.Read),
30-
IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836,
30+
IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836,
3131
IOControlCode.Access.Write),
32-
IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851,
32+
IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851,
3333
IOControlCode.Access.Read),
3434
IOCTL_OLS_WRITE_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x852,
3535
IOControlCode.Access.Write),
@@ -40,46 +40,48 @@ private static Assembly GetAssembly() {
4040
return typeof(Ring0).Assembly;
4141
}
4242

43-
private static string GetTempFileName() {
44-
45-
// try to create one in the application folder
46-
string location = GetAssembly().Location;
47-
if (!string.IsNullOrEmpty(location)) {
48-
try {
49-
string fileName = Path.ChangeExtension(location, ".sys");
50-
using (FileStream stream = File.Create(fileName)) {
51-
return fileName;
52-
}
53-
} catch (Exception) { }
43+
private static string GetTempFileName(bool useAppFolder = false) {
44+
45+
if (useAppFolder) {
46+
// try to create one in the application folder
47+
string location = GetAssembly().Location;
48+
if (!string.IsNullOrEmpty(location)) {
49+
try {
50+
string fileName = Path.ChangeExtension(location, ".sys");
51+
using (FileStream stream = File.Create(fileName)) {
52+
return fileName;
53+
}
54+
} catch (Exception) { }
55+
}
5456
}
5557

5658
// if this failed, try to get a file in the temporary folder
5759
try {
58-
return Path.GetTempFileName();
59-
} catch (IOException) {
60+
return Path.GetTempFileName();
61+
} catch (IOException) {
6062
// some I/O exception
61-
}
62-
catch (UnauthorizedAccessException) {
63+
}
64+
catch (UnauthorizedAccessException) {
6365
// we do not have the right to create a file in the temp folder
6466
}
6567
catch (NotSupportedException) {
6668
// invalid path format of the TMP system environment variable
6769
}
68-
70+
6971
return null;
7072
}
7173

7274
private static bool ExtractDriver(string fileName) {
7375
string resourceName = "OpenHardwareMonitor.Hardware." +
74-
(OperatingSystem.Is64BitOperatingSystem ? "WinRing0x64.sys" :
76+
(OperatingSystem.Is64BitOperatingSystem ? "WinRing0x64.sys" :
7577
"WinRing0.sys");
7678

7779
string[] names = GetAssembly().GetManifestResourceNames();
7880
byte[] buffer = null;
7981
for (int i = 0; i < names.Length; i++) {
8082
if (names[i].Replace('\\', '.') == resourceName) {
8183
using (Stream stream = GetAssembly().
82-
GetManifestResourceStream(names[i]))
84+
GetManifestResourceStream(names[i]))
8385
{
8486
buffer = new byte[stream.Length];
8587
stream.Read(buffer, 0, buffer.Length);
@@ -95,16 +97,16 @@ private static bool ExtractDriver(string fileName) {
9597
target.Write(buffer, 0, buffer.Length);
9698
target.Flush();
9799
}
98-
} catch (IOException) {
100+
} catch (IOException) {
99101
// for example there is not enough space on the disk
100-
return false;
102+
return false;
101103
}
102104

103105
// make sure the file is actually writen to the file system
104106
for (int i = 0; i < 20; i++) {
105107
try {
106108
if (File.Exists(fileName) &&
107-
new FileInfo(fileName).Length == buffer.Length)
109+
new FileInfo(fileName).Length == buffer.Length)
108110
{
109111
return true;
110112
}
@@ -113,22 +115,22 @@ private static bool ExtractDriver(string fileName) {
113115
Thread.Sleep(10);
114116
}
115117
}
116-
118+
117119
// file still has not the right size, something is wrong
118120
return false;
119121
}
120122

121123
public static void Open() {
122124
// no implementation for unix systems
123125
if (OperatingSystem.IsUnix)
124-
return;
125-
126+
return;
127+
126128
if (driver != null)
127129
return;
128130

129131
// clear the current report
130132
report.Length = 0;
131-
133+
132134
driver = new KernelDriver("WinRing0_1_2_0");
133135
driver.Open();
134136

@@ -147,7 +149,7 @@ public static void Open() {
147149
}
148150
} else {
149151
string errorFirstInstall = installError;
150-
152+
151153
// install failed, try to delete and reinstall
152154
driver.Delete();
153155

@@ -180,11 +182,11 @@ public static void Open() {
180182
if (File.Exists(fileName))
181183
File.Delete(fileName);
182184
fileName = null;
183-
} catch (IOException) { }
185+
} catch (IOException) { }
184186
catch (UnauthorizedAccessException) { }
185187
}
186188

187-
if (!driver.IsOpen)
189+
if (!driver.IsOpen)
188190
driver = null;
189191

190192
string isaMutexName = "Global\\Access_ISABUS.HTP.Method";
@@ -239,7 +241,7 @@ public static void Close() {
239241
try {
240242
File.Delete(fileName);
241243
fileName = null;
242-
} catch (IOException) { }
244+
} catch (IOException) { }
243245
catch (UnauthorizedAccessException) { }
244246
}
245247
}
@@ -303,7 +305,7 @@ public static bool Rdmsr(uint index, out uint eax, out uint edx) {
303305
}
304306

305307
public static bool RdmsrTx(uint index, out uint eax, out uint edx,
306-
GroupAffinity affinity)
308+
GroupAffinity affinity)
307309
{
308310
var previousAffinity = ThreadAffinity.Set(affinity);
309311

@@ -370,8 +372,8 @@ private struct ReadPciConfigInput {
370372
public uint RegAddress;
371373
}
372374

373-
public static bool ReadPciConfig(uint pciAddress, uint regAddress,
374-
out uint value)
375+
public static bool ReadPciConfig(uint pciAddress, uint regAddress,
376+
out uint value)
375377
{
376378
if (driver == null || (regAddress & 3) != 0) {
377379
value = 0;
@@ -383,7 +385,7 @@ public static bool ReadPciConfig(uint pciAddress, uint regAddress,
383385
input.RegAddress = regAddress;
384386

385387
value = 0;
386-
return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input,
388+
return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input,
387389
ref value);
388390
}
389391

@@ -394,8 +396,8 @@ private struct WritePciConfigInput {
394396
public uint Value;
395397
}
396398

397-
public static bool WritePciConfig(uint pciAddress, uint regAddress,
398-
uint value)
399+
public static bool WritePciConfig(uint pciAddress, uint regAddress,
400+
uint value)
399401
{
400402
if (driver == null || (regAddress & 3) != 0)
401403
return false;

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
1616

17-
[assembly: AssemblyVersion("0.9.*")]
17+
[assembly: AssemblyVersion("1.0.*")]

0 commit comments

Comments
 (0)