Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Delphi > 用Delphi编写VxD设备驱动程序
【标  题】:用Delphi编写VxD设备驱动程序
【关键字】:程序,Delphi,Del,VxD,Delph,Delphi,VxD
【来  源】:网络

用Delphi编写VxD设备驱动程序

Your Ad Here 介绍

 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_0  proc
   cmp  eax, sys_dynamic_device_init
   jnz  short chksysdynexit
   call  sysdynamicdeviceinit
   cmp  eax, 1
   retn  
;-------------

chksysdynexit:
   cmp  eax, sys_dynamic_device_exit
   jnz  short chkdevioctl
   call  sysdynamicdeviceexit
   cmp  eax, 1
   retn  
;-------------
chkdevioctl:
   cmp  eax, w32_deviceiocontrol
   jnz  short loc_ret
   push  esi
   push  edx
   push  ebx
   push  ecx
   call  w32deviceiocontrol
   cmp  eax, 1
   retn  
;-------------
loc_ret:
   clc  
   retn  

control_0  endp

@@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
  mov  ebx, handle    // virtual machine handle
  mov  eax, flags    // message box flags
  mov  ecx, message    // address of message text
  mov  edi, caption    // address of caption text
  mov  esi, callback    // address of callback
  mov  edx, referencedata    // reference data for callback

 int  20h      // 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 设备驱动程序的模板。]

  附一:make.bat

d:\visual~1\98ddk\bin\win98\ml -coff -dbld_coff -dis_32 -w2 -c -cx -zm -dmasm6 vxdmain.asm
call dcc3.bat -j vxdprocs.pas
d:\visual~1\98ddk\bin\link /def:vxddef.def /vxd vxdmain.obj vxdprocs /out:delphiio.vxd

  附二:

  现在让我们来编写对该 vxd 的测试程序,两个按钮:一个打开 vxd;一个关闭 vxd。

const
vxdname = ’\\.\delphiio.vxd’;

...

function tvxdtestform.openvxddriver: boolean;
begin
hvxdhandle := createfile(vxdname,0,0,nil,0,file_flag_delete_on_close,0);
result := hvxdhandle <> invalid_handle_value;
end;

procedure tvxdtestform.closevxddriver;
begin
if hvxdhandle <> invalid_handle_value then begin
  closehandle(hvxdhandle);
  hvxdhandle := invalid_handle_value;
end;
end

 

  顺便说一下,delphi中有个编译选项可以控制程序加载的入口

  一般是0x00400000,你可以改。

Delphi8 for .net 的 VCL 类库:【上一篇】
Delphi使用VB编写的ActiveX控件全攻略:【下一篇】
【相关文章】
  • Delphi8 for .net 的 VCL 类库
  • Delphi+Word解决方案参考
  • 拦截其它程序的网络数据封包
  • 用Delphi制作DLL小结
  • Delphi Informant杂志 2003年度读者选择大奖名单
  • 学着给朋友做的一个顾客管理程序
  • 程序员网上考试系统 v1.1 (三层C/S)
  • 奇迹游戏装备更改器DELPHI代码
  • Delphi Tip Explorer V2.0 (分类管理技巧)
  • DelphiFun 1.0 (Delphi函数查询器+源代码+Access库)
  • 【随机文章】
  • zope 学习笔记 1
  • tomcat Load Balancing
  • Photoshop渐变工具4
  • 开心就贴(四)
  • 一个主引导区病毒的分析(3)
  • MQ系统管理编程概述
  • Visual Studio 2005 Service Pack 1 (SP1) 发布了
  • [ASP.NET AJAX]类似.NET框架的JavaScript扩展
  • 一段模拟按键查找文件的JS
  • 在sql server express中安装示例数据库
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.