【问题标题】:masm32 ReadFile Function x86 -Windowsmasm32 读取文件功能 x86 -Windows
【发布时间】:2017-05-31 04:11:19
【问题描述】:

我一直在尝试读取写入 .txt 文件中的字符串并在控制台上打印出来。但似乎我做得不对。有人可以查看我的代码并告诉我出了什么问题吗?谢谢!

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

【问题讨论】:

  • 当您调用 ReadFile 时,您推送的是 offset lnt 而不仅仅是 lntnNumberOfBytesToRead 参数按值传递,而不是按地址传递。
  • 我也怀疑你必须提供长度 lnt 作为 ascii 字符串。我会尝试'lnt dd 1024'

标签: windows assembly command-line x86 masm32


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多