@@ -7,97 +7,82 @@ namespace CrashReporterDotNET
77{
88 internal static class HelperMethods
99 {
10- [ DllImport ( "kernel32" , SetLastError = true , CallingConvention = CallingConvention . Winapi ) ]
11- private static extern IntPtr LoadLibrary ( string libraryName ) ;
10+ [ DllImport ( "kernel32.dll" ) ]
11+ static extern IntPtr GetCurrentProcess ( ) ;
1212
13- [ DllImport ( "kernel32" , SetLastError = true , CallingConvention = CallingConvention . Winapi ) ]
14- private static extern IntPtr GetProcAddress ( IntPtr hwnd , string procedureName ) ;
13+ [ DllImport ( "kernel32.dll" ) ]
14+ static extern IntPtr GetModuleHandle ( string moduleName ) ;
1515
16- private static bool IsOS64Bit ( )
17- {
18- return IntPtr . Size == 8 || ( IntPtr . Size == 4 && Is32BitProcessOn64BitProcessor ( ) ) ;
19- }
16+ [ DllImport ( "kernel32" ) ]
17+ static extern IntPtr GetProcAddress ( IntPtr hModule , string procName ) ;
2018
21- private static IsWow64ProcessDelegate GetIsWow64ProcessDelegate ( )
22- {
23- IntPtr handle = LoadLibrary ( "kernel32" ) ;
19+ [ DllImport ( "kernel32.dll" ) ]
20+ static extern bool IsWow64Process ( IntPtr hProcess , out bool wow64Process ) ;
2421
25- if ( handle != IntPtr . Zero )
22+ private static bool Is64BitOperatingSystem ( )
23+ {
24+ // Check if this process is natively an x64 process. If it is, it will only run on x64 environments, thus, the environment must be x64.
25+ if ( IntPtr . Size == 8 )
26+ return true ;
27+ // Check if this process is an x86 process running on an x64 environment.
28+ IntPtr moduleHandle = GetModuleHandle ( "kernel32" ) ;
29+ if ( moduleHandle != IntPtr . Zero )
2630 {
27- IntPtr fnPtr = GetProcAddress ( handle , "IsWow64Process" ) ;
28-
29- if ( fnPtr != IntPtr . Zero )
31+ IntPtr processAddress = GetProcAddress ( moduleHandle , "IsWow64Process" ) ;
32+ if ( processAddress != IntPtr . Zero )
3033 {
31- return
32- ( IsWow64ProcessDelegate )
33- Marshal . GetDelegateForFunctionPointer ( fnPtr , typeof ( IsWow64ProcessDelegate ) ) ;
34+ bool result ;
35+ if ( IsWow64Process ( GetCurrentProcess ( ) , out result ) && result )
36+ return true ;
3437 }
3538 }
36-
37- return null ;
38- }
39-
40- private static bool Is32BitProcessOn64BitProcessor ( )
41- {
42- IsWow64ProcessDelegate fnDelegate = GetIsWow64ProcessDelegate ( ) ;
43-
44- if ( fnDelegate == null )
45- {
46- return false ;
47- }
48-
49- bool isWow64 ;
50- bool retVal = fnDelegate . Invoke ( Process . GetCurrentProcess ( ) . Handle , out isWow64 ) ;
51-
52- if ( retVal == false )
53- {
54- return false ;
55- }
56-
57- return isWow64 ;
39+ // The environment must be an x86 environment.
40+ return false ;
5841 }
5942
6043 private static string HKLM_GetString ( string key , string value )
6144 {
6245 try
6346 {
6447 RegistryKey registryKey = Registry . LocalMachine . OpenSubKey ( key ) ;
65- if ( registryKey == null ) return "" ;
66- return registryKey . GetValue ( value ) . ToString ( ) ;
48+ return registryKey ? . GetValue ( value ) . ToString ( ) ?? String . Empty ;
6749 }
6850 catch
6951 {
70- return "" ;
52+ return String . Empty ;
7153 }
7254 }
7355
7456 public static string GetOSVersion ( )
7557 {
76- return $ "{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentMajorVersionNumber" ) } .{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentMinorVersionNumber" ) } .{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentBuildNumber" ) } .0";
58+ if ( ! string . IsNullOrEmpty ( HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentMajorVersionNumber" ) ) )
59+ {
60+ return
61+ $ "{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentMajorVersionNumber" ) } .{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentMinorVersionNumber" ) } .{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentBuildNumber" ) } .0";
62+ }
63+ return $ "{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentVersion" ) } .{ HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentBuildNumber" ) } .0";
7764 }
7865
79- private delegate bool IsWow64ProcessDelegate ( [ In ] IntPtr handle , [ Out ] out bool isWow64Process ) ;
80-
8166 public static string GetWindowsVersion ( )
8267 {
8368 string osArchitecture ;
8469 try
8570 {
86- osArchitecture = IsOS64Bit ( ) ? "64-bit" : "32-bit" ;
71+ osArchitecture = Is64BitOperatingSystem ( ) ? "64-bit" : "32-bit" ;
8772 }
8873 catch ( Exception )
8974 {
9075 osArchitecture = "32/64-bit (Undetermined)" ;
9176 }
9277 string productName = HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "ProductName" ) ;
9378 string csdVersion = HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CSDVersion" ) ;
94- string currentBuild = HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentBuild " ) ;
95- if ( productName != "" )
79+ string currentBuild = HKLM_GetString ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" , "CurrentBuildNumber " ) ;
80+ if ( ! string . IsNullOrEmpty ( productName ) )
9681 {
9782 return
98- $ "{ ( productName . StartsWith ( "Microsoft" ) ? "" : "Microsoft " ) } { productName } { ( csdVersion != "" ? " " + csdVersion : "" ) } { osArchitecture } (OS Build { currentBuild } )";
83+ $ "{ ( productName . StartsWith ( "Microsoft" ) ? "" : "Microsoft " ) } { productName } { ( ! string . IsNullOrEmpty ( csdVersion ) ? " " + csdVersion : String . Empty ) } { osArchitecture } (OS Build { currentBuild } )";
9984 }
100- return "" ;
85+ return String . Empty ;
10186 }
10287 }
10388}
0 commit comments