当前位置:编程学习 > Delphi >>

Delphi研究之驱动开发篇(七)--利用共享内存与用户模式程序通讯(下)

Cleanup过程进行的工作都是很显然的,不用过多解释。唯一的奥妙在于将内存映射到用户空间和还原操作是借助于MmUnmapLockedPages函数实现的,应该在进程定义的地址上下文中进行,这是很自然的。
以上就是整个的驱动程序,利用内核计时器大约每1秒钟读取一次系统时间并写入到共享内存中供用户程序读取。下面我们来看看用户程序。

代码:unit main;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ExtCtrls;

type
    TForm1 = class(TForm)
      Timer1: TTimer;
      Label1: TLabel;
      procedure FormActivate(Sender: TObject);
      procedure Timer1Timer(Sender: TObject);
      procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
      { Private declarations }
    public
      { Public declarations }
    end;

var
    Form1: TForm1;

implementation

uses
    WinSvc, nt_status, macros;

const
    _Delete = $10000;   

var
    hDevice: THANDLE;
    pSharedMemory: PFILETIME;
    hSCManager: THANDLE;
    hService: THANDLE;

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
var
    acModulePath: string;
    dwBytesReturned: DWORD;
    lpTemp: PChar;
    IOCTL_GIVE_ME_YOUR_MEMORY: DWORD;
begin
    IOCTL_GIVE_ME_YOUR_MEMORY := CTL_CODE(FILE_DEVICE_UNKNOWN,
                                          $800, METHOD_BUFFERED,
                                          FILE_READ_ACCESS);
    hSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if hSCManager <> 0 then
    begin
      acModulePath := GetCurrentDir + + ExtractFileName(SharingMemory.sys);
      hService := CreateService(hSCManager, SharingMemory,
                                Another way how to share memory,
                                SERVICE_START or SERVICE_STOP or _Delete,
                                SERVICE_KERNEL_DRIVER,
                                SERVICE_DEMAND_START,
                                SERVICE_ERROR_IGNORE,
                                PChar(acModulePath),
                                nil, nil, nil, nil, nil);
      if hService <> 0 then
      begin
        if StartService(hService, 0, lpTemp) then
        begin;
          hDevice := CreateFile(PChar(\.SharingMemory),
                                GENERIC_READ, 0,
                                nil, OPEN_EXISTING, 0, 0);
          if hDevice <> INVALID_HANDLE_VALUE then
          begin
            if DeviceIoControl(hDevice, IOCTL_GIVE_ME_YOUR_MEMORY,
                               nil, 0, @pSharedMemory,
                               sizeof(PVOID),
                               dwBytesReturned,
                               nil) then
            begin
              if dwbytesReturned = sizeof(pSharedMemory) then
              begin
                Timer1.Enabled := true;    {激活用户端计时器,每隔1秒读取一次共享内存}
              end;                      
            end else
            begin
              ShowMessage(Cant send control code to device.);
            end;
          end else
          begin
            ShowMessage(Device is not present.);
          end;
        end else
        begin
          ShowMessage(Cant start driver.);
        end;
      end else
      begin
        ShowMessage(Cant register driver.);
      end;
  &

补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,