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