the prototype for Writefile as follows:
WriteFile PROTO, ; write buffer to output file
fileHandle:DWORD, ; output handle
pBuffer:PTR BYTE, ; pointer to buffer
nBufsize:DWORD, ; size of buffer
pBytesWritten:PTR DWORD, ; number of bytes written
pOverlapped:PTR DWORD ; ptr to asynchronous info
the pBuffer:PTR BYTE clearly indicates that you can only write strings (eg. my_string byte "This is string type variable" using this prototype into a file output.
My problem is wat do you do if you want to write non-string variable like values from a struct like SYSTEMTIME
SYSTEMTIME STRUCT
wYear WORD ? ;year (4 digits)
wMonth WORD ? ;1-12 1=January
wDayOfWeek WORD ? ;0-6 0=Sunday
wDay WORD ? ;1-31
wHour WORD ? ;0-23
wMinute WORD ? ;0-59
wSecond WORD ? ;0-59
wMilliseconds WORD ? ;0-999
SYSTEMTIME ENDS
I presume that if using WriteFile , we cannot just point the address of a SYSTEMTIME struct.AM i wrong on this?
If it is possible to write these SYSTEMTIME values, how do i include symbols like "/",":" in the output files.
i want something like this in the a txt file:
Date: 17/8/2004
Time: 23:10:31:234 (hour,minutes,seconds,pro-seconds)
Any source code is greatly appreciated....I still thinking of converting each value into ASCII then putting each ASCII into a string.....but with the values are stored in hex value...is it possible....
Any solutions?
WriteFile PROTO, ; write buffer to output file
fileHandle:DWORD, ; output handle
pBuffer:PTR BYTE, ; pointer to buffer
nBufsize:DWORD, ; size of buffer
pBytesWritten:PTR DWORD, ; number of bytes written
pOverlapped:PTR DWORD ; ptr to asynchronous info
the pBuffer:PTR BYTE clearly indicates that you can only write strings (eg. my_string byte "This is string type variable" using this prototype into a file output.
My problem is wat do you do if you want to write non-string variable like values from a struct like SYSTEMTIME
SYSTEMTIME STRUCT
wYear WORD ? ;year (4 digits)
wMonth WORD ? ;1-12 1=January
wDayOfWeek WORD ? ;0-6 0=Sunday
wDay WORD ? ;1-31
wHour WORD ? ;0-23
wMinute WORD ? ;0-59
wSecond WORD ? ;0-59
wMilliseconds WORD ? ;0-999
SYSTEMTIME ENDS
I presume that if using WriteFile , we cannot just point the address of a SYSTEMTIME struct.AM i wrong on this?
If it is possible to write these SYSTEMTIME values, how do i include symbols like "/",":" in the output files.
i want something like this in the a txt file:
Date: 17/8/2004
Time: 23:10:31:234 (hour,minutes,seconds,pro-seconds)
Any source code is greatly appreciated....I still thinking of converting each value into ASCII then putting each ASCII into a string.....but with the values are stored in hex value...is it possible....
Any solutions?
Use wsprintf, it will convert numbers to text (hex or decimal), then use WriteFile to write the result of the wsprintf to your file.
Mirno
Mirno
Unfortunately, i dun have the use of that function because i am doing it with limited functions i am using irvine32.inc only(i am currently studying MASM615) :). Is there a source code doing similar stuff? Anyone plz help me out here?
Why irvine32.inc? Is that a requirement for your class or whatever? If you cannot use windows.inc from the MASM32 package, do the wsprintf prototype yourself:
The function is located in user32.dll, so you'll have to link against user32.lib.
wsprintfA PROTO C :DWORD,:VARARG
wsprintf equ <wsprintfA>
The function is located in user32.dll, so you'll have to link against user32.lib.
Okey how to use it?
wsprintfA PROTO C :DWORD,:VARARG how should i call/invoke this prototype? I am a still a newbie. i am only learned irvine32.lib which mostly all the function using kernel32.lib.
I think i will use user32.lib but i dunno how should i passed the parameter, and what is the output. I want to change all the decimal into strings.
or there is another where?
wsprintfA PROTO C :DWORD,:VARARG how should i call/invoke this prototype? I am a still a newbie. i am only learned irvine32.lib which mostly all the function using kernel32.lib.
I think i will use user32.lib but i dunno how should i passed the parameter, and what is the output. I want to change all the decimal into strings.
or there is another where?
.data
buffer db 512 dup(0) ; A buffer for general use, nice & big!
format db "Date: %d/%d/%d", 13, 10, 0
...
.code
invoke wsprintf, ADDR buffer, ADDR format, mySysTime.wDay, mySysTime.wMonth, mySysTime.wYear
; buffer now contains the properly formatted string
; Call write file or whatever here.
Mirno
Worth mentioning is that after the wsprintf call, EAX will hold the number of characters in the output string... so there's no need to call strlen or whatever.
unfortunately i got the following in my output:
Date: 18/589824/0 which should be 18/9/2004..only the day was correct
invoke GetLocalTime,ADDR mySysTime
invoke wsprintf, ADDR buffer, ADDR format,
mySysTime.wDay, mySysTime.wMonth, mySysTime.wYear
INVOKE WriteFile,
fileHandle,
ADDR buffer,
formatted_length,
ADDR bytesWritten,
0
formatted_length is value of eax after wsprintf as mentioned by f0dder.
I looked into the microsoft debugger the value for Systemtime is correct but for buffer it wrote ambiguous symbol error.
why mySysTime.wDay and not mySysTime.Day (without w)? is a required in the function?Even so removing the "w", still the program would give results
plz help me on this.
Date: 18/589824/0 which should be 18/9/2004..only the day was correct
invoke GetLocalTime,ADDR mySysTime
invoke wsprintf, ADDR buffer, ADDR format,
mySysTime.wDay, mySysTime.wMonth, mySysTime.wYear
INVOKE WriteFile,
fileHandle,
ADDR buffer,
formatted_length,
ADDR bytesWritten,
0
formatted_length is value of eax after wsprintf as mentioned by f0dder.
I looked into the microsoft debugger the value for Systemtime is correct but for buffer it wrote ambiguous symbol error.
why mySysTime.wDay and not mySysTime.Day (without w)? is a required in the function?Even so removing the "w", still the program would give results
plz help me on this.
You have to use:
invoke wsprintf, ADDR buffer, ADDR format,
dword ptr mySysTime.wDay, dword ptr mySysTime.wMonth, dword ptr mySysTime.wYear
...
push eax
mov ecx,esp
invoke WriteFile, fileHandle, ADDR buffer, formatted_length, ecx, 0
pop ecx
...
format db "Date: %hu/%hu/%hu",13,10,0
invoke wsprintf, ADDR buffer, ADDR format,
dword ptr mySysTime.wDay, dword ptr mySysTime.wMonth, dword ptr mySysTime.wYear
...
push eax
mov ecx,esp
invoke WriteFile, fileHandle, ADDR buffer, formatted_length, ecx, 0
pop ecx
...
format db "Date: %hu/%hu/%hu",13,10,0
the pBuffer:PTR BYTE clearly indicates that you can only write strings (eg. my_string byte "This is string type variable" using this prototype into a file output.
No. WriteFile is a binary write, and bytes are binary data, not restricted to readable text.
nBufsize is the exact number of bytes that will be written to the file, regardless of how the data is formatted, or whether it contains bytes with the value of zero.
pBytesWritten contains the address of a "return" variable where the actual number of bytes written is stored. It can be less than nBufsize if you fill up a small "disk" such as floppies or USB memory drives.
The bytes are written "as is", without conversion to a readable form. So if it contains nontext data, it will look terrible under Notepad or Wordpad.
gilazilla,
There is no reason why you cannout write binary data to a file and be able to read it back into the program again when required. All data types reduce down to a sequence of BYTES so if you want, just write the data sequentially to a file.
What you have to do is have a reliable method of both reading and writing binary data to get the right piece of data in the right place.
If the file needs to be human editable, you normally convert the data to text first and write it to a file line by line but you have the choicxe of doing ewither depending on what you require.
There is no reason why you cannout write binary data to a file and be able to read it back into the program again when required. All data types reduce down to a sequence of BYTES so if you want, just write the data sequentially to a file.
What you have to do is have a reliable method of both reading and writing binary data to get the right piece of data in the right place.
If the file needs to be human editable, you normally convert the data to text first and write it to a file line by line but you have the choicxe of doing ewither depending on what you require.
hutch, re-read:
i want something like this in the a txt file:
Date: 17/8/2004
Time: 23:10:31:234 (hour,minutes,seconds,pro-seconds)
i want something like this in the a txt file:
Date: 17/8/2004
Time: 23:10:31:234 (hour,minutes,seconds,pro-seconds)
Originally posted by Tenkey:
The bytes are written "as is", without conversion to a readable form. So if it contains nontext data, it will look terrible under Notepad or Wordpad.
The bytes are written "as is", without conversion to a readable form. So if it contains nontext data, it will look terrible under Notepad or Wordpad.
Yes.Tenkey you are right about writing bytes.I want to write readable text into the file not binary or hex which would give funny symbols.
Originally posted by hutch--:
If the file needs to be human editable, you normally convert the data to text first and write it to a file line by line but you have the choicxe of doing ewither depending on what you require.
If the file needs to be human editable, you normally convert the data to text first and write it to a file line by line but you have the choicxe of doing ewither depending on what you require.
I am trying to convert the data to text first and writing it line by line using SetFilePointer and Writefile.Hutch what do you mean by choice of doing what you require ,Is that without using wsprintf?
Sephiroth3 i tried the codes still cannot.
the output is Date: 655378/393225/591828 for 18/9/2004
gilazilla,
I said that you can use either method depending oin what you want to do. If you are using a late version of MASM32 and have grabbed the last service pack, it has procedures for reading text on a line by line basis as well as writing it. The conversion both to and from text is easily done with library functions.
There is a simple example of reading and writing an INI file in the latest service pack.
f0dder,
hutch, re-read:
No need, I read it correctly the first time but passed the advice that it does not "have" to be done that way. If you are writing large mounts of data to file to be re-read later, it saves all of the conversions both ways by saving it in binary rather than text.
Regards,
hutch at movsd dot com
I said that you can use either method depending oin what you want to do. If you are using a late version of MASM32 and have grabbed the last service pack, it has procedures for reading text on a line by line basis as well as writing it. The conversion both to and from text is easily done with library functions.
There is a simple example of reading and writing an INI file in the latest service pack.
f0dder,
hutch, re-read:
No need, I read it correctly the first time but passed the advice that it does not "have" to be done that way. If you are writing large mounts of data to file to be re-read later, it saves all of the conversions both ways by saving it in binary rather than text.
Regards,
hutch at movsd dot com
Hmm. The wsprintf function doesn't seem to work right. By a lucky coincidence, you'll never discover this mistake when writing C programs. I guess you have to do this:
movzx ecx,mySysTime.wYear
push ecx
mov cx,mySysTime.wMonth
push ecx
mov cx,mySysTime.wDay
push ecx
push addr format
push addr buffer
call wsprintf
add esp,20
...
push eax
mov ecx,esp
invoke WriteFile, fileHandle, ADDR buffer, formatted_length, ecx, 0
pop ecx
...
format db "Date: %d/%d/%d",13,10,0
movzx ecx,mySysTime.wYear
push ecx
mov cx,mySysTime.wMonth
push ecx
mov cx,mySysTime.wDay
push ecx
push addr format
push addr buffer
call wsprintf
add esp,20
...
push eax
mov ecx,esp
invoke WriteFile, fileHandle, ADDR buffer, formatted_length, ecx, 0
pop ecx
...
format db "Date: %d/%d/%d",13,10,0
Sephiroth3 i tried the codes still cannot.
the output is Date: 655378/393225/591828 for 18/9/2004
try thisthe output is Date: 655378/393225/591828 for 18/9/2004
;================================================
.data
buffer db 512 dup(0)
mySysTime SYSTEMTIME <>
format db "Date: %i/%i/%i", 13,10,0
sz7788 db "Fuck 16 to 32!", 0
.code
;================================================
WinMain proc uses ebx ecx
invoke GetLocalTime, addr mySysTime
xor eax, eax
xor ebx, ebx
xor ecx, ecx
mov ax, mySysTime.wDay
mov bx, mySysTime.wMonth
mov cx, mySysTime.wYear
invoke wsprintf, addr buffer,
addr format,
eax,
ebx,
ecx
invoke MessageBox, 0,
addr buffer,
addr sz7788,
MB_OK
ret
WinMain endp
;================================================
It works correctly, but as the win32 calling convention dictates (because of C), word-sized data must be zero/sign-extended to dword-size before pushing.
sorry for the late reply.Just wanna say thanks for all ur help.