Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   Read a bit on the hard drive (http://www.programmingforums.org/showthread.php?t=14814)

PhilBon Dec 26th, 2007 8:28 PM

Read a bit on the hard drive
 
What language would be the best language to read the hard drive bit by bit and then display it. for example I'd have a layout of the whole hard drive and then i could select a sector and it'd display the bits and each value. With the possibility of being able to write specifically to the bit. I'm used to VB.Net so if there is any chance it could be done in that, that'd be cool, if not I'm guessing C and C++ would be the way to go.

lectricpharaoh Dec 26th, 2007 11:34 PM

Re: Read a bit on the hard drive
 
The system usually accesses hard drive data in units of sectors, with sectors often being 512 bytes. There is also a logical unit, typically called a cluster, which is the smallest logical unit the OS deals with (it allocates space on disk as a number of clusters). You won't have bit-level addressability of disk contents; you won't even have this fine access to memory contents (for memory, a byte is the smallest addressable unit).

I'm not sure what your final goal is, but you can read in the contents of a file and display the data. How you display it is entirely up to you; you can display individual bits if that's what you want to do. Writing to an individual bit will entail reading in the cluster (into a buffer), locating the byte and bit within the byte, setting or clearing the bit with a bitwise operation, and then writing the buffer back to disk.

However, there is a catch. As far as most application software is concerned, the disk exists in terms of files, and not absolute sectors. Some software, notably disk tools such as defragmenters, imagers, and partitioning tools, will operate at a level below that of the file system in question. I'm not sure how this is done, but in order to accomplish it from application code, you'd doubtless need special permissions from the OS to obtain this level of access.

To answer your question, though, assuming you can call whatever API you need from the language in question, any language would be fine. C and C++ both make it easy to call native Win32 API methods, but you can also do it from the .NET family of languages through the use of p/invoke.

Alias Dec 27th, 2007 1:30 PM

Re: Read a bit on the hard drive
 
Here's an example from my blog using C# and as lectricpharaoh brings up, it uses Win32 API calls to function since the .NET framework classes available do not support such behaviour, seemingly by design. Anyway here it is, almost guaranteed to break your system with even slight changes...

:

....
        public static unsafe byte[] ReadSector(char drive, long startingsector, int numberofsectors)
        {
            IntPtr handle;
            byte[] buffer;
            int bytesread = 0;
            //create a handle to the device
            handle = CreateFile(
                "\\\\.\\" + drive + ":", FileAccess.Read, FileShare.ReadWrite, 0, FileMode.Open, 0, 0);
            if (handle == IntPtr.Zero)
                return null;
            //setting the pointer to point to the start of the sector to read
            SetFilePointer(handle, (startingsector * 512), 0, 0);
            //allocate buffer length in a managed context
            buffer = new byte[512 * numberofsectors];
            fixed (byte* p = buffer)
            {
                //attempt to read file contents into buffer
                if (!ReadFile(handle, p, 512 * numberofsectors, &bytesread, 0))
                {
                    CloseHandle(handle);
                    return null;   
              }
            }   
            CloseHandle(handle);
            return buffer;
        }
 ....


PhilBon Dec 27th, 2007 7:55 PM

Re: Read a bit on the hard drive
 
I want to be able to have this:
Input:
StartReadSector
EndReadSector
For each sector between StartReadSector and EndReadSector
Display bit values of the sector, in a binary form. I want to be able to extract an image of the hard drive into the binary form. so 101010111010101010010101010101, etc. But on the Fly and based on where I start and stop.
next

Kuryn Dec 28th, 2007 1:23 AM

Re: Read a bit on the hard drive
 
I know this isn't what you preferred. But I have one for floppy, maybe you can figure it out for hdd.

http://www.codeguru.com/cpp/cpp/cpp_...le.php/c13809/

null_ptr0 Dec 28th, 2007 10:31 AM

Re: Read a bit on the hard drive
 
PhilBon, just read bytes, then convert them to binary strings.


All times are GMT -5. The time now is 3:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC