Hardware/SMBIOS/SMBIOSGroup.cs
author moel.mich
Fri, 29 Jan 2010 19:20:44 +0000
changeset 8 39067efce486
parent 1 361e324a0ed4
child 9 e5adb0fd4917
permissions -rw-r--r--
Fixed ADL dll loading. The name of the dll is always atiadlxx, except when running as 32-bit app on 64-bit systems (atiadlxy in that case).
     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       List<Structure> structureList = new List<Structure>();
    59       
    60       try {
    61         ManagementObjectCollection collection = new ManagementObjectSearcher(
    62           "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
    63   
    64         byte[] raw = null;
    65         foreach (ManagementObject mo in collection) {
    66           raw = (byte[])mo["SMBiosData"];
    67           break;
    68         }      
    69       
    70         if (raw != null && raw.Length > 0) {
    71           int offset = 0;
    72           byte type = raw[offset];
    73           while (offset < raw.Length && type != 127) {
    74             
    75             type = raw[offset]; offset++;
    76             int length = raw[offset]; offset++;
    77             ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]); 
    78             offset += 2;
    79             
    80             byte[] data = new byte[length];
    81             Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
    82   
    83             List<string> stringsList = new List<string>();
    84             if (raw[offset] == 0)
    85               offset++;
    86             while (raw[offset] != 0) {   
    87               StringBuilder sb = new StringBuilder();
    88               while (raw[offset] != 0) {
    89                 sb.Append((char)raw[offset]); offset++;
    90               }
    91               offset++;
    92               stringsList.Add(sb.ToString());
    93             }
    94             offset++;
    95             switch (type) {
    96               case 0x00:
    97                 this.biosInformation = new BIOSInformation(
    98                   type, handle, data, stringsList.ToArray());
    99                 structureList.Add(this.biosInformation); break;
   100               case 0x02: this.baseBoardInformation = new BaseBoardInformation(
   101                   type, handle, data, stringsList.ToArray());
   102                 structureList.Add(this.baseBoardInformation); break;
   103               default: structureList.Add(new Structure(
   104                 type, handle, data, stringsList.ToArray())); break;
   105             }
   106           }
   107         }
   108       } catch (NotImplementedException) { }
   109       
   110       table = structureList.ToArray();
   111     }
   112 
   113     public IHardware[] Hardware { get { return new IHardware[0]; } }
   114 
   115     public string GetReport() {
   116       StringBuilder r = new StringBuilder();
   117 
   118       r.AppendLine("SMBIOS");
   119       r.AppendLine();
   120 
   121       if (biosInformation != null) {
   122         r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor);
   123         r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version);
   124         r.AppendLine();
   125       }
   126 
   127       if (baseBoardInformation != null) {
   128         r.Append("Mainboard Manufacturer: "); 
   129         r.AppendLine(baseBoardInformation.Manufacturer);
   130         r.Append("Mainboard Name: "); 
   131         r.AppendLine(baseBoardInformation.ProductName);
   132         r.AppendLine();
   133       }
   134      
   135       return r.ToString();
   136     }
   137 
   138     public void Close() { }
   139 
   140     public class Structure {
   141       private byte type;
   142       private ushort handle;
   143 
   144       private byte[] data;
   145       private string[] strings;
   146 
   147       public Structure(byte type, ushort handle, byte[] data, string[] strings) 
   148       {
   149         this.type = type;
   150         this.handle = handle;
   151         this.data = data;
   152         this.strings = strings;
   153       }
   154 
   155       public byte Type { get { return type; } }
   156 
   157       public ushort Handle { get { return handle; } }
   158     }
   159 
   160     public class BIOSInformation : Structure {
   161 
   162       private string vendor = "";
   163       private string version = "";
   164 
   165       public BIOSInformation(byte type, ushort handle, byte[] data,
   166         string[] strings)
   167         : base(type, handle, data, strings) {
   168 
   169         if (data[0x04] > 0 && data[0x04] <= strings.Length)
   170           vendor = strings[data[0x04] - 1];
   171 
   172         if (data[0x05] > 0 && data[0x05] <= strings.Length)
   173           version = strings[data[0x05] - 1];
   174       }
   175 
   176       public string Vendor { get { return vendor; } }
   177 
   178       public string Version { get { return version; } }
   179     }
   180 
   181     public class BaseBoardInformation : Structure {
   182 
   183       private string manufacturer  = "";
   184       private string productName = "";
   185 
   186       public BaseBoardInformation(byte type, ushort handle, byte[] data,
   187         string[] strings)
   188         : base(type, handle, data, strings) {
   189 
   190         if (data[0x04] > 0 && data[0x04] <= strings.Length)
   191           manufacturer = strings[data[0x04] - 1];
   192 
   193         if (data[0x05] > 0 && data[0x05] <= strings.Length)
   194           productName = strings[data[0x05] - 1];
   195       }
   196 
   197       public string Manufacturer { get { return manufacturer; } }
   198 
   199       public string ProductName { get { return productName; } }
   200     }
   201   }
   202 }