News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Find, Open and alter a file

Started by Hyzau, November 20, 2012, 08:44:39 AM

Previous topic - Next topic

Hyzau

Hi,

First of all, I'm new on this forum and in MASM so sorry if I'm posting in the wrong sections.
Also, my mother-tongue isn't English so excuse me if I've made some errors.

I would like to create a small console executable that can find every file in the current repertory, open the .txt file, and then (for example) replace every letter 'a' with an 'e'.

I've looked a little into the forum and also in the masm32\example repertory but I couldn't found anything similar that didn't open a windows.
However, I've find mdir.asm (in examples\exampl02\dir\) which can be a good start to my program.
So my question is, do you have an example where you open a file, read it and then change it.
Because, I've seen the function GetOpenFileName and CreateFile but even after many tries I couldn't make it work.

Thanks for your help.

Best regards,



qWord

you may show us your code that fails?

; build as console program
include \masm32\include\masm32rt.inc
.code
main proc
LOCAL cbRead:DWORD
LOCAL cbWrite:DWORD
LOCAL dwFileSizeHigh:DWORD
LOCAL dwFileSizeLow:DWORD
LOCAL hFile:HANDLE

        .if rv(CreateFile,"test.txt",GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)
                mov hFile,eax
                mov dwFileSizeLow,rv(GetFileSize,hFile,ADDR dwFileSizeHigh)
               
                .if !dwFileSizeHigh && dwFileSizeLow <= 5*1024*1024 ; some limit: 5Meg
                        .if rv(GlobalAlloc,GPTR,dwFileSizeLow)
                               
                                mov ebx,eax
                                .if rv(ReadFile,hFile,ebx,dwFileSizeLow,ADDR cbRead,0)
                                        .if cbRead
                                                xor esi,esi
                                                .while esi < cbRead
                                                        .if CHAR ptr [ebx+esi] == 'a'
                                                                mov CHAR ptr [ebx+esi],'e'
                                                        .endif
                                                        inc esi
                                                .endw
                                                invoke SetFilePointer,hFile,0,0,FILE_BEGIN
                                                .if rv(WriteFile,hFile,ebx,cbRead,ADDR cbWrite,0)
                                                        mov eax,cbRead
                                                        .if !cbWrite
                                                                print "error: no bytes written",13,10
                                                        .elseif eax != cbWrite
                                                                print "error: not all bytes written",13,10
                                                        .else
                                                                print "sucess",13,10
                                                        .endif
                                                .else
                                                        print "error: can write file",13,10
                                                .endif
                                        .else
                                                print "error: empty file",13,10
                                        .endif
                                .else
                                        print "error: can read file",13,10
                                .endif
                               
                                invoke GlobalFree,ebx
                        .else
                                print "error: allocation faild",13,10
                        .endif
                .else
                        print "file size to large",13,10
                .endif
               
                invoke CloseHandle,hFile
        .else
                print "error: cant't open file",13,10
        .endif
       
        inkey
        exit
       
main endp
end main
MREAL macros - when you need floating point arithmetic while assembling!

Hyzau

Hi,

I'm actually at work right now ^^ so I can't post my code, sorry.
However, the one that you've posted seems quite nice, thanks a lot.

There's many things that I don't understand in your code and my main problem is that I can't manage to find any online (or offline) documentation for MASM.
In the MASM32 folder I only found some tutorial and code example but no documentation.
If you know a place where I can find a list of avaible functions with like a description of the function's parameters it would be great.

Thanks again for your help.

Best regards,

dedndave

seems like there are about a million of them - lol
(10,000 is closer - probably not that many)

for masm - the masm programmer's guide

http://www.box.net/shared/qkpzlg42hf

for the functions, i don't know if you can still get the MSDN library ISO or not
but, you can just use the MSDN library online
better in some ways, because it has user comments that can be valuable
for example...

new format
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx

old format
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

it is documented for C programmers
not hard to figure out what the asm looks like, though

qWord used the "rv" macro, which is part of the masm32 library
it is documented in \masm32\help\hlhelp.chm under "Code Calling Macros"
basically, it is similar to INVOKE, but offers direct access to the value returned in EAX (return value)

hutch--

Hyzau,

Use the link below, its the full Borland collection of Microsoft Winhelp files that have all of the older win32 functions. The main file you need in WIN32.HLP. Note that if you are using Vista or Win7 you will need to get the winhelp download from Microsoft.

http://www.masmforum.com/winhelp_files/

Hyzau

Quote from: dedndave on November 20, 2012, 10:13:27 PM
qWord used the "rv" macro, which is part of the masm32 library
it is documented in \masm32\help\hlhelp.chm under "Code Calling Macros"
basically, it is similar to INVOKE, but offers direct access to the value returned in EAX (return value)

Wow thanks !
I've commented qWord's code, I think i've understanded the main part. Correct me if you see any mistakes.



if rv(CreateFile,"test.txt",GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0) ; Open the file test.txt and store it in EAX
                mov hFile,eax  ; Move value of EAX into hFile
                mov dwFileSizeLow,rv(GetFileSize,hFile,ADDR dwFileSizeHigh)  ; invoke getFileSize, store return value in EAX and move it into dwFileSizeLow. Meanwhile a "high-order doubleword of the file size" is stored into dwFileSizeHigh
               
                .if !dwFileSizeHigh && dwFileSizeLow <= 5*1024*1024 ; some limit: 5Meg
                        .if rv(GlobalAlloc,GPTR,dwFileSizeLow)   ; invoke GlobalAlloc which allocate a "buffer" of fixed size and initializes memory contents to zero. The buffer size is dwFileSizeLow
                               
                                mov ebx,eax ; store the pointer of the buffer into ebx
                                .if rv(ReadFile,hFile,ebx,dwFileSizeLow,ADDR cbRead,0) ; invoke ReadFile, read the entire file at once and store the content into ebx
                                        .if cbRead ; if Number Of Bytes Read > 0
                                                xor esi,esi ; i assume that it's like esi = 0
                                                .while esi < cbRead
                                                        .if CHAR ptr [ebx+esi] == 'a'
                                                                mov CHAR ptr [ebx+esi],'e' ; modify every a of ebx into e
                                                        .endif
                                                        inc esi
                                                .endw
                                                invoke SetFilePointer,hFile,0,0,FILE_BEGIN ; the file Handle was at the End of file because of Read file
                                                .if rv(WriteFile,hFile,ebx,cbRead,ADDR cbWrite,0) ; write ebx (of size cbRead) into hFile
                                                        mov eax,cbRead
                                                        .if !cbWrite
                                                                print "error: no bytes written",13,10 ; Okay here why 13, 10 ?
                                                        .elseif eax != cbWrite
                                                                print "error: not all bytes written",13,10
                                                        .else
                                                                print "sucess",13,10
                                                        .endif
                                                .else
                                                        print "error: can write file",13,10
                                                .endif
                                        .else
                                                print "error: empty file",13,10
                                        .endif
                                .else
                                        print "error: can read file",13,10
                                .endif
                               
                                invoke GlobalFree,ebx
                        .else
                                print "error: allocation faild",13,10
                        .endif
                .else
                        print "file size to large",13,10
                .endif
               
                invoke CloseHandle,hFile
        .else
                print "error: cant't open file",13,10
        .endif
       
        inkey
        exit
       
main endp
end main


There's still one thing, I can't figure out the difference between dwFileSizeHigh and dwFileSizeLow

Once again, thank you very much for your help (and sorry if it was already posted somewhere on the forum)

Best regards,

CommonTater

Quote from: dedndave on November 20, 2012, 10:13:27 PM
for the functions, i don't know if you can still get the MSDN library ISO or not


Windows SDK...
http://www.microsoft.com/en-us/download/details.aspx?id=18950

(To save space, use custom install and only install the documentation for windows desktop)