首页
社区
课程
招聘
[求助]关于移动文件指针
发表于: 2007-1-23 11:57 5474

[求助]关于移动文件指针

2007-1-23 11:57
5474
比如一个文件,hosts,我想在文件的最后添加一段:
127.0.0.1  www.163.com
我记得是移动文件指针到文件的末尾处,不过我把代码编译执行后,hosts原来的内容被清空了.

代码:

.386
.model flat,stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc

includelib kernel32.lib
includelib user32.lib

.data?
hSize dd ?
.data
szLogFile db "hosts",0
lpBuffer db "127.0.0.1  www.163.com",0

.code
start:
_test proc
    LOCAL @hFile:DWORD
    LOCAL BytesToRead:DWORD
   invoke  CreateFile,addr szLogFile,GENERIC_WRITE,FILE_SHARE_READ,\
            0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0;打开文件
            mov @hFile,eax
   .if eax != INVALID_HANDLE_VALUE
   invoke lstrlen,addr lpBuffer ;计算写入内容的长度
   mov hSize,eax
invoke SetFilePointer,@hFile,0,0,FILE_END ;移动文件指针到尾处
invoke WriteFile,@hFile,addr lpBuffer,hSize,addr BytesToRead,NULL ;开始写文件
.endif
invoke CloseHandle,@hFile ;关闭句柄
ret
_test endp
call _test
invoke ExitProcess,NULL
end start

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 237
活跃值: (31)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
CreateFile参数不对,应改为

invoke  CreateFile,addr szLogFile,GENERIC_WRITE,FILE_SHARE_READ,\
            0,[COLOR=red]OPEN_EXISTING[/color],FILE_ATTRIBUTE_NORMAL,0;打开文件


参见下面的说明

dwCreationDisposition
Specifies which action to take on files that exist, and which action to take when files do not exist.
For more information about this parameter, see the remarks section. This parameter must be one of the
following values
指定当文件存在或者不存在时如何动作。关于这个参数更多的信息,参考批注部分。这个参数必须是一个或多个
下列值。

VALUE(值)  Neaning(含义)
CREATE_NEW  Creates a new file. The function fails if the specified file already exists
    创建一个新文件. 如果该文件已经存在函数则会失败.

CREATE_ALWAYS  Creates a new file. If the file exsts, the function overwrites the file and
    clears the existing attributes.
    创建一个新文件.如果该文件已经存在,函数将覆盖已存在的文件并清除已存在的文件属性

OPEN_EXISTING  Opens the file. The function fails if the file does not exist.
    See the Remarks section for a discussion of why you should use the
    OPEN_EXISTING flag if you are using the CreateFile function for devices,
    including the console.
    打开一个文件,如果文件不存在函数将会失败.
    如查你使用CreateFile函数为设备装载控制台.请查看批注中的"为什么使用
    OPEN_EXISTING标志"的部分.
   
OPEN_ALWAYS  Opens the file, if it exsts. If the file does not exist, the function creates
    the file as if dwCreationDisposition were CREATE_NEW.
    如果文件存在,打开文件. 如果文件不存在,并且参数中有CREATE_NEW标志,则创建文件.

TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero
    bytes The calling process must open the file with at least GENERIC_WRITE access.
    The function fails if the file does not exist.
    打开一个文件,每次打开,文件将被截至0字节.调用进程必须用GENERIC_WRITE访问模式打
    开文件.如果文件不存在则函数就会失败.
2007-1-23 12:36
0
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
3
搞定,谢谢了.
2007-1-23 19:09
0
雪    币: 237
活跃值: (31)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
,路过时看见了
2007-1-24 21:53
0
游客
登录 | 注册 方可回帖
返回
//