Win32 Diskdrive Serial Number

C++ WinAPI - get first physical drive serial number
Try this command. Vennilave vennilave song lyrics in tamil. Vol C: this will get the volume serial number given to it by windows. Wmic diskdrive get serialnumber. Ufed 4 pc software download free. This gets the.
main.cpp
#include<windows.h> |
#include<memory> |
#include<string> |
//returns the serial number of the first physical drive in a std::string or an empty std::string in case of failure |
//based on http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/ |
std::string getFirstHddSerialNumber() { |
//get a handle to the first physical drive |
HANDLE h = CreateFileW(L'.PhysicalDrive0', 0, FILE_SHARE_READ FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
if(h INVALID_HANDLE_VALUE) return {}; |
//an std::unique_ptr is used to perform cleanup automatically when returning (i.e. to avoid code duplication) |
std::unique_ptr<std::remove_pointer<HANDLE>::type, void(*)(HANDLE)> hDevice{h, [](HANDLE handle){CloseHandle(handle);}}; |
//initialize a STORAGE_PROPERTY_QUERY data structure (to be used as input to DeviceIoControl) |
STORAGE_PROPERTY_QUERY storagePropertyQuery{}; |
storagePropertyQuery.PropertyId= StorageDeviceProperty; |
storagePropertyQuery.QueryType= PropertyStandardQuery; |
//initialize a STORAGE_DESCRIPTOR_HEADER data structure (to be used as output from DeviceIoControl) |
STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader{}; |
//the next call to DeviceIoControl retrieves necessary size (in order to allocate a suitable buffer) |
//call DeviceIoControl and return an empty std::string on failure |
DWORD dwBytesReturned= 0; |
if(!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY), |
&storageDescriptorHeader, sizeof(STORAGE_DESCRIPTOR_HEADER), &dwBytesReturned, NULL)) |
return {}; |
//allocate a suitable buffer |
const DWORD dwOutBufferSize= storageDescriptorHeader.Size; |
std::unique_ptr<BYTE[]> pOutBuffer{new BYTE[dwOutBufferSize]{}}; |
//call DeviceIoControl with the allocated buffer |
if(!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY), |
pOutBuffer.get(), dwOutBufferSize, &dwBytesReturned, NULL)) |
return {}; |
//read and return the serial number out of the output buffer |
STORAGE_DEVICE_DESCRIPTOR* pDeviceDescriptor= reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(pOutBuffer.get()); |
const DWORD dwSerialNumberOffset= pDeviceDescriptor->SerialNumberOffset; |
if(dwSerialNumberOffset0) return {}; |
constchar* serialNumber= reinterpret_cast<constchar*>(pOutBuffer.get() + dwSerialNumberOffset); |
return serialNumber; |
} |
#include<iostream> |
intmain() { |
std::string serialNumber = getFirstHddSerialNumber(); |
if(serialNumber.empty()) |
std::cout << 'failed to retrieve serial numbern'; |
else |
std::cout << 'serial number: ' << serialNumber << 'n'; |
return0; |
} |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
I found myself looking for a way to retrieve the serial number of the computer's hard drive as a way of preventing unauthorized copies of an Access app. I have done it before but couldn't remember how. To my surprise, I found several ways to accomplish this task. I decided to post a couple of those techniques here to help anyone else who may find himself or herself looking to do the same thing in the future.Before we start, the first thing we need to realize is hard disk drives have more than one serial numbers. One of the serial numbers we can retrieve is the one assigned by the hard disk manufacturer. This serial number should stay consistent throughout the life of the equipment. The other serial number available to us is the logical serial number assigned by the operating system when a disk is formatted. The value for the logical serial number may change if the disk is reformatted. Each technique presented below depends on which serial number you are interested in retrieving.
Physical Disk Drive Serial Numbers
The following function uses Windows Management Instrumentation (WMI) to create a connection to the local computer. The 'WinMgmts' moniker is used to create a WMI object. Once a WMI object is instantiated, we can use the InstancesOf method to query the machine for system information.
Logicl Disk Drive Serial Numbers
The above function used the 'Win32_PhysicalMedia' collection to reference all the physical drives connected to the computer. We can now modify the above function using 'Win32_LogicalDisk' to get a collection of all logical or mapped drives connected to the computer for the current user.
However, there is a more straightforward way to get the serial number of a specific logical disk drive. The following function uses the File System Object.
There is an important difference between the two functions for logical disk drive presented above. Using WMI returns the serial number as HEX; whereas, using FSO returns the serial number as a Long Integer. However, you can use the Hex() function to either convert the return value from the LDSerialFSO() function to HEX or modify the LDSerialFSO() function to return the serial number as HEX.
I hope you find this post helpful. As usual, please feel free to submit your comments to let me know how I can improve these functions. Thank you!