Thread: Reading disk sectors directly in C/C++.

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Reading disk sectors directly in C/C++.

    How do i directly read the disk sectors on my HDD in Turbo C/C++?I am working in win98 environment.'absread' or 'abswrite' gives 'Unknown error"
    When i use F8 and get the machine into DOS,the error changes to "invalid function number".All this works fine when I change the drive to A: drive but not when i change it to C or D or E i.e HD drives.
    Is there any function in VC++ which can give direct access to the HDD ?
    Thanks in advance.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Since you're on Win98 with Turbo C, the following routine worked for me:
    Code:
    int GetSector()
    {
        int s, c, h, d;
        union
        {   char  ch[0x10];
            short sh[0x08];
            long  lg[0x04];
        } extbuf;
    
        if (Drive < 0) Drive= 0x80;
        if (Sector< 0)
        {
            printf("No Sector specified\n");
            return (-1);
        }
    
        reg.r_ax = 0x4200;
        reg.r_dx = Drive;
        reg.r_si = FP_OFF(extbuf.ch);
        reg.r_ds = FP_SEG(extbuf.ch);
    
        extbuf.ch[0] = 0x10;
        extbuf.ch[1] = 0;
        extbuf.sh[1] = 1;
        extbuf.sh[2] = FP_OFF(getbuf);
        extbuf.sh[3] = FP_SEG(getbuf);
        extbuf.lg[2] = Sector;
        extbuf.lg[3] = 0;
    
        intr(0x13, &reg);
    
        if (reg.r_flags & CF)
        {
            printf("Error %02X \n", reg.r_ax);
            return (1);
        }
        return (NO_ERROR);
    }
    Drive C: is 0x80, as defaulted above
    Variables Drive and Sector are global.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Question Using inbuilt function ?

    Thanks for the code.I will try to use it.But,why can't i use any inbuilt functions,after all roads leads to BIOS services !!!

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Using inbuilt function ?

    Originally posted by someonenearHim
    Thanks for the code.I will try to use it.But,why can't i use any inbuilt functions,after all roads leads to BIOS services !!!
    That is in-built. That's how Turbo accesses the interrupts and disk.

    What are you looking for? A function they wrote called
    ReadAbsoluteDiskSector(buffer,drive,sector)?

    If they had one, I might have used it. They didn't, so I had to supply it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You can open a handle to a disk with the API CreateFile and read/write sectors from that....but from memory I dont think method this is available with Windows 98 (you need a recent NT based system)

    Also if you are looking for an API route to this, dump the Turbo C complier...it's DOS based and pretty much junkware. Go for a free, updated, windows ready compiler like MingW or Borland's Free Comandline Tools. Unless you want to spend some money on a compiler like VC++ or Codewarrior

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Fordy
    You can open a handle to a disk with the API CreateFile and read/write sectors from that....but from memory I dont think method this is available with Windows 98 (you need a recent NT based system)
    Doesn't CreateFile ... um .. Create a File? how would you "directly read the disk sectors on my HDD" by creating a file, as someonenearHim asked? And if it's not available in 98....

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object:

    · files
    · pipes
    · mailslots
    · communications resources
    · disk devices (Windows NT only)
    · consoles
    · directories (open only)
    It works on 95 and up.

    Edit: The function works on 95 and up, not sure if using that to directly access the harddrive will work on 95 or 98 though.
    Last edited by Thantos; 01-14-2004 at 06:37 PM.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Exclamation Where did 'file' come from ?

    Hello,
    I do not want to create or read any file directly from the HDD.I want to read the root directory of my HDD modify some values in it.I do not wish to create any handle to any file or directory.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    As I posted....
    I wrote it to do exactly what you want to do.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Thanks.

    Thanks WaltP.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Talking What about XP ?

    Is is possible to directly read disk sectors through TC when i am running WinXP ?

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As I said, CreateFile can open a handle to a disk on an NT system, then you can use this handle to read raw disk sectors....no files or directories involved - read the documentation at MSDN

    >>Is is possible to directly read disk sectors through TC when i am running WinXP ?

    Maybe with the old DOS interupt method mentioned above, but it's old and XP doesnt have to run that code if it doesnt want to. Again, the problem is your crappy compiler (TC) - it's a museum piece - dump it and get something half decent

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Unhappy Couldnt get the code to work

    Hello WaltP,
    You helpfully gave me the code but i could not get it to work.The compiler says that 'reg',setbuf' is not defined.And the character placed inside the intr() function is illegal(0xae). Can you(please ofcourse) give me the main() function which could use the GetSector() function ?


    Again, the problem is your crappy compiler (TC) - it's a museum piece - dump it and get something half decent
    Can anybody suggest a better compiler for the DOS environment ?
    Last edited by someonenearHim; 01-15-2004 at 11:55 AM.

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Sorry about that. I cut it from a longer file and forgot the header info. Add:
    Code:
    #include <dos.h>
    #define SECTSIZE  1024
    struct REGPACK reg;
    #define CF 1  /* carry flag */
    
    unsigned char newbuf[SECTSIZE];
    unsigned char getbuf[SECTSIZE];
    
    long Sector = -1L;
    long Drive  = -1L;
    And if you want to run it in 98, don't dump the crappy compiler. It works great for 95,98,ME. You need the crap that was removed by the latest and greatest compilers that Fordy would like you to move to.

    Is is possible to directly read disk sectors through TC when i am running WinXP ?
    No, this form won't work. The interrupts have been removed and a completely new I/O subsystem has been installed.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by WaltP
    You need the crap that was removed by the latest and greatest compilers that Fordy would like you to move to.
    If he wants a decent free dos compiler then try http://www.delorie.com/djgpp/

    That's leaps and bounds above TurboC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Directly to Hard Disk Sectors
    By ali_aslam in forum Linux Programming
    Replies: 2
    Last Post: 07-27-2007, 02:29 AM
  2. Help on reading a disk from my program
    By civix in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2002, 07:52 PM
  3. reading data from disk into array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 03:19 PM
  4. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM
  5. Writing directly to sectors on a floppy.
    By someonenearHim in forum C Programming
    Replies: 0
    Last Post: 12-12-2001, 02:44 AM