Deviceiocontrol Hard Drive Serial Number

Torry s Delphi Pages Description With Callback File System you can create your own file system within your application and expose this file system to Windows as a.

Thread: WinAPI: How to Get Hard Disk Serial Number?

IsSubst: Neil Rubenking s IsSubst in the AllFuncs.pas unit of his ColorClue utility. Is a Subst drive. Managing Disks: Delphi in a Nutshell, p. 500.

June 14th, 2014,  AM

1

Q: How to programmatically get the serial number assigned to a hard disk or another type of physical drive by the manufacturer.

A: One solution is to call DeviceIoControl function with IOCTL_STORAGE_QUERY_PROPERTY control code.

Example

Code:

include

DWORD GetPhysicalDriveSerialNumber UINT nDriveNumber, CString strSerialNumber

DWORD dwResult NO_ERROR;

strSerialNumber.Empty ;

// Format physical drive path may be . PhysicalDrive0, . PhysicalDrive1 and so on.

CString strDrivePath;

strDrivePath.Format _T. PhysicalDrive u, nDriveNumber ;

// call CreateFile to get a handle to physical drive

HANDLE hDevice ::CreateFile strDrivePath, 0, FILE_SHARE_READ FILE_SHARE_WRITE,

NULL, OPEN_EXISTING, 0, NULL ;

if INVALID_HANDLE_VALUE hDevice

return ::GetLastError ;

// set the input STORAGE_PROPERTY_QUERY data structure

STORAGE_PROPERTY_QUERY storagePropertyQuery;

ZeroMemory storagePropertyQuery, sizeof STORAGE_PROPERTY_QUERY ;

storagePropertyQuery.PropertyId StorageDeviceProperty;

storagePropertyQuery.QueryType PropertyStandardQuery;

// get the necessary output buffer size

STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader 0 ;

DWORD dwBytesReturned 0;

if. ::DeviceIoControl hDevice, IOCTL_STORAGE_QUERY_PROPERTY,

storagePropertyQuery, sizeof STORAGE_PROPERTY_QUERY,

storageDescriptorHeader, sizeof STORAGE_DESCRIPTOR_HEADER,

dwBytesReturned, NULL

dwResult ::GetLastError ;

::CloseHandle hDevice ;

return dwResult;

// allocate the necessary memory for the output buffer

const DWORD dwOutBufferSize storageDescriptorHeader.Size;

BYTE pOutBuffer new BYTE dwOutBufferSize ;

ZeroMemory pOutBuffer, dwOutBufferSize ;

// get the storage device descriptor

if . ::DeviceIoControl hDevice, IOCTL_STORAGE_QUERY_PROPERTY,

pOutBuffer, dwOutBufferSize,

delete pOutBuffer;

// Now, the output buffer points to a STORAGE_DEVICE_DESCRIPTOR structure

// followed by additional info like vendor ID, product ID, serial number, and so on.

STORAGE_DEVICE_DESCRIPTOR pDeviceDescriptor STORAGE_DEVICE_DESCRIPTOR pOutBuffer;

const DWORD dwSerialNumberOffset pDeviceDescriptor- SerialNumberOffset;

if dwSerialNumberOffset. 0

// finally, get the serial number

strSerialNumber CString pOutBuffer dwSerialNumberOffset ;

// perform cleanup and return

//

UINT nDriveNumber 0;

CString strSerialNumber;

DWORD dwResult GetPhysicalDriveSerialNumber nDriveNumber, strSerialNumber ;

CString strReport;

if NO_ERROR dwResult

strReport.Format _T Drive u serial number: s, nDriveNumber, strSerialNumber ;

else

strReport.Format _T GetPhysicalDriveSerialNumber failed. Error: u, dwResult ;

::MessageBox NULL, strReport, _T Test, MB_OK ;

Resources and related articles

Last edited by ovidiucucu; June 14th, 2014 at AM.

Dec 30, 2004  Q. Will there be any changes in Server Clusters in Win2003 Service Pack 1. A. Windows Server 2003 SP1 release will contain a substantial number of.

Sizeof VersionParams ; if . DeviceIoControl __int64 sectors 0; __int64 bytes 0; // copy the hard drive serial number to the.

Sample DiskId32 Output for a 180 GB Intel SSD and a 2 TB WD SATA hard drive on Windows 7 x64 Ultimate: C: diskid32 diskid32 To get all details use diskid32 /d.

Hi I want to retrieve HDD unique hardware serial number. I use some functions but in Windows Seven or Vista they don t work correctly because of admin right. Is it.

Introduction. Most flash-based USB disk devices have a unique serial number assigned by the manufacturer. However, some earlier v1.1-based USB devices may.

How can I read the hard disk serial number for IDE and SATA drives in VB.NET. I don t want the volume serial number. This info should be gathered both for XP and.