hey all... im trying to code a program where the user enters some information into some edit controls
and then they click a 'Generate' button, and the text from the edit controls is put into a template file, which then generates into the same folder as the program as source.asm or something

i dont know how to make it create the file, and i dont know how to get the buffer from the edit controls into the created file... could someone please gimme an example?

like in edit1 the user puts hello my name is something
then in the output file it will be similair to this
; hello my name is something

if you could help it would be appriciated thank you
Posted on 2003-07-31 15:15:13 by sTonER_DeVIOUS
It's actually easy...
LOCAL BytesWritten :DWORD

[color=blue]; open a file handle for write (ofwFilePath has the path and name of the file ie C:\myfile.txt)[/color]
mov hFile,eax
[color=blue]; Get the number of characters in the buffer[/color]
invoke strlen, ADDR buffer
[color=blue]; Write the buffer to the file, return the number of bytes actually written in BytesWritten[/color]
invoke WriteFile, hFile, ADDR buffer, eax, ADDR BytesWritten, NULL
[color=blue]; Close the file handle when all writes are done[/color]
invoke CloseHandle, hFile
Posted on 2003-07-31 15:35:11 by donkey
Posted on 2003-07-31 15:36:32 by JimmyClif
alright thank you.. but what if i wanted to create a NEW file, instead of an already existing one?
Posted on 2003-07-31 20:18:20 by sTonER_DeVIOUS
You substitude:


Open_Always : Create if not existing or open existing File


Create_New or Open_Existing


Did you read over Iczelion's Tut Number 12 ? ;)
Posted on 2003-07-31 20:41:17 by JimmyClif
EDIT: hmm i tried compiling the code given before and i got an undefined error for the strlen... any ideas?
yeah i read over iczelions tut 12.. and uhm.. im looking for more then just saving the text into a file.. i was to have like 8 edit boxes, and with information in those i was it to generate a file... the program im working on is to generate masm source code, so i just want the output to go into a file that is already pre-defined like eg.


include blah
includelib blah

TextfromEdit1 db " this i want to equal the text in edit 1 ",0
TextfromEdit2 db " this i want to equal the text in edit 2 ",0

etc... does that make sense? and thanks for that code im going to toy with it
Posted on 2003-07-31 21:01:31 by sTonER_DeVIOUS
strlen is from the masm32.lib - you got to include that one, See masm\help\masmlib.hlp.


Assuming you have all your EditBoxes created and handles are called hEdit1 to hEdit8. ofwFilePath points to an existing File

push ebx
invoke GlobalAlloc, GMEM_FIXED+GMEM_ZEROINIT, 10000
mov ebx, eax ; pointer to allocated memory
mov pMem, eax ; idem
invoke SendMessage, hEdit1, WM_GETTEXT, 1000,pMem
add ebx, eax ; number of chars copied
invoke SendMessage, hEdit2, WM_GETTEXT, 1000,ebx
add ebx, eax ; number of chars copied
invoke SendMessage, hEdit3, WM_GETTEXT, 1000,ebx
add ebx, eax ; number of chars copied
invoke CreateFile, ADDR ofwFilePath, GENERIC_WRITE, NULL,
mov hFile,eax

;attention this one:
invoke setfilepointer, hFile, 0, 0, FILE_BEGIN ; or FILE_END

invoke WriteFile, hFile, pMem, ebx, ADDR BytesWritten, NULL
invoke CloseHandle, hFile
pop ebx

I don't remember if WM_GETTEXT includes the 0 terminator in eax as char copied or not so maybe you have to dec it by one if it does...

and I borrowed some code from Donkey as I was to lazy to type ;)
Posted on 2003-07-31 21:25:03 by JimmyClif
alright i put this into my code

push ebx
invoke GlobalAlloc,GMEM_FIXED+GMEM_ZEROINIT,10000
mov ebx,eax
mov pMem,eax
invoke SendMessage,hEdit1,WM_GETTEXT,1000,pMem
add ebx, eax
invoke SendMessage,hEdit2,WM_GETTEXT,1000,ebx
add ebx, eax
invoke SendMessage,hEdit3,WM_GETTEXT,1000,ebx
add ebx, eax
mov hFile,eax

invoke SetFilePointer,hFile,0,0,FILE_BEGIN
invoke WriteFile,hFile,pMem,ebx,ADDR BytesWritten,NULL
invoke CloseHandle,hFile

and its not doing anything when i press the generate button... ive tried it with the file existing, and w/o it existing... and help? .. and i defined pMem like
pMem dd ?
and i dont know if thats what i was supposed to do
... any suggestions? thnx
Posted on 2003-07-31 21:41:41 by sTonER_DeVIOUS
Action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values.

CREATE_NEW or OPEN_EXISTING - These are mutually exclusive, this parameter takes only one flag, you cannot OR two flags

Also you forgot to pop ebx at the end of your sniplet.

MSDN Create file entry...


Win32.hlp file (8MB) you need this as a reference...

Posted on 2003-07-31 22:17:27 by donkey
lol oops i forgot to post the pop ebx at the end.. but i did have it in my code, and as soon as i limited it down to CREATE_NEW, the file was created, but nothing was in the file.....help? thnx
Posted on 2003-07-31 22:23:01 by sTonER_DeVIOUS
invoke WriteFile,hFile,pMem,ebx,ADDR BytesWritten,NULL

The value in ebx at this point will probably be somewhere near 200,000 or 300,000 bytes to write to the file from a buffer that is only 10,000 bytes. I doubt that it will work. There are alot of problems with the code, you must add a CRLF to the end of each string, you must make sure that the resulting buffer is null terminated, you must supply a real number of bytes to write.
Posted on 2003-07-31 22:32:44 by donkey
im not quite sure what you mean my all of that.. do you maybe have time to give me an example?? if not its ok, i guess... thanks tho if you do
Posted on 2003-07-31 22:37:52 by sTonER_DeVIOUS
I haven't tried this but it should be close
push ebx

invoke GlobalAlloc,GMEM_FIXED+GMEM_ZEROINIT,4096
mov ebx,eax
mov pMem,eax
invoke SendMessage,hEdit1,WM_GETTEXT,1024,pMem
add ebx, eax
mov [ebx],0Dh [color=blue]; >>> Add CR[/color]
mov [ebx+1],0Ah [color=blue]; >>> Add LF[/color]
add ebx,2
invoke SendMessage,hEdit2,WM_GETTEXT,1024,ebx
add ebx, eax
mov [ebx],0Dh [color=blue]; >>> Add CR[/color]
mov [ebx+1],0Ah [color=blue]; >>> Add LF[/color]
add ebx,2
invoke SendMessage,hEdit3,WM_GETTEXT,1024,ebx
add ebx, eax
mov [ebx],0 [color=blue]; >>> Ensure that the string is null terminated (this is redundant but a good idea)[/color]

[color=blue];Create the file[/color]

mov hFile,eax

invoke SetFilePointer,hFile,0,0,FILE_BEGIN
[color=blue]; Get the zero terminated string length[/color]
invoke lstrlen,pMem
[color=blue]; Write the buffer to the file[/color]
invoke WriteFile,hFile,pMem,eax,ADDR BytesWritten,NULL
[color=blue]; Close the file[/color]
invoke CloseHandle,hFile

pop ebx
Be sure to release your memory when you are done
Posted on 2003-07-31 23:12:49 by donkey
that gave me invalid instruction operands error... yeah
Posted on 2003-08-01 01:47:40 by sTonER_DeVIOUS
Replace all instances of:

mov ,0Dh ; >>> Add CR
mov ,0Ah ; >>> Add LF


mov ,BYTE PTR 0Dh ; >>> Add CR
mov ,BYTE PTR 0Ah ; >>> Add LF


mov ,0


mov ,BYTE PTR 0
Posted on 2003-08-01 01:53:01 by donkey

Don't you mean: mov byte ptr , 0Ah :tongue: Why not mov word ptr , 0a0dh ;)

Anyways... I know the code was not a for sale version... but the size of the Buffer was fine... (8 Edit Boxes, where each gets 1000 space... fill up a 10.000 Buffer) Only thing I forgot was the size in WriteFile which should have been:

sub ebx, pMem ; gets the size of the whole Buffer
invoke WriteFile,hFile,pMem,ebx,ADDR BytesWritten,NULL

And... I don't GlobalFree the Buffer either at the end...
Posted on 2003-08-01 06:50:33 by JimmyClif

Maybe he do not like the prefix 66h. :grin:

Actually, why not

mov eax, 0a0dh

But then again he is using ebx instead of edi. Bah! so much easier if edi was used.
Posted on 2003-08-01 07:16:14 by roticv
Here I attach a RadAsm DialogAsMain project I quickly hacked together.. Hopefully it will help you a bit.
Posted on 2003-08-01 08:27:24 by JimmyClif
WriteFile is looking for a pointer to a buffer. GlobalAlloc returns a handle, use GlobalLock to get a pointer to memory. Or use heap functions.

best regards,

Posted on 2003-08-01 08:47:24 by czDrillard
GlobalAlloc returns a pointer if called with GMEM_FIXED
Posted on 2003-08-01 10:00:49 by JimmyClif