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

发布于 2024-10-07 10:22:36 字数 1353 浏览 3 评论 0原文

问题

我一直在尝试各种字节计数,试图获得WriteFile 即可工作。问题是写入文件后立即崩溃。所有文本都在文件中,但“程序已崩溃,发送到 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 

Problem

I have been trying all sorts of byte counts trying to get WriteFile to work. The problem is it immediately crashes after writing to the file. All the text is in the file but yet the "A program has crashed, send to Microsoft??" Error Dialog pops up.

When commenting out invoke WriteFile and everything below it, the program runs fine and does not crash. However when I just uncomment WriteFile and leave all the code below it commented out it again rears it's ugly head. The code is below and if anybody can see something that I missed it is much appreciated :-)

Byte Lengths I have tried.

I have tried byte lengths of 23, 24 (string length + null), 25 (Maybe I forgot a byte), and also just using SIZEOF WriteText and all of them failed :-(.

Code

.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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2024-10-14 10:22:37

根据WriteFile函数的文档

lpNumberOfBytesWritten [输出,可选]
[...]
仅当lpOverlapped参数不为NULL时,该参数才可以为NULL。

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

According to the documentation for the WriteFile function:

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

You have both lpNumberOfBytesWritten and lpOverlapped as NULL. Pass addr some_writable_variable as lpNumberOfBytesWritten and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文