WriteFile 字符串字节长度导致崩溃
问题
我一直在尝试各种字节计数,试图获得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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据WriteFile函数的文档:
您将 lpNumberOfBytesWritten 和 lpOverlapped 都设置为 NULL。将
addr some_writable_variable
作为 lpNumberOfBytesWritten 传递,它应该可以工作。According to the documentation for the WriteFile function:
You have both lpNumberOfBytesWritten and lpOverlapped as NULL. Pass
addr some_writable_variable
as lpNumberOfBytesWritten and it should work.