繁体中文
设为首页
加入收藏
当前位置:技术首页 >> 开发 >> Delphi >> 用Delphi编写VxD设备驱动程序

用Delphi编写VxD设备驱动程序

2006-10-11 17:12:52  作者:admin  来源:赛迪网  浏览次数:135  文字大小:【】【】【
关键字:Delphi
 介绍

windows 存在有两种类型的 vxd 设备驱动程序:

1、静态(static) vxd ,装入操作系统并永久的存在于内存中;
2、动态(dynamic) vxd,当需要时才调入内存,用完后关闭vxd即可释放内存。

inprise delphi 有能力建立任何一种类型的 vxd 设备驱动程序,下面我们将介绍如何建立动态 vxd。

当 win32 应用程序打开一个 vxd “虚拟”设备时,vwin32 使用 loaddevice 将 vxd 装入内存,并建立消息w32_deviceiocontrol ,发向 vxd。也就是说,vxd 至少应该响应以下两个系统信息和编写以下的一个函数:

sys_dynamic_device_init
sys_dynamic_device_exit
w32_deviceiocontrol 函数

消息 sys_dynamic_device_init 在尝试装入 vxd 时发送到 vxd ,消息 sys_dynamic_device_exit 在尝试动态交换时发送到 vxd ,消息的处理者在成功处理后,应该在寄存器 ax 中返回 vxd_success 标志。

w32_deviceiocontrol 的 dwservice 参数有以下的值:

dioc_open 当 vxd 通过 createfile() 函数尝试打开操作时发送(在 sys_dynamic_device_init 消息后),如果成功返回 no_error (0); 

dioc_closehandle 当 vxd 通过 closehandle() 函数尝试关闭操作时发送(在 sys_dynamic_device_exit 前)

所有其它的值 > 0 意味着不同的函数调用(由 dwiocontrolcode 给出),当 vxd 被 deviceiocontrol 函数调用时。

启动模块(vxdmain.asm)

... 

extrn sysdynamicdeviceinit :proc 

extrn sysdynamicdeviceexit :proc 

extrn w32deviceiocontrol  :proc 

... 

public delphiio_ddb 

public @@handlefinally 

public @initialization 

... 

control_0proc 

cmpeax, sys_dynamic_device_init 

jnzshort chksysdynexit 

callsysdynamicdeviceinit 

cmpeax, 1 

retn 

;------------- 

chksysdynexit: 

cmpeax, sys_dynamic_device_exit 

jnzshort chkdevioctl 

callsysdynamicdeviceexit 

cmpeax, 1 

retn 

;------------- 

chkdevioctl: 

cmpeax, w32_deviceiocontrol 

jnzshort loc_ret 

pushesi 

pushedx 

pushebx 

pushecx 

callw32deviceiocontrol 

cmpeax, 1 

retn 

;------------- 

loc_ret: 

clc 

retn 

control_0endp 

@@handlefinally: 

@initialization: 

ret 

_ltext  ends 

end

delphi 会为单元的 initialization/finalization 建立代码调用外部过程 handlefinaly 和 initialization ,即使 initialization/finalization 在单元中不存在。因此我们在汇编的启动文件中建立空的外部过程入口。

主 delphi 程序单元(vxdprocs.pas) 

... 

procedure shellmessage(handle, flags : integer; const message, caption : pchar; 

  callback, referencedata : pointer); stdcall; assembler; 

asm 

  movebx, handle// virtual machine handle 

  moveax, flags// message box flags 

  movecx, message// address of message text 

  movedi, caption// address of caption text 

  movesi, callback// address of callback 

  movedx, referencedata// reference data for callback 

 int20h// vxdcall 

  dd 170004h// shell_message 

end; 

function sysdynamicdeviceinit : integer; 

begin 

  shellmessage(0, $10, copyright, ’sysdyninit: hello from delphi vxd !!!’, nil, nil); 

  result := vxd_success; 

end; 

function sysdynamicdeviceexit : integer; 

begin 

  shellmessage(0, $10, copyright, ’sysdyndevexit: bye from delphi vxd !!!’, nil, nil); 

  result := vxd_success; 

end; 

function w32deviceiocontrol(dwservice : integer; 

dwddb : integer; 

hdevice : integer; 

lpdiocparms : pointer) : integer; 

begin 

  shellmessage(0, $10, copyright, ’w32devioctl’, nil, nil); 

 if (dwservice = dioc_open) then 

  begin 

  result := no_error; 

  end 

  else if (dwservice = dioc_closehandle) then 

  begin 

  result := vxd_success; 

  end 

  else if (dwservice > max_pasvxd_w32_api) then 

  begin 

  result := error_not_supported; 

  end 

else 

  begin 

  result := vxd_success; 

  end; 

end; 

...

[译者:好了,简单的 vxd 设备驱动程序编写完毕了。你可以将它当作一个写 vxd 设备驱动程序的模板。


点击收藏到

责任编辑:admin

本文引用地址: http://tech.itzero.com/2006/1011/838.html 请粘贴到你的QQ/MSN上推荐给你的好友

相关文章
Delphi使用VB编写的ActiveX控件全攻略
在delphi.net的VCL.net里使用Ado.net
用Delphi开发Web服务数据库程序
Delphi程序实现对光驱盘盒的开关控制
Delphi中动态链接库(DLL)的建立和使用
在delphi.net的VCL.net里使用Ado.net
Delphi中优秀的字符串分割函数
在Delphi中如何维护COM+的状态信息
Delphi使用VB编写的ActiveX控件全攻略
在delphi中使用xml文档的两种方法
 

最新文章

更多

· Delphi使用VB编写的Acti...
· 在delphi.net的VCL.net里...
· 用Delphi开发Web服务数据...
· Delphi程序实现对光驱盘...
· Delphi中动态链接库(DLL...
· 在delphi.net的VCL.net里...
· Delphi中优秀的字符串分...
· 在Delphi中如何维护COM+...
· 用Delphi编写VxD设备驱动...
· Delphi使用VB编写的Acti...

热点文章

更多

其它推荐