none
Low Level Disk Access

    Question

  • At its nost basic every SATA and SAS hard disk have a few things in common. Each still use 512 bytes per sector. So all I need is a 512 byte disk transfer area, no problem. Any size is fine as new disks with larger sectors are surfacing to get around the MBR problem.

    So I would to be able to read any sector one at a time or several in a bunch. I also want to be able write to them.

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Thursday, May 26, 2011 11:14 PM

All replies

  • Hi Vegan,

     

    Please double check if this article helps: Reading and Writing to Raw Disk Sectors.

     

    Row disk accessing is quite a hardware related topic, VC++ general forum may not help you lot. I think the Windows WDK Forum is much better for discussing such low level developing topic.

     

    If you need help to move the thread to that forum, please let me know.

     

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 27, 2011 6:20 AM
    Moderator
  • Hi Vegan,

    Would you mind letting me know the result of the suggestions? If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 01, 2011 4:00 AM
    Moderator
  • Nothing concrete from that forum as of yet.

    Some research has found a few references.

     

    One tool I wanted to implement is a disk wipe that can erase everything.

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Wednesday, June 01, 2011 6:02 PM
  • For what it is worth, large SATA drives (3gb) are starting to use 4 kb sectors now.  So I would make sure your software actually figures out the correct sector size.

    If you are just looking at increasing throughput to file access, but still want a file system under your feet, consider the FILE_FLAG_NO_BUFFERING flag for CreateFile.

    If the disk in question is not currently mounted (i.e. no drive letter, not mounted in an NTFS folder etc) you can directly open the drive using the device name for CreateFile.  Getting the device name is something I have forgotten how to do. (I did do it once when I wrote my own FAT driver and wanted to test it out by raw disk reading)

    Wednesday, June 01, 2011 8:07 PM
  • I am aware of varying sector sizes from old floppy disks.

     

    Probably will use a DTA based on a sector class that can be resized to suit.

     

    That is why I wanted IDENT so that I can adapat.

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Wednesday, June 01, 2011 8:14 PM
  • Well, the normal way to get a handle to the raw disk is to use CreateFile with a path to the physical drive. So for example to open a handle to the first physical disk on your system, it would be.

    CreateFile("\\\\.\\PhysicalDrive0", /*Whatever access you want*/, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, /*whatever flags you want/, NULL);
    

    You can then use ReadFile or WriteFile to access the disk. You can read the Physical Disks and Volumes section of CreateFile to get more information. You can use DeviceIoControl to get more information on the disk too.

    An example program to open a raw disk handle and read from it is

    #include <tchar.h>
    #include <Windows.h>
    #include <stdio.h>
    
    int _tmain(int argc, TCHAR* argv[])
    {
    	HANDLE hdisk;
    	char *buf;
    	DWORD read;
    
    	hdisk = CreateFile(_T("\\\\.\\PhysicalDrive0"), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    	if(hdisk == INVALID_HANDLE_VALUE)
    	{
    		DWORD errval = GetLastError();
    		_tprintf(_T("CreateFile failed with error %d\n"), errval);
    		return -1;
    	}
    
    	buf = (char *)malloc(512);
    	if(!ReadFile(hdisk, buf, 512, &read, NULL))
    	{
    		DWORD errval = GetLastError();
    		_tprintf(_T("ReadFile failed with error %d\n"), errval);
    	}
    
    	free(buf);
    	CloseHandle(hdisk);
    
    	return 0;
    }
    

    just remember than on Windows Vista onwards it requires administrative privilages.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/
    Thursday, June 02, 2011 2:45 AM
  • I think that on some disks that are trashed badly might be better off with simple reads of sectors. This way then lost files can be recovered.

    What I want to do fist though identify the sector size

    then adapt the disk tranfer area to have adequate size

    then I want to call u my random generator and populate the DTA and then write to every sector on the disk

    So I need to know how many sectors are on the disk which is available from IDENT

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Thursday, June 02, 2011 3:07 AM
  • Thats why I mentioned DeviceIoControl. Using IOCTL_DISK_GET_DRIVE_GEOMETRY you can get the bytes per sector and it works on a handle to a physical disk too.

    As an example

    #include <tchar.h>
    #include <Windows.h>
    #include <stdio.h>
    
    int _tmain(int argc, TCHAR* argv[])
    {
    	HANDLE hdisk;
    	DWORD read;
    	DISK_GEOMETRY dg;
    
    	hdisk = CreateFile(_T("\\\\.\\PhysicalDrive0"), GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    	if(hdisk == INVALID_HANDLE_VALUE)
    	{
    		DWORD errval = GetLastError();
    		_tprintf(_T("CreateFile failed with error %d\n"), errval);
    		return -1;
    	}
    
    	if(!DeviceIoControl(hdisk, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &dg, sizeof(dg), &read, NULL))
    	{
    		DWORD errval = GetLastError();
    		_tprintf(_T("DeviceIoControl failed with error %d\n"), errval);
    		CloseHandle(hdisk);
    		return -1;
    	}
    
    	_tprintf(_T("Disk bytes per sector: %d\n"), dg.BytesPerSector);
    
    	CloseHandle(hdisk);
    
    	return 0;
    }

    This was outputting 512 for that disk on my system.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/

    Thursday, June 02, 2011 3:18 AM