Hardware/SMBIOS/SMBIOSGroup.cs
author moel.mich
Wed, 27 Jan 2010 18:12:51 +0000
changeset 2 105939e4eb7e
child 8 39067efce486
permissions -rw-r--r--
Fixed a NullReferenceException in the HDD SMART code.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Management;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.SMBIOS {
    44 
    45   public class SMBIOSGroup : IGroup {
    46 
    47     private Structure[] table;
    48 
    49     private BIOSInformation biosInformation = null;
    50 
    51     private BaseBoardInformation baseBoardInformation = null;
    52 
    53     public SMBIOSGroup() {
    54       int p = (int)System.Environment.OSVersion.Platform;
    55       if ((p == 4) || (p == 128))
    56         return;
    57           
    58       ManagementObjectCollection collection = new ManagementObjectSearcher(
    59          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    60 
    61       byte[] raw = null;
    62       foreach (ManagementObject mo in collection) {
    63         raw = (byte[])mo["SMBiosData"];
    64         break;
    65       }
    66 
    67       List<Structure> structureList = new List<Structure>();
    68       if (raw != null && raw.Length > 0) {
    69         int offset = 0;
    70         byte type = raw[offset];
    71         while (offset < raw.Length && type != 127) {
    72           
    73           type = raw[offset]; offset++;
    74           int length = raw[offset]; offset++;
    75           ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]); 
    76           offset += 2;
    77           
    78           byte[] data = new byte[length];
    79           Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
    80 
    81           List<string> stringsList = new List<string>();
    82           if (raw[offset] == 0)
    83             offset++;
    84           while (raw[offset] != 0) {   
    85             StringBuilder sb = new StringBuilder();
    86             while (raw[offset] != 0) {
    87               sb.Append((char)raw[offset]); offset++;
    88             }
    89             offset++;
    90             stringsList.Add(sb.ToString());
    91           }
    92           offset++;
    93           switch (type) {
    94             case 0x00:
    95               this.biosInformation = new BIOSInformation(
    96                 type, handle, data, stringsList.ToArray());
    97               structureList.Add(this.biosInformation); break;
    98             case 0x02: this.baseBoardInformation = new BaseBoardInformation(
    99                 type, handle, data, stringsList.ToArray());
   100               structureList.Add(this.baseBoardInformation); break;
   101             default: structureList.Add(new Structure(
   102               type, handle, data, stringsList.ToArray())); break;
   103           }
   104         }
   105       }
   106       table = structureList.ToArray();
   107     }
   108 
   109     public IHardware[] Hardware { get { return new IHardware[0]; } }
   110 
   111     public string GetReport() {
   112       StringBuilder r = new StringBuilder();
   113 
   114       r.AppendLine("SMBIOS");
   115       r.AppendLine();
   116 
   117       if (biosInformation != null) {
   118         r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   119         r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
   120         r.AppendLine();
   121       }
   122 
   123       if (baseBoardInformation != null) {
   124         r.Append("Mainboard Manufacturer: "); 
   125         r.AppendLine(baseBoardInformation.Manufacturer);
   126         r.Append("Mainboard Name: "); 
   127         r.AppendLine(baseBoardInformation.ProductName);
   128         r.AppendLine();
   129       }
   130      
   131       return r.ToString();
   132     }
   133 
   134     public void Close() { }
   135 
   136     public class Structure {
   137       private byte type;
   138       private ushort handle;
   139 
   140       private byte[] data;
   141       private string[] strings;
   142 
   143       public Structure(byte type, ushort handle, byte[] data, string[] strings) 
   144       {
   145         this.type = type;
   146         this.handle = handle;
   147         this.data = data;
   148         this.strings = strings;
   149       }
   150 
   151       public byte Type { get { return type; } }
   152 
   153       public ushort Handle { get { return handle; } }
   154     }
   155 
   156     public class BIOSInformation : Structure {
   157 
   158       private string vendor = "";
   159       private string version = "";
   160 
   161       public BIOSInformation(byte type, ushort handle, byte[] data,
   162         string[] strings)
   163         : base(type, handle, data, strings) {
   164 
   165         if (data[0x04] > 0 && data[0x04] <= strings.Length)
   166           vendor = strings[data[0x04] - 1];
   167 
   168         if (data[0x05] > 0 && data[0x05] <= strings.Length)
   169           version = strings[data[0x05] - 1];
   170       }
   171 
   172       public string Vendor { get { return vendor; } }
   173 
   174       public string Version { get { return version; } }
   175     }
   176 
   177     public class BaseBoardInformation : Structure {
   178 
   179       private string manufacturer  = "";
   180       private string productName = "";
   181 
   182       public BaseBoardInformation(byte type, ushort handle, byte[] data,
   183         string[] strings)
   184         : base(type, handle, data, strings) {
   185 
   186         if (data[0x04] > 0 && data[0x04] <= strings.Length)
   187           manufacturer = strings[data[0x04] - 1];
   188 
   189         if (data[0x05] > 0 && data[0x05] <= strings.Length)
   190           productName = strings[data[0x05] - 1];
   191       }
   192 
   193       public string Manufacturer { get { return manufacturer; } }
   194 
   195       public string ProductName { get { return productName; } }
   196     }
   197   }
   198 }