11

I want to know whether it is possible in Delphi to read a CD as a raw Stream direct from the logical disk drive device "C:\".

I hope I could use a TFileStream if I have already a valid file handle.

Warren P
  • 65,725
  • 40
  • 181
  • 316
menjaraz
  • 7,551
  • 4
  • 41
  • 81
  • 4
    -1 Your question seems vague to me. Update your question to say how you want to treat the data on the CD. – Samuel Harmer Feb 06 '12 at 11:46
  • @Styne666: How could I treat the data prior to accessing it in one way or another? My strong conviction is that accessing it (the physical media as a whole, I mean) through a logical file is the way to go. Even using the term 'data' is confusing, this may lead people to think of the file system. – menjaraz Feb 06 '12 at 11:56
  • That is my point. Saying "read a CD as a file" is vague. Clarify the question so there is **no ambiguity**. – Samuel Harmer Feb 06 '12 at 12:15
  • 1
    @Styne666: Believe me! Your input is valuable to me and I appreciate too much an edit to the post. – menjaraz Feb 06 '12 at 12:28
  • Well the concept of a [file](http://en.wikipedia.org/wiki/Computer_file) requires a [file system](http://en.wikipedia.org/wiki/File_system). Your original question doesn't explain what **you** mean by "file". The reason most people access 'files' from a CD via the operating system's definition is to avoid having to write code to handle the CD's file system (usually [ISO 9660](http://en.wikipedia.org/wiki/ISO_9660)) – Samuel Harmer Feb 06 '12 at 13:09
  • I did a bit of English Grammar fixing. Hope it makes it better? – Warren P Feb 06 '12 at 15:11
  • @Warren P: Editing is encouraged here. Thank you. – menjaraz Feb 06 '12 at 17:24

1 Answers1

12

It is easiest to use THandleStream rather than TFileStream in my view. Like this:

procedure ReadFirstSector;
var
  Handle: THandle;
  Stream: THandleStream;
  Buffer: array [1..512] of Byte;
  b: Byte;
begin
  Handle := CreateFile('\\.\C:', GENERIC_READ,
    FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if Handle=INVALID_HANDLE_VALUE then
    RaiseLastOSError;
  try
    Stream := THandleStream.Create(Handle);
    try
      Stream.ReadBuffer(Buffer, SizeOf(Buffer));
      for b in Buffer do
        Writeln(AnsiChar(b));
    finally
      Stream.Free;
    end;
  finally
    CloseHandle(Handle);
  end;
end;

Beware that when using raw disk access you have to read exactly multiples of sectors. The sectors on the disk I tested with are 512 bytes in size. I expect that CD disk sectors could very well be a different size.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • So the pattern is '\\.\DRIVELETTER:', am I right? Please do me a extra favour: I am not a native speaker, can you give a hint to improve my post if it's confusing (It was under the linefire of downvoters as soon as posted)? – menjaraz Feb 06 '12 at 12:16
  • Physical volume naming described here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx#physical_disks_and_volumes I had no problems understanding your post (I think). Sometimes you'll pick up downvotes from people that don't understand what you mean because they are perhaps lacking in some knowledge. Not much you can do about that. Don't worry. I'll give you an upvote now! – David Heffernan Feb 06 '12 at 12:41
  • 2
    @David and sometimes people give down votes for incomplete questions when the answer is dependent on information not provided. – Samuel Harmer Feb 06 '12 at 13:11
  • 2
    Be aware that this will only reliably work for data CDs. Reading digital audio data from audio CDs is an entirely different beast. – afrazier Feb 06 '12 at 13:42
  • 5
    @Styne666: You help me a lot editing my post. Here on SO a good communication skill is also sought after and should pay in reputation (Upvote/Downvote). I remenber one day David himself edited my post after answering it because it was full of blunders. As a non native, learning english is an ongoing process for me and hope there is still room for improvement. – menjaraz Feb 06 '12 at 13:46
  • @Styne666 Yeah, that can happen too! – David Heffernan Feb 06 '12 at 13:52
  • Sir David, where do I put callback for progress bar for that file stream? ? – Bianca Mar 17 '18 at 13:13