Initial checkin

This commit is contained in:
chudov
2008-11-26 18:57:40 +00:00
parent 0cb204d2bc
commit 16cbf58dd4
56 changed files with 12266 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B75FA7AD-968E-4990-B342-1B4B17C850DF}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Bwg.Hardware</RootNamespace>
<AssemblyName>Bwg.Hardware</AssemblyName>
<SccProjectName>
</SccProjectName>
<SccLocalPath>
</SccLocalPath>
<SccAuxPath>
</SccAuxPath>
<SccProvider>
</SccProvider>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Bwg.Hardware.XML</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>bin\Debug\Bwg.Hardware.XML</DocumentationFile>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>bin\Debug\Bwg.Hardware.XML</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HardwareDevice.cs" />
<Compile Include="HardwareManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Bwg.Hardware
{
/// <summary>
/// This class represents a hardware device as extracted from the registry.
/// </summary>
public class HardwareDevice
{
#region private member variables
private string m_name;
private string m_desc;
private string m_class;
private Guid m_class_guid;
private string m_key_name ;
private string m_location;
private string[] m_hardware;
#endregion
#region constructor
/// <summary>
/// The constructor for a device.
/// </summary>
/// <param name="key">the name of the registry key that contains info about this device</param>
public HardwareDevice(string key)
{
m_key_name = key;
m_name = string.Empty ;
m_desc = string.Empty ;
m_location = string.Empty;
m_hardware = new string[0];
}
#endregion
#region public methods
/// <summary>
/// Add a new hardware address to the device
/// </summary>
/// <param name="addr">the new address to add to the device</param>
public void AddHardware(string addr)
{
int count = m_hardware.GetLength(0);
Array.Resize(ref m_hardware, count + 1);
m_hardware[count] = addr;
}
#endregion
#region public properties
/// <summary>
/// The name of the key where this information was extracted
/// </summary>
public string KeyName
{
get { return m_key_name; }
}
/// <summary>
/// This property is the (friendly) name of the device
/// </summary>
public string Name
{
get
{
if (m_name != string.Empty)
return m_name;
if (m_desc != string.Empty)
return m_desc;
return m_key_name;
}
set
{
m_name = value;
}
}
/// <summary>
/// This property is the description of the device
/// </summary>
public string Description
{
get
{
return m_desc;
}
set
{
m_desc = value;
}
}
/// <summary>
/// This property is the class that the device belong to
/// </summary>
public string Class
{
get
{
return m_class;
}
set
{
m_class = value;
}
}
/// <summary>
/// This property is the GUID for the class that the device belongs to
/// </summary>
public Guid ClassGUID
{
get
{
return m_class_guid;
}
set
{
m_class_guid = value;
}
}
/// <summary>
/// This property is the location of the device
/// </summary>
public string Location
{
get { return m_location; }
set { m_location = value; }
}
/// <summary>
/// This class is the hardware addresses assocaited with the device
/// </summary>
public string[] Hardware
{
get
{
return m_hardware;
}
}
/// <summary>
/// This property returns true if the device is an IDE device
/// </summary>
public bool IsIde
{
get
{
return HardwareStartsWith("IDE");
}
}
/// <summary>
/// This property returns true if this is a USB device
/// </summary>
public bool IsUsb
{
get
{
return HardwareStartsWith("USB") ;
}
}
/// <summary>
/// This property returns true if this is a Firewire device
/// </summary>
public bool IsFirewire
{
get
{
return HardwareStartsWith("SBP2");
}
}
/// <summary>
/// This property returns true if this is a SCSI device
/// </summary>
public bool IsScsi
{
get
{
return HardwareStartsWith("SCSI");
}
}
#endregion
#region private methods
/// <summary>
/// This method returns true if the hardware address start with the string given
/// </summary>
/// <param name="begin">the string to start with</param>
/// <returns>true if the string begins with the right value, otherwise false</returns>
public bool HardwareStartsWith(string begin)
{
foreach (string str in m_hardware)
{
if (str.StartsWith(begin))
return true;
}
return false;
}
#endregion
}
}

View File

@@ -0,0 +1,209 @@
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Security;
using Microsoft.Win32;
namespace Bwg.Hardware
{
/// <summary>
/// This class is the manager that reads the registry and determines which devices exist
/// </summary>
public class HardwareManager : IEnumerable<HardwareDevice>, IEnumerable
{
#region private member variables
private IList<HardwareDevice> m_devices;
#endregion
#region constructors
/// <summary>
/// The constructor for the device manager, creates a list for devices to be added.
/// </summary>
public HardwareManager()
{
m_devices = new List<HardwareDevice>();
}
#endregion
/// <summary>
/// Return an enumerator for iterating over all of the tracks on the
/// disk.
/// </summary>
/// <returns>iterator</returns>
public IEnumerator<HardwareDevice> GetEnumerator()
{
return m_devices.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return m_devices.GetEnumerator();
}
/// <summary>
/// Open a given machine, or the default machine if no machine is given
/// </summary>
/// <param name="keyname"></param>
/// <returns></returns>
public bool OpenMachine(string keyname)
{
if (keyname == string.Empty)
keyname = "System\\CurrentControlSet\\Enum";
RegistryKey key = Registry.LocalMachine.OpenSubKey(keyname);
if (key == null)
return false;
CheckForHardware(key);
key.Close();
return true;
}
/// <summary>
/// Find a specific device given its class, vendor, and product
/// </summary>
/// <param name="classname">the class of the device</param>
/// <param name="vendor">the vendor id of the device</param>
/// <param name="product">the product id of the device</param>
/// <returns></returns>
public HardwareDevice FindDevice(string classname, string vendor, string product)
{
foreach (HardwareDevice dev in m_devices)
{
if (dev.Class != classname)
continue;
if (dev.Name.Contains(vendor) && dev.Name.Contains(product))
{
if (dev.IsUsb && dev.Location.Length == 0)
dev.Location = FindUsbLocation(dev);
return dev;
}
}
return null;
}
#region private methods
private string FindUsbLocation(HardwareDevice cddev)
{
string result = string.Empty;
int index = cddev.KeyName.LastIndexOf('&');
if (index == -1)
return result;
string devname = cddev.KeyName.Substring(0, index);
foreach (HardwareDevice dev in m_devices)
{
if (dev.Class == "USB" && dev.KeyName == devname)
return dev.Location;
}
return result;
}
private static bool FindClassString(string s1)
{
return s1 == "Class";
}
private static bool FindClassGUIDString(string s1)
{
return s1 == "ClassGUID";
}
private void CreateDevice(RegistryKey key)
{
string keyname = key.ToString();
string onekey ;
HardwareDevice dev;
try
{
onekey = Path.GetFileName(keyname);
}
catch (ArgumentException)
{
return;
}
dev = new HardwareDevice(onekey);
object value;
value = key.GetValue("Class");
if (value != null)
{
if (value is string)
dev.Class = (string)value;
else if (value is string[])
{
dev.Class = ((string[])value)[0];
}
}
value = key.GetValue("ClassGUID");
if (value != null && value is string)
dev.ClassGUID = new Guid((string)value);
value = key.GetValue("DeviceDesc");
if (value != null && value is string)
dev.Description = (string)value;
value = key.GetValue("FriendlyName");
if (value != null && value is string)
dev.Name = (string)value;
value = key.GetValue("LocationInformation");
if (value != null && value is string)
dev.Location = (string)value;
value = key.GetValue("HardwareID");
if (value != null)
{
if (value is string)
dev.AddHardware((string)value);
else if (value is string[])
{
foreach (string str in (string[])value)
dev.AddHardware(str);
}
}
m_devices.Add(dev);
}
private void CheckForHardware(RegistryKey key)
{
string[] values = key.GetValueNames();
if (Array.Find(values, FindClassString) != null && Array.Find(values, FindClassGUIDString) != null)
{
// This is a device
CreateDevice(key);
}
else
{
values = key.GetSubKeyNames();
foreach (string str in values)
{
try
{
RegistryKey subkey = key.OpenSubKey(str);
if (subkey != null)
{
CheckForHardware(subkey);
subkey.Close();
}
}
catch (SecurityException)
{
}
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Bwg.Hardware")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("BwgSoftware")]
[assembly: AssemblyProduct("BwgBurn")]
[assembly: AssemblyCopyright("Copyright © 2006 by Jack W. Griffin, Jr.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("de5dc145-3520-47ee-8988-205a142c6177")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.0.7.1")]
[assembly: AssemblyFileVersion("0.0.7.1")]