gpt4 book ai didi

winapi - WriteFile字符串字节长度导致崩溃

转载 作者:行者123 更新时间:2023-12-03 15:52:54 26 4
gpt4 key购买 nike

问题

我一直在尝试各种字节计数,以使WriteFile正常工作。问题是写入文件后立即崩溃。所有文本都在文件中,但是“程序已崩溃,发送给Microsoft?”弹出错误对话框。

当注释掉调用WriteFile及其下面的所有内容时,该程序可以正常运行并且不会崩溃。但是,当我只取消注释WriteFile并在其下面保留所有代码时,将其注释掉,这再次使它变得丑陋。代码在下面,如果有人可以看到我错过的内容,将不胜感激:-)

我尝试过的字节长度。

我尝试了23、24(字符串长度+空值),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

最佳答案

根据the documentation for the WriteFile function:

lpNumberOfBytesWritten [out, optional]
[...]
This parameter can be NULL only when the lpOverlapped parameter is not NULL.



lpNumberOfBytesWritten和lpOverlapped都为NULL。将 addr some_writable_variable传递为lpNumberOfBytesWritten,它应该可以工作。

关于winapi - WriteFile字符串字节长度导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com