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).
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@8
    59
      
moel@8
    60
      try {
moel@8
    61
        ManagementObjectCollection collection = new ManagementObjectSearcher(
moel@8
    62
          "root\\WMI", "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get();
moel@8
    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@8
    68
        }      
moel@8
    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@8
    74
            
moel@8
    75
            type = raw[offset]; offset++;
moel@8
    76
            int length = raw[offset]; offset++;
moel@8
    77
            ushort handle = (ushort)((raw[offset] << 8) | raw[offset + 1]); 
moel@8
    78
            offset += 2;
moel@8
    79
            
moel@8
    80
            byte[] data = new byte[length];
moel@8
    81
            Array.Copy(raw, offset - 4, data, 0, length); offset += length - 4;
moel@8
    82
  
moel@8
    83
            List<string> stringsList = new List<string>();
moel@8
    84
            if (raw[offset] == 0)
moel@8
    85
              offset++;
moel@8
    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@8
   108
      } catch (NotImplementedException) { }
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@1
   147
      public Structure(byte type, ushort handle, byte[] data, string[] strings) 
moel@1
   148
      {
moel@1
   149
        this.type = type;
moel@1
   150
        this.handle = handle;
moel@1
   151
        this.data = data;
moel@1
   152
        this.strings = strings;
moel@1
   153
      }
moel@1
   154
moel@1
   155
      public byte Type { get { return type; } }
moel@1
   156
moel@1
   157
      public ushort Handle { get { return handle; } }
moel@1
   158
    }
moel@1
   159
moel@1
   160
    public class BIOSInformation : Structure {
moel@1
   161
moel@1
   162
      private string vendor = "";
moel@1
   163
      private string version = "";
moel@1
   164
moel@1
   165
      public BIOSInformation(byte type, ushort handle, byte[] data,
moel@1
   166
        string[] strings)
moel@1
   167
        : base(type, handle, data, strings) {
moel@1
   168
moel@1
   169
        if (data[0x04] > 0 && data[0x04] <= strings.Length)
moel@1
   170
          vendor = strings[data[0x04] - 1];
moel@1
   171
moel@1
   172
        if (data[0x05] > 0 && data[0x05] <= strings.Length)
moel@1
   173
          version = strings[data[0x05] - 1];
moel@1
   174
      }
moel@1
   175
moel@1
   176
      public string Vendor { get { return vendor; } }
moel@1
   177
moel@1
   178
      public string Version { get { return version; } }
moel@1
   179
    }
moel@1
   180
moel@1
   181
    public class BaseBoardInformation : Structure {
moel@1
   182
moel@1
   183
      private string manufacturer  = "";
moel@1
   184
      private string productName = "";
moel@1
   185
moel@1
   186
      public BaseBoardInformation(byte type, ushort handle, byte[] data,
moel@1
   187
        string[] strings)
moel@1
   188
        : base(type, handle, data, strings) {
moel@1
   189
moel@1
   190
        if (data[0x04] > 0 && data[0x04] <= strings.Length)
moel@1
   191
          manufacturer = strings[data[0x04] - 1];
moel@1
   192
moel@1
   193
        if (data[0x05] > 0 && data[0x05] <= strings.Length)
moel@1
   194
          productName = strings[data[0x05] - 1];
moel@1
   195
      }
moel@1
   196
moel@1
   197
      public string Manufacturer { get { return manufacturer; } }
moel@1
   198
moel@1
   199
      public string ProductName { get { return productName; } }
moel@1
   200
    }
moel@1
   201
  }
moel@1
   202
}