參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk
main.pas
unit main; interface uses DDDK; const DEV_NAME = '\Device\MyDriver'; SYM_NAME = '\DosDevices\MyDriver'; IOCTL_TEST = (FILE_DEVICE_UNKNOWN shl 16) or ($800 shl 2) or (FILE_ANY_ACCESS shl 14) or (METHOD_BUFFERED); function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall; implementation var pNextDevice : PDEVICE_OBJECT; szBuf : array[0..255] of char; procedure Unload(pMyDriver : PDRIVER_OBJECT); stdcall; begin end; function IrpFile(pMyDevice : PDEVICE_OBJECT; pIrp : PIRP) : NTSTATUS; stdcall; var pStack: PIO_STACK_LOCATION; begin pStack := IoGetCurrentIrpStackLocation(pIrp); case pStack^.MajorFunction of IRP_MJ_CREATE: DbgPrint('IRP_MJ_CREATE', []); IRP_MJ_CLOSE: DbgPrint('IRP_MJ_CLOSE', []); end; Result := STATUS_SUCCESS; pIrp^.IoStatus.Status := Result; pIrp^.IoStatus.Information := 0; IoCompleteRequest(pIrp, IO_NO_INCREMENT); end; function IrpIOCTL(pMyDevice : PDeviceObject; pIrp : PIrp) : NTSTATUS; stdcall; var code : ULONG; pStack : PIO_STACK_LOCATION; begin pStack := IoGetCurrentIrpStackLocation(pIrp); code := pStack^.Parameters.DeviceIoControl.IoControlCode; case code of IOCTL_TEST: begin DbgPrint('IOCTL_TEST', []); end; end; Result := STATUS_SUCCESS; pIrp^.IoStatus.Information := 0; pIrp^.IoStatus.Status := Result; IoCompleteRequest(pIrp, IO_NO_INCREMENT); 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; begin pMyDriver^.MajorFunction[IRP_MJ_PNP] := @IrpPnp; pMyDriver^.MajorFunction[IRP_MJ_CREATE] := @IrpFile; pMyDriver^.MajorFunction[IRP_MJ_CLOSE] := @IrpFile; pMyDriver^.MajorFunction[IRP_MJ_DEVICE_CONTROL] := @IrpIOCTL; pMyDriver^.DriverExtension^.AddDevice := @AddDevice; pMyDriver^.DriverUnload := @Unload; Result := STATUS_SUCCESS; end; end.
app.pas
program main; {$APPTYPE CONSOLE} uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; const METHOD_BUFFERED = 0; METHOD_IN_DIRECT = 1; METHOD_OUT_DIRECT = 2; METHOD_NEITHER = 3; FILE_ANY_ACCESS = 0; FILE_DEVICE_UNKNOWN = $22; IOCTL_TEST = (FILE_DEVICE_UNKNOWN shl 16) or ($800 shl 2) or (FILE_ANY_ACCESS shl 14) or (METHOD_BUFFERED); IOCTL_SET_BUF = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($800 shl 2) or (METHOD_BUFFERED); IOCTL_GET_BUF = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($801 shl 2) or (METHOD_BUFFERED); IOCTL_SET_DIR = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($800 shl 2) or (METHOD_IN_DIRECT); IOCTL_GET_DIR = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($801 shl 2) or (METHOD_OUT_DIRECT); IOCTL_SET_NEI = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($800 shl 2) or (METHOD_NEITHER); IOCTL_GET_NEI = (FILE_DEVICE_UNKNOWN shl 16) or (FILE_ANY_ACCESS shl 14) or ($801 shl 2) or (METHOD_NEITHER); var hFile : DWORD; dwRet : DWORD; dwLen : DWORD; szBuffer : array[0..255] of char; begin hFile := CreateFile('\\.\MyDriver', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if paramCount() = 0 then begin DeviceIoControl(hFile, IOCTL_TEST, nil, 0, nil, 0, dwRet, nil); end else begin StrCopy(szBuffer, 'I am error'); dwLen := strlen(szBuffer); WriteLn(Output, Format('SET: %s, %d', [szBuffer, dwLen])); if paramStr(1) = 'BUF' then begin DeviceIoControl(hFile, IOCTL_SET_BUF, @szBuffer, dwLen, nil, 0, dwRet, nil); FillChar(szBuffer, sizeof(szBuffer), #0); DeviceIoControl(hFile, IOCTL_GET_BUF, nil, 0, @szBuffer, dwLen, dwRet, nil); end; if paramStr(1) = 'DIR' then begin DeviceIoControl(hFile, IOCTL_SET_DIR, @szBuffer, dwLen, nil, 0, dwRet, nil); FillChar(szBuffer, sizeof(szBuffer), #0); DeviceIoControl(hFile, IOCTL_GET_DIR, nil, 0, @szBuffer, dwLen, dwRet, nil); end; if paramStr(1) = 'NEI' then begin DeviceIoControl(hFile, IOCTL_SET_NEI, @szBuffer, dwLen, nil, 0, dwRet, nil); FillChar(szBuffer, sizeof(szBuffer), #0); DeviceIoControl(hFile, IOCTL_GET_NEI, nil, 0, @szBuffer, dwLen, dwRet, nil); end; WriteLn(Output, Format('GET: %s, %d', [szBuffer, dwRet])); end; CloseHandle(hFile); end.
完成