News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Size of Bytes in a Buffer ?

Started by ic2, November 27, 2006, 12:16:49 AM

Previous topic - Next topic

ic2

This is not easy.  I wrote and used more than a ton of code trying to do this  for quite a few days. I never founded a solution on my own, here or anywhere else while doing a search.  I type WriteFile and get 1000 threads, I read 499 of them that has no relation whatsoever.  I still picked up some great things but ....  I'm sure I did this same thing a few years ago, and I think I figured something out but now I FORGOT what I did... *if i did*.  It may have something to do with strlen.  I really have forgotten.

Here are the facts:

I successfully read a a small portion of a large file into EDIT_BUFFER_1 which possibly could be only 12 bytes out of 4500 bytes.  THIS WILL VARY. It read to the first 0h of that file so I have no way of knowing what the result will be in  EDIT_BUFFER_1 and this buffer is 5000 dup

Anyway, whatever size is in EDIT_BUFFER_1 I than to write it to a newly created file on disk.

LENGTHOF offset EDIT_BUFFER_1 will still write 5000 bytes to the new file. 

I don't know how to GetFileSize of a buffer.  While trying, it did not work or it gave me the whole size of the buffer.

What can I do to make this thing work the way I need it to ???

I hope explained completely of what I am trying to do.  If someone need the whole non-working file just let me know.

Edit_1          equ 101

EDIT_BUFFER_1   db 5000 dup (?)

hwndEdit_1            DWORD ?

Etc...


.code

PUSH 0
PUSH 0
PUSH CREATE_ALWAYS
PUSH 0
PUSH 0
PUSH GENERIC_WRITE
PUSH offset szFileName
CALL CreateFileA
mov hFile, eax

PUSH FILE_END
PUSH 0
PUSH 0
PUSH [hFile]
CALL SetFilePointer

PUSH 0
PUSH offset Num
PUSH LENGTHOF offset EDIT_BUFFER_1
PUSH offset EDIT_BUFFER_1
PUSH [hFile]
CALL WriteFile

PUSH hFile
CALL CloseHandle


PBrennick

ic2,
The problem is you are looking in the wrong location for help. Read the API for ReadFile, you will see that the number of bytes read is returned after the readin one of the parameters, if all goes well. This, then can be copied to the number of bytes to write parameter and the job is done.

If you cannot figure out how to do this, just email me the project or attach it here. Emailing it to me allows you to maintain your privacy if that is necessary.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

ic2

Pbrennick, I NEVER even thought about that for a moment.  Yes I read 12 so I write 12.  I mean I wasted 5 days which would have been even more if you would never had responded. Now I got the idea of how to automate it.

I have one more question.  Now that i know what to do and plan to  generate a list of all string size to re-use.  I believe PUSH nNumberOfBytesTo Read or Write is known as an immediate where there is no easy way to replace that like you can do with regular db strings.  Is this true?  I'm going to try it right now like calling a Proc with Dword values.  Couldn't wait to say thanks.  I was about to give up before i crack up ... I felt it coming for real.

Thanks you

Push 039h
Call WriteFile

nNumberOfBytesToWrite = a:DWORD

what you think about this.  Happy to be going back to the drawing board now :)

PBrennick

ic2,
Pushing 39h or any immediate value is no problem. Remember that it will be pushed as a dword so you could pop it into EAX, for example. Two things to mention, here. One is for you to  be careful represennting the size of the buffer in hex, there is no reason to do this and can easily lead to mistakes. Secondly, be careful using call instead of invoke, remember that you must push in reverse order. Stack maintenance will be an issue, also.

nNumberOfBytesToWrite IS a dword, why did you ask about that?

By the way, never give up. Working with APIs can be frustrating for ALL of us, not just you.  :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

ic2

Pbrennick, I don't think i will have any problem with calculating everything up.  It's a shame that  WriteFile has that (nNumberOfBytesToWrite or read) parameter that we have to fill in.  Just write the darn text that is text out of a buffer with-out all the trailing zero.  I just don't know how to get rid of them.  It must take a higher level language to do things like that.

I'm  ending up doing stuff like below which I also need to know how to do anyway.  All pieces are  in a large project and it's a job trying to remember where everything is to cut out a decent example to post.  I'm at the stage now, where I am trying to read and write to my .ini file the hard way (no API).   It's a habit of mine, the less API the better.  I did not know I could just send a number when a  API in a proc instead of hex.  Never did that before.


Push 127
Call wWriteFile

.....

wWriteFile proc a:DWORD
PUSH 0
PUSH 0
PUSH CREATE_ALWAYS
PUSH 0
PUSH 0
PUSH GENERIC_WRITE
PUSH offset szFileName
CALL CreateFileA
mov hFile, eax

PUSH FILE_END
PUSH 0
PUSH 0
PUSH [hFile]
CALL SetFilePointer

PUSH 0
PUSH offset Num
PUSH a
PUSH offset EDIT_BUFFER_1
PUSH [hFile]
CALL WriteFile

PUSH hFile
CALL CloseHandle

ret

wWriteFile endp




QuoteBy the way, never give up. Working with APIs can be frustrating for ALL of us, not just you.
It's true the less, the better.