Complete driveinfoex source code to obtain the computer's hard drive serial number

Source: Internet
Author: User
Overview:

It is widely used to obtain the serial number of a computer's hard disk. I found a C ++ source code driveinfoex on the Internet (Click here to view it ). A very good DLL, the. NET project can be directly referenced, and there are examples in the source code.

However, this DLL cannot obtain the hard disk serial number under the non-Administrator permission of win7, So I improved this DLL to support win7 non-administrator.

Source code:

Https://github.com/Xiongpq/DriveInfoExFull

Note during compilation

Some methods in the source code have been directly supported by the system in vc90, so you do not need to define them again. Otherwise, the compilation will fail. Therefore, if the method is compiled on the vc90 and later than the vc90 platform, add a pre-processor definition for "vc90 ".

I made a judgment in the Code. If "vc90" is predefined, some methods will not be defined. This setting is not required for compiling on vc80 and earlier than vc80.

The driveinfoexfull/driveinfoex/bin directory contains the compiled DLL, which supports. NET Framework 2.0.

Code Analysis:

The original author's code can be used to obtain hard drive serial numbers without administrator permissions. I will not analyze the original code, just let me talk about the modified content.

Readphysicaldriveinntwithzerorights is commented out by the original author. I don't know why. This method is used to obtain the hard disk serial number without permission.

Use this method to add the hard disk number to m_serizalnovec if it meets the requirements. This is a vector <char *>

In the load method, if the number of hard disks found in the conventional method is 0, the hard disk information in m_serizalnovec is added to the result. This only includes the serial number of the hard disk, excluding the size and other information.

The following is the method:

Int diskinfo: readphysicaldriveinntwithzerorights (void) {int done = false; int drive = 0; For (drive = 0; drive <max_ide_drives; drive ++) {handle hphysicaldriveioctl = 0; // try to get a handle to physicaldrive ioctl, report failure // and exit if can't. tchar drivename [256]; swprintf (drivename, l "\\\\. \ physicaldrive % d ", drive); // Windows NT, Windows 2000, Windows XP-Admin rights not requi Red hphysicaldriveioctl = createfile (drivename, 0, file_1__read | file_1__write, null, open_existing, 0, null); If (hphysicaldriveioctl! = Invalid_handle_value) {storage_property_query query; DWORD cbbytesreturned = 0; char buffer [10000]; memset (void *) & query, 0, sizeof (query); query. propertyid = storagedeviceproperty; query. querytype = propertystandardquery; memset (buffer, 0, sizeof (buffer); If (deviceiocontrol (hphysicaldriveioctl, ioctl_storage_query_property, & query, sizeof (query), & buffer, sizeof (buffer ), & clobby Tesreturned, null) {storage_device_descriptor * descrip = (storage_device_descriptor *) & buffer; char * serialnumber = new char [1000]; strcpy (serialnumber, flipandcodebytes (& buffer [descrip-> serialnumberoffset]); int isalnumandspace = true; int isallspace = true; int length = strlen (serialnumber); For (INT I = 0; I <length; I ++) {If (! Isalnum (serialnumber [I]) & serialnumber [I]! = '') {Isalnumandspace = false; break;} If (isallspace & serialnumber [I]! = '') {Isallspace = false ;}// the hard disk number is a letter, number, and space, but not a pure space. If (isalnumandspace &&! Isallspace) {m_serizalnovec.push_back (serialnumber);} done = true;} closehandle (hphysicaldriveioctl);} return done ;}

 

Int load () {diskinfo & di = diskinfo: getdiskinfo (); uint CNT = di. loaddiskinfo (); For (uint I = 0; I <CNT; I ++) This-> Add (gcnew driveinfoex (I )); // if the hard disk found in the conventional method is 0, add the hard disk information in m_serizalnovec to the result. // Only the serial number of the hard disk, excluding the size and other information. If (CNT = 0) {uint zerorightcount = di. m_serizalnovec.size (); For (uint I = 0; I <zerorightcount; I ++) {This-> Add (gcnew driveinfoex (Di. m_serizalnovec [I]);} return this-> count ;};

Some other modifications will not be described in detail. For more information, see the source code.

I don't want to know. Download the DLL and use it directly. Haha ~

Complete driveinfoex source code to obtain the computer's hard drive serial number

Related Article

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.