Delphi研究之驱动开发篇(七)--利用共享内存与用

页面导航:首页 > 软件编程 > Delphi > Delphi研究之驱动开发篇(七)--利用共享内存与用

Delphi研究之驱动开发篇(七)--利用共享内存与用

来源: 作者: 时间:2016-02-05 11:47 【

Cleanup过程进行的工作都是很显然的,不用过多解释。唯一的奥妙在于将内存映射到用户空间和还原操作是借助于MmUnmapLockedPages函数实现的,应该在进程定义的地址上下文中进行,这是很

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
    WinS, 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;
  &

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<