Obtain driver information based on hardwareid in C #

Source: Internet
Author: User

Recently, you need to obtain the Device Driver Based on the device's hardwareid.ProgramInformation, such as the driver version. After exploration, two different solutions have been obtained. The two solutions have their own merits and can be written and shared with you.

1. Use the win32_pnpsigneddriver class in WMI

Win32_pnpsigneddriver details: http://msdn2.microsoft.com/en-us/library/aa394354.aspx
WMI (Windows Management Instrumentation) is the most convenient method. The driverversion we need can be obtained based on the Program snippets below.

Private   String Getdriverversion ( String Hardwareid)
{
String Querystring =   " Select hardwareid, driverversion from win32_pnpsigneddriver " ;
Selectquery =   New Selectquery (querystring );
Managementobjectsearcher searcher =   New Managementobjectsearcher (selectquery );

Foreach (Managementobject Mo In Searcher. Get ())
{
Object Tempid = Mo [ " Hardwareid " ];
If (Tempid ! = Null   && Tempid. tostring (). toupper () = Hardwareid. Trim (). toupper ())
{
Return Mo [ " Driverversion " ]. Tostring ();
}
}

Return "Unknownversion";
}

The way to get the driver is very simple, but there is a very serious problem that is efficiency. On average, it takes about 3 seconds to obtain a driverversion for each query. This time is unacceptable for our applications. Maybe you will say, why don't you need more restrictions to further reduce the number of queries?

If we change the connection string:

String Querystring =   " Select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid = 'somehardware' " ;

Program efficiency has not been significantly improved. We also found a problem. If somehardware contains a '\' (that is, hardwareid = 'some \ hardware '), we will get an "invalid query" exception. However, it is normal to query in wmitools. I hope someone can give me some advice. Finally, according to the description in msdn, only Windows Vista, Windows XP, and Windows 2003 support this class. Because our program needs to run under 2000, this method does not work.

2 Use pinvoke

Since WMI cannot be used, we thought of using pinvoke to call windows APIs. By querying msdn, you can use the setupdixxxx function to implement our functions. The basic idea is as follows:
(1) Use the setupdigetclassdevs function to obtain a class containing all device information.
(2) Use setupdienumdeviceinfo to obtain the information of a specific device and save it in a structure named sp_devinfo_data.
(3) Use setupdigetdeviceregistryproperty to obtain the device's hardwareid, Which is compared with the input hardwareid.
(4) If the two hardwareids are the same, use setupdibuilddriverinfolist to obtain the driver information list of the device.
(5) Use setupdienumdriverinfo to traverse the driver information list and obtain all required information, which is saved in a structure named sp_drvinfo_data.
(6) Get the driver version from sp_drvinfo_data. Is a dwordlong number, which needs to be converted to the x. x structure.

Note that the above functions are encapsulated in setupapi. dll. To use these functions, you must install Windows DDK.

In C #, when using pinvoke to call windows APIs, we need to pay attention to the corresponding type and structure alignment. For example, the sp_devinfo_data structure above must be declared as follows:

[Structlayout (layoutkind. Sequential, pack =   4 , Charset = Charset. Auto)]
Public   Struct Sp_devinfo_data
{
Public   Int Cbsize;
Public Guid classguid;
Public Intptr devinst;
Public Intptr reserved;
}

Note that layoutkind. Sequential, pack = 4 and public intptr reserved. If this statement is not followed, the call fails.
Sp_drvinfo_data can also be declared in the same way.

[Structlayout (layoutkind. Sequential, pack =   4 , Charset = Charset. Auto)]
Public   Struct Sp_drvinfo_data
{
Public   Int Cbsize;
Public   Int Drivertype;
Public Intptr reserved;
[Financialas (unmanagedtype. byvaltstr, sizeconst =   256 )]
Public   String Description;
[Financialas (unmanagedtype. byvaltstr, sizeconst =   256 )]
Public   String Mfgname;
[Financialas (unmanagedtype. byvaltstr, sizeconst =   256 )]
Public   String Providername;
Public Filetime driverdate;
Public   Ulong Driverversion;
}

You can convert the last version from dwordlong to x. x in the following way. Dwordlong is an 8-byte unsigned integer, and X in x. x. x is an unsigned integer from 0 to 65536, accounting for 2 bytes. Therefore, the 8-byte integer can be directly divided into four 2-byte integers, and the version number is used together. Assume that the version is 1407379348914176. Convert the version to the binary number:
101 00000000 00000001 00001010 00101000 00000000 00000000
-------------------------------------------------------------------
5 1 2600 0
Therefore, the expected version is 5.1.2600.0.

You can use the following example function to obtain the version information.

// Version = 1407379348914176. The converted version is 5.1.2600.0.
Private   String Getversionfromlong ( Ulong Version)
{
Ulong Basenumber =   0 xFFFF ;
Stringbuilder sb =   New Stringbuilder ();
Ulong Temp =   0l ;

For ( Int Offset =   48 ; Offset > =   0 ; Offset -=   16 )
{
Temp = (Version > Offset) & Basenumber;
SB. append (temp. tostring () +   " . " );
}

Return SB. tostring ();
}

By calling APIs, the speed is greatly improved, and a query can be completed within one second. It is also suitable for Win2000, Win XP, win2003, and Vista.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.