Saturday, February 5, 2011

How to make program to get device drivers - Part 2

In order to get device drivers which installed on your computer we will write method its name (GetDriversList), this method is it most important part of our program which it consists of sub-method its name (GetDriver), which get and store driver informaion.
So the first stage is to bring a list containing the device drivers installed on your computer, which, as we mentioned previously that it found in the Registry Editor, and then brought the information contained in the key of each driver.
 
To store information about each driver, you declare the structure (DriverInfo) containing 6 fields to store the name, version, date, manufacture ....... of each driver.
 
The method that bring in device drivers (GetDriversList), supplied with a parameter determines if we want to get all the drivers, including drivers that are installed with the system or only drivers that you installed after you install the system. In order to get that we test the value of the manufacturer (drvManufacture) If it is equal to (Microsoft), this means that the driver was already installed with the system and we do not need to do a backup copy of it.

struct DriverInfo  //structure for storing driver informations
{
    public string drvName;
    public string drvVersion;
    public string drvManufacture;
    public string drvDate;
    public string drvInfPath;
    public string drvInfSection;
}


//this methode collect and store driver info using the given driver path
DriverInfo GerDriver(string DriverPath)  
{
    DriverInfo temp = new DriverInfo();

    try
    {   //open the key which contains driver info
        RegistryKey tKey = Registry.LocalMachine.OpenSubKey(DriverPath); 

        temp.drvManufacture = tKey.GetValue("ProviderName", "").ToString();
        temp.drvName = tKey.GetValue("DriverDesc", "").ToString();
        temp.drvDate = tKey.GetValue("DriverDate", "").ToString();
        temp.drvVersion = tKey.GetValue("DriverVersion", "").ToString();
        temp.drvInfPath = tKey.GetValue("InfPath", "").ToString();
        temp.drvInfSection = tKey.GetValue("InfSection", "").ToString();
    }
    catch (SecurityException exs)
    {
        MessageBox.Show("SecurityException:\n" + exs.Message);
    }
    catch (Exception ex)
    {
        MessageBox.Show("GetDriver\\Error:\n" + ex.Message);
    }

    return temp;
}

void GetDriversList(bool bAllDrivers = true//main methode in our program
//bAllDrivers determines if we will extract both windows and device drivers or
   //just devices drivers
    int i, j, iCnt;

    //path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class";
    RegistryKey hKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\", false);

    string[] subKeys = hKey.GetSubKeyNames();
    string[] guidSubKeys;

    for (i = 0; i < subKeys.Length; i++) //loop through all subkeys
    {   //get the subkeys of the driver key
        guidSubKeys = hKey.OpenSubKey(subKeys[i].ToString()).GetSubKeyNames(); 
        //loop through all subkeys of the driver key
        for (j = 0; j < guidSubKeys.Length; j++) 
        { 
      //this key exist in all drivers keys and it doesn't benefit us so we skip it
            if (guidSubKeys[j].ToUpper() == "PROPERTIES"
                continue;
            try
            {   //delete the first part of the path "HKEY_LOCAL_MACHINE\"
                string path = hKey.Name.Substring(19); 
                path += "\\" + subKeys[i].ToString() + "\\" +    guidSubKeys[j].ToString();

                //path now will be:
                //SYSTEM\CurrentControlSet\Control\Class\subkey[i]\guidSubkey[j]
                DriverInfo tmp = GerDriver(path); //collect driver info

                if (tmp.drvName.Trim() != "") //add the driver to gridview
                {
                    if (bAllDrivers)
                        goto AddIt;
                    else if (bAllDrivers == false && (tmp.drvManufacture.ToUpper() != "MICROSOFT"))
                        goto AddIt;
                    else
                        continue;


                AddIt:
                    lvwDrivers.Items.Add(tmp.drvName);
                    iCnt = lvwDrivers.Items.Count - 1;
                    lvwDrivers.Items[iCnt].SubItems.Add(tmp.drvManufacture);
                    lvwDrivers.Items[iCnt].SubItems.Add(tmp.drvDate);
                    lvwDrivers.Items[iCnt].SubItems.Add(tmp.drvVersion);
                    lvwDrivers.Items[iCnt].SubItems.Add(tmp.drvInfPath);
                    lvwDrivers.Items[iCnt].SubItems.Add(tmp.drvInfSection);
                }
            }
            catch (SecurityException ex)
            {
                MessageBox.Show("SecurityException In GetDriversList:\n" + "Key: " + subKeys[i].ToString() + "\\" + guidSubKeys[j].ToString());
                continue;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("NullReferenceException IN GetDriversList:\n" + ex.Message);
            }
        }
    }
}

4 comments:

  1. good code but there is another idea
    but for now good article

    ReplyDelete
  2. Hi, Patrick here from the SW-Box blog(blog.sw-box.com). Just wondering if you would like to exchange blogroll links with me. If yes, please leave me a comment on one of my recent blog posts. Then I'll link to you first. When you have a free moment, just link back ok? If you're not interested, that's cool too. In which case, happy blogging and have a great day.

    Regards

    Patrick

    ReplyDelete
  3. I put your link in the "linkroll" box on the right side below. So please link back. Have a nice day. :D

    ReplyDelete
  4. Thanks for link the blog,as you asked I link your Arabic version blog, so can you link back there too? Thanks.

    ReplyDelete