[英]OpenFile not working in x86 NASM assembly
我一直在使用以下代碼編寫程序,以打開其自身的文件句柄並讀取其內容,但是我遇到了問題...這是代碼...
extern GetStdHandle
extern GetModuleFileNameA
extern OpenFile
extern WriteFile
extern ExitProcess
import GetStdHandle kernel32.dll
import GetModuleFileNameA kernel32.dll
import OpenFile kernel32.dll
import WriteFile kernel32.dll
import ExitProcess kernel32.dll
global ..start
segment .code USE32
;Get standard output handle for writing to the console
push dword -11
call [GetStdHandle]
mov dword [hStdOut], eax
push dword filepath ;Buffer to store filepath
push dword 0 ;Setting this to NULL retrieves file name of
;the program's exe on disk
call [GetModuleFileNameA] ;ANSI format of return string (the way that
;OpenFile likes it)
;Here we are checking to see if the filename was returned
;On the console it looks blank but if you dump the output of the
;program to a file using the command program.exe > program.dump the
;path shows up in a hex editor. So the path is fine...
push dword 0
push dword bytesRead
push dword 128 ;Maximum path size for OpenFile
push dword filepath
push dword [hStdOut]
call [WriteFile]
push dword 0
push dword ofstruct
push dword filepath
call [OpenFile]
mov dword [hSelfFile], eax
push dword 0
push dword bytesRead
push dword 32 ;Arbitrary number to show the beginning of
;hSelfFile to see if the handle is pointing to
;the file. It should show the magic number MZ
;at the beginning if we're doing this right
push dword hSelfFile
push dword [hStdOut]
call [WriteFile]
;Here is where we should see the elusive MZ magic number in the
;output
;Yes I will use CloseHandle (even though the program automatically
;closes all open handles before exiting), but for now I need to see
;if the OpenFile actually works or not before I do such things
push 0
call [ExitProcess]
segment .data
segment .bss
hStdOut resd 1
hSelfFile resd 1
bytesRead resd 1
ofstruct resb 136
filepath resb 128
樣品輸出...
C:\DOCUME~1\Admin\Desktop\asm>test0.exe
C:\DOCUME~1\Admin\Desktop\asm\test0.exe
Φ ê☺ σF±ÿC:\DOCUME~1\Admi
C:\DOCUME~1\Admin\Desktop\asm>
OpenFile我在做什么錯?
在WriteFile輸出文件內容之前,需要使用ReadFile將文件讀入緩沖區,僅通過將文件句柄從OpenFile傳遞到WriteFile不能讀取該文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.