问题
我一直在尝试各种字节计数,试图让WriteFile起作用。问题是写入文件后立即崩溃。所有文本都在文件中但是“A程序崩溃了,发送给Microsoft?”弹出错误对话框。
当注释掉调用WriteFile及其下面的所有内容时,程序运行正常并且不会崩溃。然而,当我只是取消注释WriteFile并将其下面的所有代码留下来时,它再次注释掉了它的丑陋头脑。代码在下面,如果有人能看到我错过的东西,我们非常感激: - )
我尝试过的字节长度。
我已经尝试了字节长度23,24(字符串长度+ null),25(也许我忘了一个字节),还只使用SIZEOF WriteText并且所有这些都失败了: - (。
代码
.386
.model flat,stdcall
option casemap:none ; Case Sensitive
; Windows
include \masm32\include\windows.inc
; Kernel32
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
.data
FilePath db "C:\test.txt",0
WriteText db "This is some test text."
.code
start:
; Edit a file
invoke CreateFile, addr FilePath, GENERIC_WRITE, FILE_SHARE_WRITE or FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
push eax ; save the file handle
; This works other than the crashing, any number less then 23
; and the file has some of the text clipped
; any larger and NUL is appended until the byte count is matched.
invoke WriteFile, eax, addr WriteText, 23, NULL, NULL
pop eax
push eax
invoke CloseHandle, eax
invoke ExitProcess, 0
end start
答案 0 :(得分:4)
根据the documentation for the WriteFile function:
lpNumberOfBytesWritten [out,optional]
[...]
仅当lpOverlapped参数不为NULL时,此参数才为NULL。
你有lpNumberOfBytesWritten和lpOverlapped都是NULL。将addr some_writable_variable
作为lpNumberOfBytesWritten传递,它应该可以正常工作。