News:

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

Main Menu

Opening a file for writing and reading

Started by bluedevil, May 06, 2017, 11:55:52 PM

Previous topic - Next topic

bluedevil

Hello everyone, how are you doing?

I wanna create a text file with CreateFile and than write some string via WriteFile. Before closing the handle, i wanna read what is inside the created text file. Although invoking CreateFile with GENERIC_READ or GENERIC_WRITE and FILE_SHARE_READ or FILE_SHARE_WRITE; i can write the string, but can't read it back?
But i can read the text file after closing the handle and invoking the CreateFile again.

Here is the Code - this is a windows console source -:
main proc
invoke SetConsoleTitle,offset konsolBaslik ;title for console
invoke ClearScreen ;really?
print offset sctGS

invoke CreateFile,offset pDosyaYolu,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
mov hDosya,eax
invoke GetLastError ;eax = returns error code.
.if eax == INVALID_HANDLE_VALUE
print cat$(chr$("[ ERROR ] * Cannot open file. Error Code: "), str$(eax),chr$(13,10))
.elseif eax == ERROR_FILE_EXISTS
print cat$(chr$("[ WARNN ] * File Created! Error Code: "), str$(eax),chr$(13,10))
.elseif eax == ERROR_ALREADY_EXISTS
print cat$(chr$("[ WARN ] * File Already Created! Error Code: "), str$(eax),chr$(13,10))
invoke ReadFile,hDosya,offset genelTampon,100,offset NumOfBytesRead,NULL ;WORKS
invoke StdOut,offset genelTampon
invoke CloseHandle,hDosya ;close that damn handle
.else
invoke lstrlen,offset strMetin ;eax = lenght
invoke WriteFile,hDosya,offset strMetin,eax,offset NumOfBytesWritten,NULL
invoke ReadFile,hDosya,offset genelTampon,100,offset NumOfBytesRead,NULL ;->DOESNT WORK???
invoke StdOut,offset strMetin
invoke CloseHandle,hDosya ;close that damn handle
.endif

ret

main endp

Look at the line that says doesn't work. Did i miss something? Or it is not possible to write and read with the same handle created by CreateFile?

Also attach the project.
Thanks.
edit:because of misspell
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

nidud

#1
deleted

bluedevil

Quote from: nidud on May 07, 2017, 12:11:32 AM
After Write the offset is at the end of the file, so...
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365542(v=vs.85).aspx

And maybe use ADDR as oppose to OFFSET in INVOKE.

Thank you very much. I've learnt something new. :eusa_clap:
Before ReadFile i add this line:
invoke SetFilePointerEx,hDosya,NULL,NULL,FILE_BEGIN,0
And voila!

Here is the sample code:
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

jj2007

Quote from: nidud on May 07, 2017, 12:11:32 AMAnd maybe use ADDR as oppose to OFFSET in INVOKE.

What's the advantage?

nidud

#4
deleted

bluedevil

OFFSET returns address of variables declared in DATA or DATA? segments. You can't use OFFSET for local variables.
ADDR returns global variables same as OFFSET -by pushing them on the stack- but also returns the LOCAL variables addresses -by loading their effective addresses to eax and then pushin eax to stack-

I generally use OFFSET for global and ADDR for local variables. So while coding i can differ variables. That is why i used OFFSET int example above.

Am i wrong @nidud?
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

nidud

#6
deleted

bluedevil

..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

newrobert


bluedevil

AFAIK, this is called "Direct memory addressing"
.DATA
    szStr        dd 0DEADBEEFh
.CODE
    mov eax, [szStr]                 ;copy szStr content to eax, eax = DEADBEEF


In the debugger:
00401025  /$  A1 7F374000   MOV EAX,DWORD PTR DS:[0x40377F]
In the hex dump we can see at address 0x40377F has DEADBEEF in reverse order
0040377F  EF BE AD DE                                      ï¾­Þ

when you try:
mov eax, addr szStr        ;it fails
mov eax, offset szStr        ;it works

and in the debugger mov eax, offset szStr will be like:
0040102F  |.  B8 7F374000   MOV EAX,0x40377F
eax will have the 40377F address not the value inside.
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github

mineiro

Quote from: blue_devil on May 07, 2017, 04:09:24 PM
mov eax, addr szStr        ;it fails
mov eax, offset szStr        ;it works
lea eax,offset szStr        ;it works, to global or local variables (lea = load effective address)

hello sir blue_devil;
that don't work because scopes, if we are inside 'invoke' scope so that works, if not that don't work.
'addr' works as 'offset' inside invoke scope, on other places not.
If we search for 'invoke' on intel, amd manuals we can't find that word as being an instruction, that processor don't have that instruction. This means that this is an abstraction, or on other words, the team that have programmed masm or other assembler have created that name as being an automation tool to call (call is an instruction) functions even using other calling conventions (c, stdcall,...).

Quote
In the hex dump we can see at address 0x40377F has DEADBEEF in reverse order
That's little endian order. On some processors that data on memory is really "deadbeef", but intel followed little endian way to store data while per example motorola followed big endian order.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

jj2007

Quote from: newrobert on May 07, 2017, 11:32:51 AM
addr szStr is equl [szStr]?

No.
include \masm32\MasmBasic\MasmBasic.inc

.data
szStr db "Hello World", 0

Init
  mov eax, dword ptr [szStr] ; absolutely
  mov ebx, dword ptr szStr ; identical!
  mov ecx, offset szStr
  deb 4, "Results decimal:", eax, ebx, ecx
  deb 4, "Results hex:", x:eax, x:ebx, x:ecx
EndOfCode


Results decimal:
eax             1819043144
ebx             1819043144
ecx             4222976

Results hex:
x:eax           6C6C6548
x:ebx           6C6C6548
x:ecx           00407000


One can learn a lot by using a debugger like Olly.

mineiro

Oh, I forgot to say one detail;
You're assuming that betwen brackets "[]" means 'contents of addresss', this is truth to think if you recognize the only exception that is 'lea' instruction. Lea instruction can be used inside invoke scope as 'addr' too.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

bluedevil

Quote from: mineiro on May 07, 2017, 09:29:33 PM
lea eax,offset szStr        ;it works, to global or local variables (lea = load effective address)
by using offset we have already getting the address of variable. lea instruction is also aimed to get effective address.
so should we use like this:
lea eax, szStr
When i checked on debugger:
00401025  /$  8D05 7F374000 LEA EAX,DWORD PTR DS:[0x40377F];same with below
0040102B  |.  8D05 7F374000 LEA EAX,DWORD PTR DS:[0x40377F];

so lea instruction doesn't care offset  operator?

Quote from: mineiro on May 07, 2017, 09:29:33 PMIf we search for 'invoke' on intel, amd manuals we can't find that word as being an instruction, that processor don't have that instruction. This means that this is an abstraction, or on other words, the team that have programmed masm or other assembler have created that name as being an automation tool to call (call is an instruction) functions even using other calling conventions (c, stdcall,...).
wow, exactly. thats why we cant use invoke in Visual Studio. We can only use "call" instructor, right?

Quote from: mineiro on May 07, 2017, 09:29:33 PMThat's little endian order. On some processors that data on memory is really "deadbeef", but intel followed little endian way to store data while per example motorola followed big endian order.
Exactly!
..Dreams make the future
But the past never lies..
BlueDeviL // SCT
My Code Site:
BlueDeviL Github