


Std::cout << "serial number: " << serialNumber << "\n" Std::cout << "failed to retrieve serial number\n" Std::string serialNumber = getFirstHddSerialNumber() STORAGE_DEVICE_DESCRIPTOR* pDeviceDescriptor = reinterpret_cast(pOutBuffer.get()) Ĭonst DWORD dwSerialNumberOffset = pDeviceDescriptor->SerialNumberOffset Ĭonst char* serialNumber = reinterpret_cast(pOutBuffer.get() + dwSerialNumberOffset) read and return the serial number out of the output buffer POutBuffer.get(), dwOutBufferSize, &dwBytesReturned, NULL)) If (!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY), call DeviceIoControl with the allocated buffer Std::unique_ptr::type, void(*)(HANDLE)> hDevice an std::unique_ptr is used to perform cleanup automatically when returning (i.e. HANDLE h = CreateFileW(L"\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL) get a handle to the first physical drive returns the serial number of the first physical drive in a std::string or an empty std::string in case of failure Here is a solution using win32 api and std string in case you need your application to run on a OS with no CLR. Now we create a process, assign its ProcessStartInfo and start it ProcStartInfo.RedirectStandardOutput = true This means that it will be redirected to the Process.StandardOutput StreamReader. The following commands are needed to redirect the standard output. Incidentally, /c tells cmd that we want it to execute the command that follows, create the ProcessStartInfo using "cmd" as the program to be run, Public static string ExecuteCommandSync(object command) String currentDiskID = res.Substring(startI + search.Length).Trim() Int startI = res.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) Use "vol" shell command and parse serial from it's output, like this. If I format the hard disk, this will change. Label3.Text = "VolumeSerialNumber="+ disk.ToString()

ManagementObject disk = new ManagementObject("win32_viceid=\"" + drive + ":\"") In this one I am not getting any Unique Serial number. Response.Write(strProp + " = " + drv + "") ManagementObjectCollection drives = driveClass.GetInstances() PropertyDataCollection props = driveClass.Properties įoreach (PropertyData driveProperty in props) ManagementClass driveClass = new ManagementClass("Win32_DiskDrive") I tried with two code but I am not getting StringCollection propNames = new StringCollection()
