Greetings everyone!
I wonder who could help me to understand better the following statement. Probably, I am doing something wrong or with the wrong tools.
STATEMENT
Direct access by a sector level to a hard disk is needed under OS of Win2000 or compatible. Program is designed to be compiled under UNIX platforms as well, thus I have a low level functionality dealing with HDD routines enclosed separately with base types redefined.
REVISION
I use function
CreateFileW to obtain a
HANDLE to a particular physical device. There is an example of source-code opening a device:
Quote:
DEVICE OpenDevice(wchar_t* devName, int mode, bool fUseException)
{
// detect operation mode needed
DWORD dwOpMode = ( mode&mdRead ? GENERIC_READ : 0 ) |
( mode&mdWrite ? GENERIC_WRITE : 0 );
// opening a device. DEVICE type is a redefined HANDLE
DEVICE dev = CreateFileW(devName, dwOpMode, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
// check if there are errors opening a device
if ( INVALID_HANDLE_VALUE == dev )
{
// errors detected. To know an extended error code
// use GetLastError
//
if ( fUseException )
// generating exception
//
throw afx_except(EX_DEV_OPEN);
// reflect there is an error
dev = NULL;
}
// here - a device handle
return dev;
}// OpenDevice
|
Well, lads from Micro$oft say: due the unbuffered way of working with devices, use those restriction, described in their technical articles for unbuffered access to files. There, my buffer preparation routine:
Quote:
unsigned char* alloc_buffer(long cb)
{
// allocate some paging memory. This memory to be sure is aligned to page boundaries
//
unsigned char* pBuff = (unsigned char*)VirtualAlloc(NULL, cb, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if ( pBuff )
// guaranty this memory is currently present in our RAM
VirtualLock(pBuff, cb);
return pBuff;
}// alloc_buffer
|
And finally, I assume I could read and write sectors to my buffer prepared in its special way, using
ReadFile and
WriteFile functions providing them with my device's handle.
REMARKS
Function
OpenDevice works fine, returns a device's handle;
alloc_buffer works as well but when I try to
ReadFile (
WriteFile I have not tried yet), system reads nothing and
GetLastError would return 0x00000005 - "Access denied". My program runes with administrative rights, thus I guess, I need to gain an exclusive access to a drive, i.e. \\.\PHYSICALDRIVE0. But how should I do so? Do I need to lock an entire drive and how could that be done?