The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: travism on October 28, 2008, 11:39:32 PM

Title: Need help to read documents in the right direction..
Post by: travism on October 28, 2008, 11:39:32 PM
Im not really looking for a answer how to, I really want to learn how to write disk images to a floppy etc.. I want to write a program like rawwritewin but using assembler and ofcourse myself writing it, But im not sure if its as easy as opening a voume like \\.\C: and writing to it or sector copying or what... So basically what im asking is what docs and api's (if any) should I be studying. I hope you all dont think im stupid for asking this type of question. Thanks!  :bg
Title: Re: Need help to read documents in the right direction..
Post by: MichaelW on October 29, 2008, 12:28:26 AM
For a diskette I would just use the Interrupt 13h functions from a DOS app.
Title: Re: Need help to read documents in the right direction..
Post by: travism on October 29, 2008, 01:27:54 AM
I was thinking more along the lines of win32 based app.
Title: Re: Need help to read documents in the right direction..
Post by: Tedd on October 29, 2008, 12:46:49 PM
Don't worry, it actually is as simple as opening "\\.\A:" and then writing the image.
If you want to do anything more complex, like writing to specific sectors, then you need to calculate the offset into the disk image then move the file-pointer and write from there - it's still straightforward though,
Title: Re: Need help to read documents in the right direction..
Post by: travism on October 29, 2008, 11:34:56 PM
Ive always hated writing files in any language Ive coded in heres my code snippet that isnt working, Im sure someone can spot whats wrong :P Don't make fun of it please :\
hVolume is the drive so its "\\.\A:" and strFile is the path the the image file.

invoke CreateFile,addr hVolume,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
              .IF eax!=INVALID_HANDLE_VALUE
                  mov hDevice,eax
                 
                  invoke CreateFile,addr strFile,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
                  .IF eax!=INVALID_HANDLE_VALUE
                      mov fHandle,eax
                      ; --------------------------------------------------------------
                      ; Write our image file to the floppy
                      invoke GetFileSize,fHandle,NULL
                      invoke ReadFile,fHandle,addr iBytes,eax,addr wBytes,0
                      .IF eax!=0
                          invoke WriteFile,hDevice,addr iBytes,eax,addr tBytes,0
                      .ENDIF
                      ; --------------------------------------------------------------
                      ; clean up everything
                      invoke CloseHandle,hDevice
                      invoke CloseHandle,fHandle
                      invoke SetDlgItemText,hWnd,IDC_STC8,addr strDone
                  .ENDIF
              .ELSE
                  ; --------------------------------------------------------------
                  ; Let them know we are finished
                  invoke SetDlgItemText,hWnd,IDC_STC8,addr strNoVolume
              .ENDIF
              .ELSE
                  invoke SetDlgItemText,hWnd,IDC_STC8,addr strError
              .ENDIF


Title: Re: Need help to read documents in the right direction..
Post by: travism on October 30, 2008, 09:05:48 AM
any input? still can't figure this out :\ i dont get errors opening anything so im guessing its the read/write file part.
Title: Re: Need help to read documents in the right direction..
Post by: sinsi on October 30, 2008, 09:42:37 AM
You need to save the return value of GetFileSize somewhere, since EAX gets trashed after using an API - even using ADDR with INVOKE will trash EAX if the variable is local.

                      ; Write our image file to the floppy
                      invoke GetFileSize,fHandle,NULL
                      mov esi,eax
                      invoke ReadFile,fHandle,addr iBytes,esi,addr wBytes,0
                      .IF eax!=0
                          invoke WriteFile,hDevice,addr iBytes,esi,addr tBytes,0
                      .ENDIF

Title: Re: Need help to read documents in the right direction..
Post by: travism on October 30, 2008, 10:03:45 AM
Ah, well changed that, It still doesn't write anything though :\
Title: Re: Need help to read documents in the right direction..
Post by: sinsi on October 30, 2008, 10:16:09 AM
So which call returns the error? Run it through a debugger.
Title: Re: Need help to read documents in the right direction..
Post by: travism on October 30, 2008, 10:18:28 AM
Im not receiving an error, it finishes like it should it just doesnt write the file to the floppy..
Title: Re: Need help to read documents in the right direction..
Post by: Tedd on October 30, 2008, 09:17:47 PM
The first thing I see is that you're opening the volume with only FILE_SHARE_WRITE, you do need FILE_SHARE_READ as well (even if you're only writing, or only reading).
Then also save the filesize if you intend to use it more than once - though you wouldn't really need to, since you're only writing the same number of bytes read (which will be stored in wBytes.) However, you're assuming that a single read and a single write will do it all in one go - it may not; loop over it until filesize number of bytes have been transferred.
Note: the return value from ReadFile is NOT the number of bytes read.
How big is iBytes?
Title: Re: Need help to read documents in the right direction..
Post by: travism on October 31, 2008, 12:11:04 AM
ah, ok I got the program working finally... I had to change iBytes :) so everything works great now thanks for all the help!