驅動程式 - Windows Driver Model (WDM) - 使用範例 - Pascal (DDDK) - Use Event



參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk

main.pas

unit main;
 
interface
    uses
        DDDK;
         
    const
        MAX_THREAD = 3;
        DEV_NAME = '\Device\MyDriver';
        SYM_NAME = '\DosDevices\MyDriver';
 
    function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall;
 
implementation
var
    myEvent : KEVENT;
    pNextDevice : PDEVICE_OBJECT;
 
procedure MyThread(pParam : Pointer); stdcall;
var
    tt : LARGE_INTEGER;
    
begin
    case(ULONG(pParam)) of
    0:
        begin
            tt.HighPart := tt.HighPart or -1;
            tt.LowPart := ULONG(-10000000);
            DbgPrint('Thread%d, Sleeping', [ULONG(pParam)]);
            KeDelayExecutionThread(KernelMode, FALSE, @tt);
            DbgPrint('Thread%d, SetEvent', [ULONG(pParam)]);
            KeSetEvent(@myEvent, IO_NO_INCREMENT, FALSE);
        end;
    else
        begin
            DbgPrint('Thread%d, Waiting', [ULONG(pParam)]);
            KeWaitForSingleObject(@myEvent, Executive, KernelMode, FALSE, Nil);
            DbgPrint('Thread%d, Complete', [ULONG(pParam)]);
        end;
    end;
    PsTerminateSystemThread(STATUS_SUCCESS);
end;
 
procedure Unload(pMyDriver : PDRIVER_OBJECT); stdcall;
begin
end;
 
function IrpPnp(pMyDevice : PDEVICE_OBJECT; pIrp : PIRP) : NTSTATUS; stdcall;
var
    pStack : PIO_STACK_LOCATION;
    suSymName : UNICODE_STRING;
           
begin
    pStack := IoGetCurrentIrpStackLocation(pIrp);
    if pStack^.MinorFunction = IRP_MN_REMOVE_DEVICE then
        begin
            RtlInitUnicodeString(@suSymName, SYM_NAME);
            IoDetachDevice(pNextDevice);
            IoDeleteDevice(pMyDevice);
            IoDeleteSymbolicLink(@suSymName);
            IoCompleteRequest(pIrp, IO_NO_INCREMENT);
            Result := STATUS_SUCCESS;
        end
    else
        begin
            IoSkipCurrentIrpStackLocation(pIrp);
            Result := IoCallDriver(pNextDevice, pIrp);
        end;
end;
 
function AddDevice(pMyDriver : PDRIVER_OBJECT; pPhyDevice : PDEVICE_OBJECT) : NTSTATUS; stdcall;
var
    suDevName : UNICODE_STRING;
    suSymName : UNICODE_STRING;
    pMyDevice : PDEVICE_OBJECT;
     
begin
    RtlInitUnicodeString(@suDevName, DEV_NAME);
    RtlInitUnicodeString(@suSymName, SYM_NAME);
    IoCreateDevice(pMyDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pMyDevice);
    pNextDevice := IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice);
    pMyDevice^.Flags := pMyDevice^.Flags or DO_BUFFERED_IO;
    pMyDevice^.Flags := pMyDevice^.Flags and not DO_DEVICE_INITIALIZING;
    Result := IoCreateSymbolicLink(@suSymName, @suDevName);
end;
 
function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall;
var
    cc : ULONG;
    hThread : Handle;
    status : NTSTATUS;
 
begin
    pMyDriver^.MajorFunction[IRP_MJ_PNP] := @IrpPnp;
    pMyDriver^.DriverExtension^.AddDevice := @AddDevice;
    pMyDriver^.DriverUnload := @Unload;
 
    KeInitializeEvent(@myEvent, NotificationEvent, FALSE);
    for cc := 0 to (MAX_THREAD - 1) do
    begin
        status := PsCreateSystemThread(@hThread, THREAD_ALL_ACCESS, Nil, Handle(-1), Nil, MyThread, Pointer(cc));
        if NT_SUCCESS(status) then
        begin
            ZwClose(hThread);
        end;
    end;
    Result := STATUS_SUCCESS;
end;
end.

完成