c++ - Problem Reading MBR with DeviceIoControl function -


I get an error when calling the DeviceIoControl function to read the MBR of a removable device. The error code is 5. It means denial of access! I'm using Windows XP SP2.

  #include "stdafx.h" #include & lt; Windows.h & gt; #include & lt; Winioctl.h & gt; # Include & lt; Stdio.h & gt; BOOL GetMBR (PARTITION_INFORMATION * pdg) {Handle HDTV; // Check the BOOL bResult to handle the drive; // result flag DWORD junk; // Remove results hDevice = CreateFile (TEXT ("\\\ .. .. \\ G:"), // drive to open 0, // drive FILE_SHARE_READ | // SHARE MODEL FILE_SHARE_WRITE, NULL, // default security attributes OPEN_EXISTING , // voluntary 0, // file attributes do not copy zero / file properties); If (hDevice == INVALID_HANDLE_VALUE) // can not open the drive {printf ("CreateFile () failed! \"); return false); } Bresult = DeviceIoControl (hDevice, // device to IOCTL_DISK_GET_PARTITION_INFO, // to perform zero, 0, // to return an input buffer PDG, size (* pdg), // output buffer and junk, // # bytes (LPOVERLAPPED) NULL // Synchronous I / O); CloseHandle (hDevice); Return (BRSL); } Int _tmain (int argc, _TCHAR * argv []) {PARTITION_INFORMATION pdg; // Disc drive geometry structure BOOL bResult; // normal result flag ULONGLONG disk size; // Size of the drive, bytes in bResult = GetMBR (& amp; pdg); If (bResult) {} other {printf ("GetDriveGeometry ()) failed. Error% ld. \ N", GetLastError ()); } By (); Return (int) bResult); }  

Open it with FILE_READ_ATTRIBUTES.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -