@@ -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 ;
0 commit comments