delphi 编写 vxd 设备驱动程序

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
用 delphi 编写 vxd 设备驱动程序(转)
关键词:delphi控件杂项
作者:emil biserov(dinfo@mail.primorye.ru)(russion)
英语翻译:vitaly zayko(zayko@vitsoftware.com)
中文翻译改编:riceball(teditor@mailroom.com)
前言
 用 delphi 3.0 编写 vxd 设备驱动程序,在delphi 3 下编译通过,delphi 2 下没有测试,delphi 4 建立的 object 文件 m$ linker 5.12.8181 不能识别,这里使用的汇编器是m$的macro assembler ver. 6.11d ,联结器是m$ incremental linker ver. 5.12.8181 ,它们来自 windows 98ddk(http://www.microsoft.com/ddk/ddk98.htm)。
介绍
 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:~198ddk-coff -dbld_coff -dis_32 -w2 -c -cx -zm -dmasm6 vxdmain.asm
call dcc3.bat -j vxdprocs.pas
d:~198ddk/def:vxddef.def /vxd vxdmain.obj vxdprocs /out:delphiio.vxd
附二:
现在让我们来编写对该 vxd 的测试程序,两个按钮:一个打开 vxd;一个关闭 vxd。
const
vxdname = '/..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,你可以改.
 

Similar threads

I
回复
0
查看
662
import
I
I
回复
0
查看
573
import
I
I
回复
0
查看
594
import
I
顶部