2017-05-31 11 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

あなたがいうだけで 'lnt'よりlnt'オフセット'推進しているのReadFileを呼び出すとき。 'nNumberOfBytesToRead'引数は、アドレスではなく値によって渡されます。 –

+1

また、長さ 'lnt'をascii文字列として提供する必要があるかどうかは疑問です。代わりに 'lnt dd 1024'を試してみる – Tommylee2k

答えて

1

つの問題を実行することができます。

  • bufferLength db ?埋蔵1バイトのみを。 ReadFileはそこにDWORDを格納し、bufferの3バイトを上書きします。 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 
関連する問題