hello i try to read a text file a place the data in a buffer
1) i use CreateFile to get the handle of the file
2)i use GetFileSizeEx to get the file size it worked with success
3) i use ReadFile but it not give a error or a success return it crash
that is the code
.386
.model flat, stdcall
option casemap:none
includelib C:\masm32\lib\kernel32.lib
includelib C:\masm32\lib\user32.lib
includelib C:\masm32\lib\msvcrt.lib
include C:\masm32\include\kernel32.inc ;cotient les prototype des function
include C:\masm32\include\user32.inc
include C:\masm32\include\windows.inc
printf PROTO C :ptr, :vararg
.data
kolo db "C:\Documents and Settings\servername\Bureau\t.txt",0
ggg dd 0
hhd dd 0
hhd2 dd 0
buff db 2600 DUP (0)
messageId DWORD ?
pErrorMsg DWORD ?
format db 'result: %s',13,10,0
format2 db 'result: %x',13,10,0
format3 db 'result: %d',13,10,0
.code
Main:
invoke CreateFile, OFFSET kolo, GENERIC_READ or GENERIC_WRITE, \
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
mov hhd,eax
mov hhd2,eax
invoke GetFileSizeEx, hhd, OFFSET ggg
invoke ReadFile, hhd2, OFFSET buff, ggg, NULL, NULL
INVOKE printf, addr format3, eax
call GetLastError
mov messageId,eax
INVOKE FormatMessage, FORMAT_MESSAGE_ALLOCATE_BUFFER + \
FORMAT_MESSAGE_FROM_SYSTEM, NULL, messageId, 0,
ADDR pErrorMsg, 0, NULL
INVOKE printf, addr format, pErrorMsg
INVOKE printf, addr format2, messageId
INVOKE LocalFree, pErrorMsg
end Main
thank you
jango,
Put it all in a ZIP file so that anyone who has the time can see what is happening with your code.
ok that is the file
jango,
Apologies that I did not have time to fix the test piece but here is a simple working example using the high level part of MASM32. Its useful in that it gets the logic right. You can manually code the API calls to do the same.
include \masm32\include\masm32rt.inc
.data?
hFile dd ?
flen dd ?
bRed dd ?
pMem dd ?
.code
Main:
mov hFile, fopen("t.txt") ; open the file
mov flen, fsize(hFile) ; get its length
mov pMem, alloc(flen) ; allocate memory
mov bRed, fread(hFile,pMem,flen) ; read file into buffer
print pMem,13,10 ; display file content
fclose hFile ; close the file
free pMem ; release the allocated memory
invoke ExitProcess,0 ; terminate the process
end Main
deleted
Ignore the nonsense jango, here is the mnemonic version of the high level code I posted earlier.
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
comment * -----------------------------------------------------
Build this console app with
"MAKEIT.BAT" on the PROJECT menu.
----------------------------------------------------- *
.data
fnam db "t.txt",0
crlf db 13,10,0
.code
start:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
call main
inkey
push 0
call ExitProcess
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
main proc
LOCAL bcnt :DWORD
LOCAL bwrd :DWORD
LOCAL hFile :DWORD
LOCAL pMem :DWORD
push 0
push FILE_ATTRIBUTE_NORMAL
push OPEN_EXISTING
push 0
push 0
push GENERIC_READ or GENERIC_WRITE
push OFFSET fnam
call CreateFile
mov hFile, eax
push 0
push hFile
call GetFileSize
mov bcnt, eax
push bcnt
push GMEM_FIXED or GMEM_ZEROINIT
call GlobalAlloc
mov pMem, eax
push 0
lea eax, bwrd
push eax
push bcnt
push pMem
push hFile
call ReadFile
push pMem
call StdOut
push OFFSET crlf
call StdOut
push pMem
call GlobalFree
push hFile
call CloseHandle
ret
main endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
end start
Very nice, Hutch.
But you had no need to remove the solution I presented many hours ago.
jangogrand is not dumb.
Simple Jose,
I removed all of the bullsh*t in one go. This is a new member who is subject to the protection of this forum and did not need his question thread full of invective and smart arse comments.
I know you are the boss here and you are making it clear to everybody.
But come on Hutch, your example is not what I would call a didactic example.
Jose,
While this thread is not the venue for debate, does a new member learning assembler need a "didactic example" ? I could code it in direct opcodes but would it be of any greater use ? I did the high level version earlier today when I was busy but it has the advantage of being easy to follow to get the logic of the API calls. The later version is how to code it in bare mnemonics.
What I don't want is for new members to have to wade through a mountain of invective and bullsh*t, new members are protected here to ensure this does not happen.
Quote from: hutch-- on August 14, 2017, 11:51:10 PM
What I don't want is for new members to have to wade through a mountain of invective and bullsh*t, new members are protected here to ensure this does not happen.
All right, I understand it is some sort of Witness Protection Program.
sorry i was not online to see what aw27 have post , hutch-- thank you so much :t
hutch-- can you please let me see the solution of aw27
jango,
Just ask AW to repost them as I deleted all of the stuff that was argumentative.
There was couple things to correct.
- GetFileSizeEx overwrites hhd
- ReadFile needs 4. DWORD parameter for read count, otherwise it could crash
- you can't read more than buffer size from file.
- ExitProcess at end
.386
.model flat, stdcall
option casemap:none
includelib kernel32.lib
ExitProcess PROTO :DWORD
CreateFileA PROTO :ptr,:DWORD,:DWORD,:ptr,:DWORD,:DWORD,:ptr
GetFileSizeEx PROTO :ptr,:ptr
ReadFile PROTO :ptr,:ptr,:DWORD,:ptr,:ptr
GetLastError PROTO
FormatMessageA PROTO :DWORD,:ptr,:DWORD,:DWORD,:ptr,:DWORD,:ptr
LocalFree PROTO :ptr
GENERIC_WRITE equ 40000000h
GENERIC_READ equ 80000000h
FILE_SHARE_READ equ 1
FILE_SHARE_WRITE equ 2
OPEN_EXISTING equ 3
FILE_ATTRIBUTE_NORMAL equ 00000080h
FORMAT_MESSAGE_ALLOCATE_BUFFER equ 00000100h
FORMAT_MESSAGE_FROM_SYSTEM equ 00001000h
NULL equ 0
includelib msvcrt.lib
printf PROTO C :ptr, :vararg
.data
fname db "test1.asm",0
fsl dd ? ; lodword size
fsh dd ? ; hidword size
rd dd ?
hf dd ?
buff db 2600 DUP (?)
messageId DWORD ?
pErrorMsg DWORD ?
format db 'result: %s',13,10,0
format2 db 'result: %x',13,10,0
format3 db 'result: %d',13,10,0
.code
Main:
mainCRTStartup proc
invoke CreateFileA, OFFSET fname, GENERIC_READ or GENERIC_WRITE, \
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
mov hf,eax
invoke GetFileSizeEx, hf, OFFSET fsl
invoke ReadFile, hf, OFFSET buff, SIZEOF buff, OFFSET rd, NULL
invoke printf, addr format3, eax
call GetLastError
mov messageId,eax
INVOKE FormatMessageA, FORMAT_MESSAGE_ALLOCATE_BUFFER + \
FORMAT_MESSAGE_FROM_SYSTEM, NULL, messageId, 0,
ADDR pErrorMsg, 0, NULL
INVOKE printf, addr format, pErrorMsg
INVOKE printf, addr format2, messageId
INVOKE LocalFree, pErrorMsg
INVOKE ExitProcess,0
mainCRTStartup endp
end Main
Quote from: jangogrand on August 15, 2017, 12:25:04 AM
can you please let me see the solution of aw27
jangogrand,
What I said was actually what TWell is saying, except that he adds that the buffer might not be enough for the file which I agree.
@Thank you TWell.
thank you so much TWell and aw27 , aw27 i have edit TWell code by adding the memory allocation used by hutch-- it get good result
:t :t :greenclp:
Quote from: nidud on August 14, 2017, 10:43:11 PM
:biggrin:
Quote
My advises;
1) Watch out for the drivers of ignorance on this board. Some pretend to be the master of driving but don't even know the inner workings of a carburettor.
2) Start with smaller engines and slowly progress with more pistons as you gain familiarity.
3) Driving are not for beginners. They are either for the ones who already appreciated whats under the hood and/or for the clueless retards to keep 'driving' for unknown purposes. There are logical reasons why Microsoft dropped the wheel completely in their 64-bit ABI.
This was aw27?