Reading sectors
-
You can fix that easily by doing it this way:
#define SECTOR_SIZE 512
DWORD dwLen = SECTOR_SIZE;
DWORD dwNum = 0;
char buffer[SECTOR_SIZE];
ReadFile(hVolume, buffer, dwLen, &dwNum, NULL);If you are using C++ then instead of
#define
you can use this:const int SECTOR_SIZE = 512
-
It will interest me, not this time, but in the real program. I modified the code like this:
LONG lValueLow = 0; LONG lValueHigh = 0; if (INVALID\_SET\_FILE\_POINTER == SetFilePointer(hVolume, lValueLow, &lValueHigh, FILE\_BEGIN) && NO\_ERROR != GetLastError()) { TRACE("Error: %d\\n", GetLastError()); return FALSE; } else { TRACE("%d\\t%d\\n", lValueLow, lValueHigh); }
Thank you Superman !
-
You do not need
lValueHigh
unless the distance to move is a 64 bit value: just specify NULL instead. Also I suggest you study the difference between a variable and a pointer to a variable, you seem somewhat confused about it. -
Good idea. I have tried in this way:
CByteArray arrByte; arrByte.SetSize(512); BOOL bRet = ReadFile(hVolume, arrByte.GetData(), dwLen, &dwNum, NULL);
and seem to go well.
-
I'll try to use CFile Class | Microsoft Docs[^] Can I replace SetFilePointer as well with something from MFC yard ?
[CFile Class | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/mfc/reference/cfile-class?view=vs-2019#seek)
-
I'll try to use CFile Class | Microsoft Docs[^] Can I replace SetFilePointer as well with something from MFC yard ?
-
CFile Class.Seek | Microsoft Docs[^]. You need to read through all the documentation for the class to see what is available.
-
Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:
file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL); CFile file(hVolume); CFileStatus status; file.GetStatus(status); // return FALSE
-
Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:
file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL); CFile file(hVolume); CFileStatus status; file.GetStatus(status); // return FALSE
When using CFile::Open you should pass the CFileException* parameter to get the failure cause if CFile::Open fails. See the example in [CFile::Open](https://docs.microsoft.com/en-us/cpp/mfc/reference/cfile-class?view=vs-2019#open)
-
Good idea. I have few troubles with accessing USB drive with CFile, but once I'll solve it, the code will be simple. Here is my trial, none on them has worked:
file.Open(_T("\\\\.\\F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
file.Open(_T("F:\\"), CFile::modeRead | CFile::osSequentialScan); // return FALSE
HANDLE hVolume = ::CreateFile(sVolume, GENERIC\_READ, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, NULL, OPEN\_EXISTING, FILE\_FLAG\_SEQUENTIAL\_SCAN, NULL); CFile file(hVolume); CFileStatus status; file.GetStatus(status); // return FALSE
-
It is just the same as
OpenFile
behind the scenes so nothing to do withCFile
. Your call probably fails because you are not including the share flags in your call to CFile::Open(), which are required when trying to access a device.