前言 用 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:\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,你可以改.
|