2017-05-31 82 views
0

我一直在嘗試讀取在.txt文件中寫入的字符串,並在控制檯上將其打印出來。但是,我似乎做得不對。有人可以查看我的代碼並告訴我發生了什麼問題嗎?謝謝!masm32 ReadFile函數x86 -Windows

include \masm32\include\masm32rt.inc 

.data 
    txtFilter db "*.txt",0 

    txtFD WIN32_FIND_DATA <> 
    txtHandle HANDLE ? 
    fHandle HANDLE ? 

    bufferLength db ? 
    buffer db 5000 dup(?) 
    lnt db "1024",0 

    okay db "Okay!",0 
    dokay db "Dokay!",0 

.code 
start: 
    push offset txtFD 
    push offset txtFilter 
    call FindFirstFile 

    mov txtHandle, eax 

    push offset txtFD.cFileName 
    call StdOut 

    push 0 
    push FILE_ATTRIBUTE_NORMAL 
    push OPEN_EXISTING 
    push 0 
    push 0 
    push FILE_APPEND_DATA 
    push offset txtFD.cFileName 
    call CreateFile 

    .if eax == INVALID_HANDLE_VALUE 
    jmp _error 
    .else 
    mov fHandle, eax 
    .endif 

    push 0 
    push offset bufferLength 
    push offset lnt 
    push offset buffer 
    push fHandle 
    call ReadFile 

    jmp _next 

_error: 
    push offset dokay 
    call StdOut 
    jmp _next 

_okay: 
    push offset okay 
    call StdOut 

_next: 
    push offset buffer 
    call StdOut 

    push fHandle 
    call CloseHandle 

    push txtHandle 
    call FindClose 

    push 0 
    call ExitProcess 

end start 

該代碼似乎無法讀取我的txt文件中的內容。不過,我可以成功地搜索我的txt文件,並執行函數CreateFile

+0

當你調用ReadFile的你是推'偏移lnt'而不僅僅是'lnt'。 'nNumberOfBytesToRead'參數是按值傳遞的,而不是按地址傳遞的。 –

+1

我也懷疑你必須提供'lnt'作爲ascii字符串的長度。我會嘗試'lnt dd 1024' – Tommylee2k

回答

1

四個問題:

  • bufferLength db ?儲量只有一個字節。 ReadFile將存儲一個DWORD並覆蓋buffer的三個字節。如果有NULL,則StdOut將停止輸出。更改定義爲bufferLength dd ?

  • lnt db "1024",0是一個字符串。 ReadFile需要一個DWORD值。將其更改爲lnt dd 1024

  • push FILE_APPEND_DATA僅爲寫入創建句柄。將其更改爲push GENERIC_READ

  • push offset lnt傳遞指針。但是,ReadFile需要DWORD值。將其更改爲push lnt

就像是:

include \masm32\include\masm32rt.inc 

.data 
    txtFilter db "*.txt",0 

    txtFD WIN32_FIND_DATA <> 
    txtHandle HANDLE ? 
    fHandle HANDLE ? 

; bufferLength db ? 
    bufferLength dd ? 
    buffer db 5000 dup(?) 
; lnt db "1024",0 
    lnt dd 1024 

    okay db "Okay!",0 
    dokay db "Dokay!",0 

.code 
start: 
    push offset txtFD 
    push offset txtFilter 
    call FindFirstFile 

    mov txtHandle, eax 

    push offset txtFD.cFileName 
    call StdOut 

    ; https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx 
    push 0       ; HANDLE hTemplateFile 
    push FILE_ATTRIBUTE_NORMAL  ; DWORD  dwFlagsAndAttributes 
    push OPEN_EXISTING    ; DWORD  dwCreationDisposition 
    push 0       ; LPSECURITY_ATTRIBUTES lpSecurityAttributes 
    push 0       ; DWORD  dwShareMode 
; push FILE_APPEND_DATA   ; DWORD  dwDesiredAccess 
    push GENERIC_READ    ; DWORD  dwDesiredAccess 
    push offset txtFD.cFileName  ; LPCTSTR lpFileName, 
    call CreateFile 

    .if eax == INVALID_HANDLE_VALUE 
     jmp _error 
    .else 
     mov fHandle, eax 
    .endif 

    ; https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx 
    push 0       ; LPOVERLAPPED lpOverlapped 
    push offset bufferLength  ; LPDWORD lpNumberOfBytesRead 
; push offset lnt     ; DWORD  nNumberOfBytesToRead 
    push lnt      ; DWORD  nNumberOfBytesToRead 
    push offset buffer    ; LPVOID lpBuffer 
    push fHandle     ; HANDLE hFile 
    call ReadFile 

    jmp _next 

_error: 
    push offset dokay 
    call StdOut 
    jmp _next 

_okay: 
    push offset okay 
    call StdOut 

_next: 
    push offset buffer 
    call StdOut 

    push fHandle 
    call CloseHandle 

    push txtHandle 
    call FindClose 

    push 0 
    call ExitProcess 

end st