From 2b072b2758b997f2a77bb0701e87fd6eac38c067 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 3 Apr 2023 10:44:08 -0400 Subject: [PATCH] Web browsable export of source --- SourceExport/HWLDriverController/Settings | 43 ++ .../HWLDriverController/Sources/Form1.tbform | 467 +++++++++++++++ .../HWLDriverController/Sources/Form1.twin | 365 +++++++++++ SourceExport/TBHWldDrv/Settings | 28 + SourceExport/TBHWldDrv/Sources/modDriver.twin | 566 ++++++++++++++++++ 5 files changed, 1469 insertions(+) create mode 100644 SourceExport/HWLDriverController/Settings create mode 100644 SourceExport/HWLDriverController/Sources/Form1.tbform create mode 100644 SourceExport/HWLDriverController/Sources/Form1.twin create mode 100644 SourceExport/TBHWldDrv/Settings create mode 100644 SourceExport/TBHWldDrv/Sources/modDriver.twin diff --git a/SourceExport/HWLDriverController/Settings b/SourceExport/HWLDriverController/Settings new file mode 100644 index 0000000..6eed2ad --- /dev/null +++ b/SourceExport/HWLDriverController/Settings @@ -0,0 +1,43 @@ +{ + "configuration.inherits": "Defaults", + "project.buildPath": "${SourcePath}\\${Architecture}\\${ProjectName}.${FileExtension}", + "project.buildType": "Standard EXE", + "project.description": "HWLDriverController", + "project.dllRuntimeBinding": false, + "project.exportPathIsV2": true, + "project.id": "{85272AF9-9F96-40D2-829E-4A53C8E5CC6B}", + "project.name": "HWLDriverController", + "project.optionExplicit": true, + "project.references": [ + { + "id": "{F50B82D0-DCAB-43FE-9631-11959D4A4728}", + "isCompilerPackage": true, + "licence": "MIT", + "name": "[COMPILER PACKAGE] WinNativeForms Package", + "path32": "/Packages/WindowsControlsPackage", + "path64": "/Packages/WindowsControlsPackage", + "publishedDate": "09-FEB-2022", + "publishedTime": "15:48:54", + "publisher": "TWINBASIC-COMPILER", + "symbolId": "WindowsControlsPackage", + "versionBuild": 0, + "versionMajor": 0, + "versionMinor": 0, + "versionRevision": 24 + } + ], + "project.startupObject": "Form1", + "project.versionAutoIncrement": "Revision", + "project.versionBuild": 1, + "project.versionFileDescription": "Controls the twinBASIC HelloWorldDriver", + "project.versionLegalCopyright": "©2022", + "project.versionMajor": 1, + "project.versionMinor": 0, + "project.versionProductName": "HelloWorldDriverController", + "project.versionRevision": 11, + "project.warnings": { + "errors": [], + "ignored": [] + }, + "runtime.useUnicodeStandardLibrary": true +} \ No newline at end of file diff --git a/SourceExport/HWLDriverController/Sources/Form1.tbform b/SourceExport/HWLDriverController/Sources/Form1.tbform new file mode 100644 index 0000000..e2b496f --- /dev/null +++ b/SourceExport/HWLDriverController/Sources/Form1.tbform @@ -0,0 +1,467 @@ +[ + { + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "BorderStyle": "vbSizable", + "Caption": "HelloWorld twinBASIC Driver Controller", + "ControlBox": true, + "Enabled": true, + "ForeColor": -2147483630, + "FormDesignerId": "{B86F330F-0385-436D-92B3-76962B748687}", + "HasDC": true, + "Height": 316, + "KeyPreview": false, + "Left": 0, + "MaxButton": true, + "MinButton": true, + "MousePointer": "vbDefault", + "Moveable": true, + "Name": "Form1", + "ShowInTaskbar": true, + "StartupPosition": "vbStartUpScreen", + "Tag": null, + "Top": 0, + "Visible": true, + "Width": 604, + "WindowState": "vbNormal", + "__IDEOptions": { + "alignToGrid": false, + "gridHeight": 10, + "gridWidth": 10, + "lockedControls": [], + "showGrid": true + }, + "_children": [ + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Connect to driver", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 40, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command1", + "TabIndex": 2, + "TabStop": true, + "Tag": null, + "Top": 76, + "Visible": true, + "VisualStyles": true, + "Width": 126, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Load Driver", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 40, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command2", + "TabIndex": 1, + "TabStop": true, + "Tag": null, + "Top": 8, + "Visible": true, + "VisualStyles": true, + "Width": 126, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Send version command", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 40, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command3", + "TabIndex": 3, + "TabStop": true, + "Tag": null, + "Top": 120, + "Visible": true, + "VisualStyles": true, + "Width": 126, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Unload driver", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 40, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command4", + "TabIndex": 4, + "TabStop": true, + "Tag": null, + "Top": 164, + "Visible": true, + "VisualStyles": true, + "Width": 126, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Unload and delete", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 40, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command5", + "TabIndex": 5, + "TabStop": true, + "Tag": null, + "Top": 210, + "Visible": true, + "VisualStyles": true, + "Width": 126, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Exit", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 28, + "Left": 9, + "MousePointer": "vbDefault", + "Name": "Command6", + "TabIndex": 6, + "TabStop": true, + "Tag": null, + "Top": 276, + "Visible": true, + "VisualStyles": true, + "Width": 128, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483643, + "BorderStyle": "vbFixedSingleBorder", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "ForeColor": -2147483640, + "Height": 280, + "HideSelection": true, + "Left": 145, + "Locked": true, + "MaxLength": 0, + "MousePointer": "vbDefault", + "MultiLine": true, + "Name": "Text1", + "PasswordChar": null, + "ScrollBars": "vbBoth", + "TabIndex": 7, + "TabStop": true, + "Tag": null, + "Text": "", + "Top": 32, + "Visible": true, + "VisualStyles": true, + "Width": 454, + "_className": "TextBox", + "_clsid": "{33AD4EE0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Alignment": "tbLeftJustify", + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Set system start", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "ForeColor": -2147483630, + "Height": 20, + "Left": 39, + "MousePointer": "vbDefault", + "Name": "Check1", + "TabIndex": 8, + "TabStop": true, + "Tag": null, + "Top": 50, + "Value": "vbUnchecked", + "Visible": true, + "VisualStyles": true, + "Width": 108, + "_className": "CheckBox", + "_clsid": "{33AD4EF8-6699-11CF-B70C-00AA0060D393}" + }, + { + "BorderColor": -2147483640, + "BorderStyle": "vbBSSolid", + "BorderWidth": 1, + "DrawMode": "vbCopyPen", + "Name": "Line1", + "Tag": null, + "Visible": true, + "X1": 20, + "X2": 20, + "Y1": 48, + "Y2": 59.666656494141, + "_className": "Line", + "_clsid": "{33AD4F88-6699-11CF-B70C-00AA0060D393}", + "_paintedByParent": true + }, + { + "BorderColor": -2147483640, + "BorderStyle": "vbBSSolid", + "BorderWidth": 1, + "DrawMode": "vbCopyPen", + "Name": "Line2", + "Tag": null, + "Visible": true, + "X1": 37, + "X2": 20, + "Y1": 60, + "Y2": 60, + "_className": "Line", + "_clsid": "{33AD4F88-6699-11CF-B70C-00AA0060D393}", + "_paintedByParent": true + }, + { + "Alignment": "vbLeftJustify", + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "BorderStyle": "vbNoBorder", + "Caption": "Driver name:", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "ForeColor": -2147483630, + "Height": 18, + "Left": 151, + "MousePointer": "vbDefault", + "Name": "Label1", + "TabIndex": 9, + "Tag": null, + "Top": 10, + "UseMnemonic": true, + "Visible": true, + "VisualStyles": true, + "Width": 74, + "_className": "Label", + "_clsid": "{33AD4ED8-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483643, + "BorderStyle": "vbFixedSingleBorder", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "ForeColor": -2147483640, + "Height": 18, + "HideSelection": true, + "Left": 217, + "Locked": false, + "MaxLength": 0, + "MousePointer": "vbDefault", + "MultiLine": false, + "Name": "Text2", + "PasswordChar": null, + "ScrollBars": "vbSBNone", + "TabIndex": 10, + "TabStop": true, + "Tag": null, + "Text": "TBHWldDrv", + "Top": 8, + "Visible": true, + "VisualStyles": true, + "Width": 78, + "_className": "TextBox", + "_clsid": "{33AD4EE0-6699-11CF-B70C-00AA0060D393}" + }, + { + "Anchors": { + "Bottom": false, + "Left": true, + "Right": false, + "Top": true, + "_className": "Anchors", + "_clsid": "{8524D4B5-72A9-40A9-A189-29E7905C40CA}" + }, + "Appearance": "vbAppear3d", + "BackColor": -2147483633, + "Caption": "Set", + "Dock": "vbDockNone", + "Enabled": true, + "FontBold": false, + "FontItalic": false, + "FontName": "Segoe UI", + "FontSize": 8, + "FontStrikethru": false, + "FontUnderline": false, + "Height": 22, + "Left": 297, + "MousePointer": "vbDefault", + "Name": "Command7", + "TabIndex": 11, + "TabStop": true, + "Tag": null, + "Top": 6, + "Visible": true, + "VisualStyles": true, + "Width": 70, + "_className": "CommandButton", + "_clsid": "{33AD4EF0-6699-11CF-B70C-00AA0060D393}" + } + ], + "_className": "Form", + "_clsid": "{33AD4F38-6699-11CF-B70C-00AA0060D393}" + } +] \ No newline at end of file diff --git a/SourceExport/HWLDriverController/Sources/Form1.twin b/SourceExport/HWLDriverController/Sources/Form1.twin new file mode 100644 index 0000000..69ff90a --- /dev/null +++ b/SourceExport/HWLDriverController/Sources/Form1.twin @@ -0,0 +1,365 @@ +[ Description ("") ] +[ FormDesignerId ("B86F330F-0385-436D-92B3-76962B748687") ] +[ PredeclaredId ] +Class Form1 +Option Explicit +Private Type HelloWorldVersion + Major As Integer + Minor As Integer + Build As Integer + Revision As Integer +End Type + +Private Const METHOD_BUFFERED = &H0 +Private Const FILE_ACCESS_ANY = &H0 +Private Const FILE_DEVICE_UNKNOWN As Long = &H22 +Private Type SERVICE_STATUS + dwServiceType As Long + dwCurrentState As Long + dwControlsAccepted As Long + dwWin32ExitCode As Long + dwServiceSpecificExitCode As Long + dwCheckPoint As Long + dwWaitHint As Long +End Type + +Private Const SB_BOTTOM = 7 +Private Const EM_SCROLL = &HB5 + +Private Declare PtrSafe Function ControlService Lib "advapi32.dll" ( _ + ByVal hService As LongPtr, _ + ByVal dwControl As Long, _ + ByRef lpServiceStatus As SERVICE_STATUS) As Long +Private Declare PtrSafe Function OpenSCManager Lib "advapi32.dll" _ + Alias "OpenSCManagerW" ( _ + ByVal lpMachineName As LongPtr, _ + ByVal lpDatabaseName As LongPtr, _ + ByVal dwDesiredAccess As Long) As LongPtr +Private Declare PtrSafe Function CloseServiceHandle Lib "advapi32.dll" ( _ + ByVal hSCObject As LongPtr) As Long +Private Declare PtrSafe Function OpenService Lib "advapi32.dll" _ + Alias "OpenServiceW" ( _ + ByVal hSCManager As LongPtr, _ + ByVal lpServiceName As LongPtr, _ + ByVal dwDesiredAccess As Long) As LongPtr +Private Declare PtrSafe Function CreateService Lib "advapi32.dll" _ + Alias "CreateServiceW" ( _ + ByVal hSCManager As LongPtr, _ + ByVal lpServiceName As LongPtr, _ + ByVal lpDisplayName As LongPtr, _ + ByVal dwDesiredAccess As Long, _ + ByVal dwServiceType As Long, _ + ByVal dwStartType As Long, _ + ByVal dwErrorControl As Long, _ + ByVal lpBinaryPathName As LongPtr, _ + ByVal lpLoadOrderGroup As LongPtr, _ + ByVal lpdwTagId As LongPtr, _ + ByVal lpDependencies As LongPtr, _ + ByVal lpServiceStartName As LongPtr, _ + ByVal lpPassword As LongPtr) As LongPtr +Private Declare PtrSafe Function StartService Lib "advapi32.dll" _ + Alias "StartServiceW" ( _ + ByVal hService As LongPtr, _ + ByVal dwNumServiceArgs As Long, _ + ByVal lpServiceArgVectors As LongPtr) As Long +Private Declare PtrSafe Function DeleteService Lib "advapi32.dll" ( _ + ByVal hService As LongPtr) As Long +Private Declare PtrSafe Function CreateFile Lib "kernel32" _ + Alias "CreateFileW" ( _ + ByVal lpFileName As LongPtr, _ + ByVal dwDesiredAccess As Long, _ + ByVal dwShareMode As Long, _ + ByRef lpSecurityAttributes As Any, _ + ByVal dwCreationDisposition As Long, _ + ByVal dwFlagsAndAttributes As Long, _ + ByVal hTemplateFile As LongPtr) As LongPtr +Private Declare PtrSafe Function CloseHandle Lib "kernel32" ( _ + ByVal hObject As LongPtr) As Long +Private Declare PtrSafe Function DeviceIoControl Lib "kernel32" ( _ + ByVal hDevice As LongPtr, _ + ByVal dwIoControlCode As Long, _ + ByRef lpInBuffer As Any, _ + ByVal nInBufferSize As Long, _ + ByRef lpOutBuffer As Any, _ + ByVal nOutBufferSize As Long, _ + ByRef lpBytesReturned As Long, _ + ByRef lpOverlapped As Any) As Long +Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr +Private DeclareWide PtrSafe Function PathFileExistsW Lib "shlwapi" (ByVal lpszPath As String) As Long + +Private Const ERROR_SERVICE_ALREADY_RUNNING As Long = 1056& +Private Const ERROR_SERVICE_EXISTS As Long = 1073& +Private Const SERVICE_CONTROL_STOP As Long = &H1 +Private Const SC_MANAGER_ALL_ACCESS As Long = &HF003F +Private Const SERVICE_ALL_ACCESS As Long = &HF01FF +Private Const SERVICE_KERNEL_DRIVER As Long = &H1 +Private Const SERVICE_SYSTEM_START As Long = &H1 +Private Const SERVICE_DEMAND_START As Long = &H3 +Private Const SERVICE_ERROR_NORMAL As Long = &H1 +Private Const GENERIC_READ As Long = &H80000000 +Private Const GENERIC_WRITE As Long = &H40000000 +Private Const FILE_SHARE_READ As Long = &H1 +Private Const FILE_SHARE_WRITE As Long = &H2 +Private Const OPEN_EXISTING As Long = 3 +Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80 +Private Const INVALID_HANDLE_VALUE As Long = -1 +Private Const STATUS_SUCCESS = 0& +Private Const STATUS_INVALID_PARAMETER = &HC000000D +Private Const STATUS_INVALID_DEVICE_REQUEST = &HC0000010 + +Private IOCTL_HWRLD_VERSION As Long +Private DriverName As String + +Private DriverFile As String +Private hMgr As LongPtr +Private hSrv As LongPtr +Private hDev As LongPtr + +Private Sub InitFuncs() +IOCTL_HWRLD_VERSION = CTL_CODE(FILE_DEVICE_UNKNOWN, &h801&, METHOD_BUFFERED, FILE_ACCESS_ANY) +End Sub +Private Function NT_SUCCESS( _ + ByVal Status As Long) As Boolean + NT_SUCCESS = Status >= STATUS_SUCCESS +End Function +Private Function CTL_CODE(ByVal DeviceType As Long, ByVal lFunction As Long, ByVal Method As Long, ByVal Access As Long) As Long + CTL_CODE = ((DeviceType) << 16) Or ((Access) << 14) Or ((lFunction) << 2) Or (Method) +End Function + +Private Sub Command2_Click() Handles Command2.Click + If hDev Then + AppendLog "Already connected to driver." + Exit Sub + End If + ' // Open SC manager database + hMgr = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS) + + If hMgr Then + AppendLog "Loaded service manager..." + Else + AppendLog "Unable to load SC manager, err=0x" & Hex$(Err.LastDllError) + Exit Sub + End If + + ' // Create servise + hSrv = CreateService(hMgr, StrPtr(DriverName), StrPtr(DriverName), SERVICE_ALL_ACCESS, _ + SERVICE_KERNEL_DRIVER, IIf(Check1.Value = vbChecked, SERVICE_SYSTEM_START, SERVICE_DEMAND_START), SERVICE_ERROR_NORMAL, StrPtr(DriverFile), _ + 0, 0, 0, 0, 0) + + ' // If service already has beend launched + If hSrv = 0 And Err.LastDllError = ERROR_SERVICE_EXISTS Then + ' // Open existing service + AppendLog "Service already exists, opening..." + hSrv = OpenService(hMgr, StrPtr(DriverName), SERVICE_ALL_ACCESS) + End If + + If hSrv Then + AppendLog "Successfully created service..." + Else + AppendLog "Error: Unable to create or open service, err=0x" & Hex$(Err.LastDllError) + Exit Sub + End If + + ' // Launch driver + If StartService(hSrv, 0, 0) Then + AppendLog "Successfully started driver!" + Else + If Err.LastDllError Then + If Err.LastDllError = ERROR_SERVICE_ALREADY_RUNNING Then + AppendLog "Service was already running." + Else + AppendLog "Error: Unable to start service, err=0x" & Hex$(Err.LastDllError) + Exit Sub + End If + End If + End If + + ' // Connect to driver + hDev = CreateFile(StrPtr("\\.\" & DriverName), GENERIC_READ Or FILE_SHARE_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, _ + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) + + If hDev = INVALID_HANDLE_VALUE Then + AppendLog "Error: Unable to connect to driver, err=0x" & Err.LastDllError + Else + AppendLog "Successfully connected to driver!" + End If +End Sub + + +Public Sub AppendLog(sMsg As String) +sMsg = "[" & Format$(Now, "Hh:nn:Ss") & "] " & sMsg +Text1.Text = Text1.Text & sMsg & vbCrLf +SendMessage Text1.hWnd, EM_SCROLL, SB_BOTTOM, ByVal 0& +End Sub + +Private Sub Command5_Click() Handles Command5.Click + Dim Status As SERVICE_STATUS + + ' // Disconnect driver + If hDev Then + CloseHandle hDev + hDev = 0 + AppendLog "Disconnected from driver." + End If + + ' // Stop driver + If hSrv Then + ControlService hSrv, SERVICE_CONTROL_STOP, Status + DeleteService hSrv + AppendLog "Stopped driver and deleted service." + End If + + ' // Close handles + If hSrv Then CloseServiceHandle hSrv + If hMgr Then CloseServiceHandle hMgr + hSrv = 0: hMgr = 0 + AppendLog "Closed service handles." +End Sub + +Private Sub Command6_Click() Handles Command6.Click + Unload Me +End Sub + +Private Sub Form_Load() Handles Form.Load + InitFuncs + ' // Get driver file name + DriverName = "TBHWldDrv" + DriverFile = App.Path & "\" & DriverName & ".sys" + + If PathFileExistsW(DriverFile) Then + AppendLog "Driver file found. If installed in boot, click Connect." & vbCrLf & "Otherwise, click Load Driver." + Else + AppendLog "Error: Can't find driver. TBHWldDrv.sys must be placed in the same folder as this program." + AppendLog "Please put the driver in the same folder and restart this controller." + AppendLog "Or, if you're loading a different driver, enter it's name and click 'Set'" + Command1.Enabled = False + Command2.Enabled = False + Command3.Enabled = False + Command4.Enabled = False + Command5.Enabled = False + End If +End Sub + +Private Sub Form_Unload(Cancel As Integer) Handles Form.Unload + Dim r As VbMsgBoxResult + Dim status As SERVICE_STATUS + If hSrv Then + r = MsgBox("Delete driver service?", vbInformation + vbYesNoCancel, "Driver Controller") + End If + If r = vbCancel Then Cancel = 1: Exit Sub + ' // Disconnect driver + If hDev Then CloseHandle hDev + If hSrv Then + If r = vbYes Then + ControlService hSrv, SERVICE_CONTROL_STOP, status + DeleteService hSrv + End If + End If + ' // Close handles + If hSrv Then CloseServiceHandle hSrv + If hMgr Then CloseServiceHandle hMgr +End Sub + +Private Sub Form_Resize() Handles Form.Resize + Dim cx As Long, cy As Long + If Form1.Width < 300 Then + cx = 300 + Else + cx = Form1.Width - 150 + End If + If Form1.Height < 315 Then + cy = 315 + Else + cy = Form1.Height - 14 + End If + + Text1.Width = cx + Text1.Height = cy +End Sub + +Private Sub Command1_Click() Handles Command1.Click + ' // Connect to driver + hDev = CreateFile(StrPtr("\\.\" & DriverName), GENERIC_READ Or FILE_SHARE_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, _ + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) + + If hDev = INVALID_HANDLE_VALUE Then + AppendLog "Error: Unable to connect to driver, not loaded? err=0x" & Err.LastDllError & vbCrLf & _ + "If you didn't install the driver to load on startup, you must click 'Load Driver' instead." + Else + AppendLog "Successfully connected to driver!" + End If +End Sub + +Private Sub Command3_Click() Handles Command3.Click + If hDev = 0 Then + AppendLog "GetVersion Error: Not connected to driver." + Exit Sub + End If + + Dim tVer As HelloWorldVersion + Dim lVerify As Long + Dim cbRet As Long + Dim result As Long + AppendLog "Sending IOCTL_HWRLD_VERSION to driver..." + result = DeviceIoControl(hDev, IOCTL_HWRLD_VERSION, lVerify, 4&, tVer, LenB(tVer), cbRet, ByVal 0&) + AppendLog "Result: ret=0x" & Hex$(result) & ",cbRead=" & cbRet & vbCrLf & "Version (Expecting 1.2.3.4)=" & tVer.Major & "." & tVer.Minor & "." & tVer.Build & "." & tVer.Revision + If result = 0 Then + AppendLog "Did not receive success result, lastErr=0x" & Err.LastDllError & ",hDev=" & hDev + End If +End Sub + +Private Sub Command4_Click() Handles Command4.Click + If hDev Then CloseHandle hDev + If hMgr = 0 Then + hMgr = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS) + + If hMgr Then + AppendLog "Loaded service manager..." + Else + AppendLog "Unable to establish connection with SC manager, err=0x" & Hex$(Err.LastDllError) + Exit Sub + End If + End If + If hSrv = 0 Then + hSrv = OpenService(hMgr, StrPtr(DriverName), SERVICE_ALL_ACCESS) + If hSrv Then + AppendLog "Opened driver service..." + Else + AppendLog "Error: Unable to open service, err=0x" & Hex$(Err.LastDllError) + Exit Sub + End If + End If + Dim status As SERVICE_STATUS + Dim lRet As Long + lRet = ControlService(hSrv, SERVICE_CONTROL_STOP, status) + If lRet = 0 Then + AppendLog "Failed to stop service, Err=0x" & Hex$(Err.LastDllError) & ", Service status=" & status.dwCurrentState + Else + AppendLog "Successfully stopped driver service." + End If +End Sub + +Private Sub Command7_Click() Handles Command7.Click + DriverName = Text2.Text + DriverFile = App.Path & "\" & DriverName & ".sys" + + If PathFileExistsW(DriverFile) Then + AppendLog "Driver file found. If installed in boot, click Connect." & vbCrLf & "Otherwise, click Load Driver." + Command1.Enabled = True + Command2.Enabled = True + Command3.Enabled = True + Command4.Enabled = True + Command5.Enabled = True + Else + AppendLog "Error: Can't find driver. " & DriverName & ".sys must be placed in the same folder as this program." + AppendLog "Please put the driver in the same folder and restart this controller." + Command1.Enabled = False + Command2.Enabled = False + Command3.Enabled = False + Command4.Enabled = False + Command5.Enabled = False + End If +End Sub + + +End Class \ No newline at end of file diff --git a/SourceExport/TBHWldDrv/Settings b/SourceExport/TBHWldDrv/Settings new file mode 100644 index 0000000..01d967d --- /dev/null +++ b/SourceExport/TBHWldDrv/Settings @@ -0,0 +1,28 @@ +{ + "configuration.inherits": "Defaults", + "project.autoPrettify": true, + "project.buildPath": "${SourcePath}\\${Architecture}\\${ProjectName}.${FileExtension}", + "project.buildType": "Standard EXE", + "project.description": "TBHWldDrv", + "project.dllRuntimeBinding": false, + "project.exportPathIsV2": true, + "project.id": "{46CD6E01-8B39-402B-B73A-28522F135F00}", + "project.isNativeSubsystem": true, + "project.largeAddressAware": true, + "project.name": "TBHWldDrv", + "project.optionExplicit": true, + "project.overrideEntryPoint": "DriverEntry", + "project.references": [], + "project.versionAutoIncrement": "Revision", + "project.versionBuild": 1, + "project.versionFileDescription": "twinBASIC Driver Demo - HelloWorldDriver", + "project.versionMajor": 1, + "project.versionMinor": 0, + "project.versionProductName": "TBHWldDrv", + "project.versionRevision": 18, + "project.warnings": { + "errors": [], + "ignored": [] + }, + "runtime.useUnicodeStandardLibrary": true +} \ No newline at end of file diff --git a/SourceExport/TBHWldDrv/Sources/modDriver.twin b/SourceExport/TBHWldDrv/Sources/modDriver.twin new file mode 100644 index 0000000..255085a --- /dev/null +++ b/SourceExport/TBHWldDrv/Sources/modDriver.twin @@ -0,0 +1,566 @@ +Module modDriver +Option Explicit +'****************************************************************************** +'twinBASIC Driver Demo: Hello World Driver v1.2.3.4 +'Author: Jon Johnson (fafalone) +' +'This is a demonstration of using tB to create a kernel-mode driver. +'All it does is return it's version information. +' +'Definitions mainly from The trick's TrickMemReader VB6 driver, as well the +'method for initializing the Driver Name and Link Name. I converted those +'to x64-compatible and added some of my own. +'https://www.vbforums.com/showthread.php?788179-VB6-Kernel-mode-driver +' +'Structure and functionality are heavily based on Geoff Chappell's self-sign +'driver example, as I followed (and recommend you follow) that method to test +'self-signing to run on x64 platforms. +'https://www.geoffchappell.com/notes/windows/license/customkernelsigners.htm +' +'You can also disable signature verification by rebooting and using the +'Advanced Boot Menu; or enable testsigning mode. +'****************************************************************************** + +'Project definitions: +'A custom version structure, which is what we'll be passing to our controller. +Private Type HelloWorldVersion + Major As Integer + Minor As Integer + Build As Integer + Revision As integer +End Type + +'We can't use String variables in kernel mode; VB/tB manage them with APIs behind +'the scenes, and we can't call any APIs besides ones in ntoskrnl.exe +'So set up arrays to represent them. We have one for Unicode; 2 bytes per char, +'and one ANSI one, for DbgPrint which uses 1-byte ANSI chars. +Private Type BinaryString + D(255) As Integer +End Type +Private Type DebugCSTR + aCh(0 To 49) As Byte +End Type + +'We define custom IOCTL_ commands to communicate with user mode; these are set +'with the CTL_CODE macro, defined in code. +Private IOCTL_HWRLD_VERSION As Long + +'These represent our driver: it's name, a name for the link to user mode, and +'the device object. +Private DeviceName As UNICODE_STRING ' // Device name unicode string +Private DeviceLink As UNICODE_STRING ' // Device link unicode string +Private strName As BinaryString ' // Device name string +Private strLink As BinaryString ' // Device link string + +Private Device As DEVICE_OBJECT ' // Device object + +'Here's our debug strings for DbgPrint +Private dbgsEntry As DebugCSTR 'Entry point success +Private dbgsDevIoEntry As DebugCSTR 'DeviceIoControl received +Private dbgsStackOk As DebugCSTR +Private dbgsStackFail As DebugCSTR +Private dbgsCmdOk As DebugCSTR +Private dbgsCmdFailCode As DebugCSTR +Private dbgsCmdFailPtr As DebugCSTR +Private dbgsNoBuffer As DebugCSTR +Private dbgsBadSize As DebugCSTR +Private dbgsNoVal As DebugCSTR +Private dbgsCopyOut As DebugCSTR + + +'API Definitions: +Private Type LARGE_INTEGER + lowPart As Long + highPart As Long +End Type + +Private Type UNICODE_STRING + Length As Integer + MaximumLength As Integer + lpBuffer As LongPtr +End Type + +Private Type LIST_ENTRY + Flink As LongPtr + Blink As LongPtr +End Type + +Private Type KDEVICE_QUEUE + Type As Integer + Size As Integer + DeviceListHead As LIST_ENTRY + Lock As LongPtr + Busy As Long +End Type + +Private Type KDPC + Type As Byte + Importance As Byte + Number As Integer + DpcListEntry As LongPtr + ProcessorHistory As LongPtr + DeferredRoutine As LongPtr + DeferredContext As LongPtr + SystemArgument1 As LongPtr + SystemArgument2 As LongPtr + DpcData As LongPtr +End Type + +Private Type DISPATCHER_HEADER + Lock As Long + SignalState As Long + WaitListHead As LIST_ENTRY +End Type + +Private Type KEVENT + Header As DISPATCHER_HEADER +End Type + +Private Type IO_STATUS_BLOCK + StatusPointer As LongPtr + Information As LongPtr +End Type + +Private Type IRP + Type As Integer + Size As Integer '2 + MdlAddress As LongPtr '8 + Flags As Long '16 + AssociatedIrp As LongPtr '24 + ThreadListEntry As LIST_ENTRY '32 + IoStatus As IO_STATUS_BLOCK '48 + RequestorMode As Byte '64 + PendingReturned As Byte + StackCount As Byte + CurrentLocation As Byte + Cancel As Byte + CancelIrql As Byte + ApcEnvironment As Byte + AllocationFlags As Byte + UserIosb As LongPtr '72 + UserEvent As LongPtr '80 + #If Win64 Then + Overlay(15) As Byte '88 + #Else + Overlay(7) As Byte + #End If + CancelRoutine As LongPtr '104 + UserBuffer As LongPtr '112 + 'Tail + DriverContext(3) As LongPtr '120 + Thread As LongPtr '152 + AuxiliaryBuffer As LongPtr '160 + ListEntry As LIST_ENTRY '168 + CurrentStackLocation As LongPtr '184 + OriginalFileObject As LongPtr '192 + Pad As LongLong 'Tail union alternate _KAPC is 8 bytes larger on both x86 and x64 +End Type 'Expected size (x64=0xD0) (x86=0x70) + +'One must be *incredibly* careful about alignments. Because while unexpected issues are rare with 32bit, +'64bit will encounter a lot more issues. For instance with the below, tB inserts 4 bytes behind the +'scenes so in IO_STACK_LOCATION, the first member of the union struct we're using is at 0x8; not 0x4. +'Always confirm final sizes (LenB *ONLY*! Len does not include align pads) with the following: +'https://www.vergiliusproject.com/kernels + +Private Type DEVICEIOCTL 'Comments: x64 alignments + OutputBufferLength As Long '0x8 + #If Win64 Then + zPtrAlign2 As Long + #End If + InputBufferLength As Long '0x10 + #If Win64 Then + zPtrAlign3 As Long + #End If + IoControlCode As Long '0x18 + Type3InputBuffer As LongPtr '0x20 +End Type + +Private Type IO_STACK_LOCATION + MajorFunction As Byte '0x0 + MinorFunction As Byte '0x1 + Flags As Byte '0x2 + Control As Byte '0x3 + 'There's a union here; the DeviceIoControl + 'struct is what we're interested in right now. + 'You may need multiple versions of this for other + 'uses until there's union support. + DeviceIoControl As DEVICEIOCTL '0x8 + DeviceObject As LongPtr '0x28 + FileObject As LongPtr '0x30 + CompletionRoutine As LongPtr '0x38 + Context As LongPtr '0x40 +End Type 'Expected size (x64) = 0x48 + +Private Type DRIVER_OBJECT + Type As Integer + Size As Integer + DeviceObject As LongPtr + Flags As Long + DriverStart As LongPtr + DriverSize As Long + DriverSection As LongPtr + DriverExtension As LongPtr + DriverName As UNICODE_STRING + HardwareDatabase As LongPtr + FastIoDispatch As LongPtr + DriverInit As LongPtr + DriverStartIo As LongPtr + DriverUnload As LongPtr + MajorFunction(IRP_MJ_MAXIMUM_FUNCTION) As LongPtr +End Type 'Expected size x64=0x150, x86=0xa8 + +Private Type DEVICE_OBJECT + Type As Integer + Size As Integer + ReferenceCount As Long + DriverObject As LongPtr + NextDevice As LongPtr + AttachedDevice As LongPtr + CurrentIrp As LongPtr + Timer As LongPtr + Flags As Long + Characteristics As Long + Vpb As LongPtr + DeviceExtension As LongPtr + DeviceType As Long + StackSize As Byte + #If Win64 Then + Queue(71) As Byte 'This includes alignment padding, so be careful if copying out. + #Else + Queue(39) As Byte + #End If + AlignRequirement As Long + DeviceQueue As KDEVICE_QUEUE + Dpc As KDPC + ActiveThreadCount As Long + SecurityDescriptor As LongPtr + DeviceLock As KEVENT + SectorSize As Integer + Spare1 As Integer + DeviceObjExtension As LongPtr + Reserved As LongPtr +End Type 'Expected size x64=0x150, x86=0xb8 + +Private Type FILE_OBJECT + Type As Integer + Size As Integer + DeviceObject As LongPtr + Vpb As LongPtr + FsContext As LongPtr + FsContext2 As LongPtr + SectionObjectPointer As LongPtr + PrivateCacheMap As LongPtr + FinalStatus As Long + RelatedFileObject As LongPtr + LockOperation As Byte + DeletePending As Byte + ReadAccess As Byte + WriteAccess As Byte + DeleteAccess As Byte + SharedRead As Byte + SharedWrite As Byte + SharedDelete As Byte + Flags As Long + FileName As UNICODE_STRING + CurrentByteOffset As LARGE_INTEGER + Waiters As Long + Busy As Long + LastLock As LongPtr + Lock As KEVENT + Event As KEVENT + CompletionContext As LongPtr + IrpListLock As LongPtr + IrpList As LIST_ENTRY + FileObjectExtension As LongPtr +End Type 'Expected size x64=0xD8, x86=0x80 + +Private Const FILE_DEVICE_UNKNOWN As Long = &H22 +Private Const IO_NO_INCREMENT As Long = &H0 + +Private Const IRP_MJ_CREATE = &H00 +Private Const IRP_MJ_CREATE_NAMED_PIPE = &H01 +Private Const IRP_MJ_CLOSE = &H02 +Private Const IRP_MJ_READ = &H03 +Private Const IRP_MJ_WRITE = &H04 +Private Const IRP_MJ_QUERY_INFORMATION = &H05 +Private Const IRP_MJ_SET_INFORMATION = &H06 +Private Const IRP_MJ_QUERY_EA = &H07 +Private Const IRP_MJ_SET_EA = &H08 +Private Const IRP_MJ_FLUSH_BUFFERS = &H09 +Private Const IRP_MJ_QUERY_VOLUME_INFORMATION = &H0a +Private Const IRP_MJ_SET_VOLUME_INFORMATION = &H0b +Private Const IRP_MJ_DIRECTORY_CONTROL = &H0c +Private Const IRP_MJ_FILE_SYSTEM_CONTROL = &H0d +Private Const IRP_MJ_DEVICE_CONTROL = &H0e +Private Const IRP_MJ_INTERNAL_DEVICE_CONTROL = &H0f +Private Const IRP_MJ_SHUTDOWN = &H10 +Private Const IRP_MJ_LOCK_CONTROL = &H11 +Private Const IRP_MJ_CLEANUP = &H12 +Private Const IRP_MJ_CREATE_MAILSLOT = &H13 +Private Const IRP_MJ_QUERY_SECURITY = &H14 +Private Const IRP_MJ_SET_SECURITY = &H15 +Private Const IRP_MJ_POWER = &H16 +Private Const IRP_MJ_SYSTEM_CONTROL = &H17 +Private Const IRP_MJ_DEVICE_CHANGE = &H18 +Private Const IRP_MJ_QUERY_QUOTA = &H19 +Private Const IRP_MJ_SET_QUOTA = &H1A +Private Const IRP_MJ_PNP = &H1B + +Private Const IRP_MJ_MAXIMUM_FUNCTION As Long = &H1B + +Private Const STATUS_SUCCESS = 0& +Private Const STATUS_INVALID_PARAMETER = &HC000000D +Private Const STATUS_INVALID_DEVICE_REQUEST = &HC0000010 +Private Const STATUS_BUFFER_ALL_ZEROS = &H117 +Private Const STATUS_INVALID_BUFFER_SIZE = &HC0000206 + +Private Const METHOD_BUFFERED = &H0 +Private Const FILE_ACCESS_ANY = &H0 + + +'Drivers can only import from ntoskrnl.exe. You need to be careful which language features you use; strings and arrays besides 1-D inside UDTs +'utilize APIs behind the scenes from other libraries (currently, I opened a feature request for non-SAFEARRAY arrays), and cannot be used in drivers. +[ UseGetLastError (False) ] +Private Declare PtrSafe Function DbgPrint CDecl Lib "ntoskrnl.exe" (ByVal Format As LongPtr /* Currently unsupported in kernel mode: , ByVal ParamArray Args As Any()*/) As Long +Private Declare PtrSafe Function IoCreateDevice Lib "ntoskrnl.exe" (DriverObject As DRIVER_OBJECT, _ + ByVal DeviceExtensionSize As Long, _ + DeviceName As UNICODE_STRING, _ + ByVal DeviceType As Long, _ + ByVal DeviceCharacteristics As Long, _ + ByVal Exclusive As Long, _ + DeviceObject As DEVICE_OBJECT) As Long + +Private Declare PtrSafe Sub IoCompleteRequest Lib "ntoskrnl.exe" (pIrp As IRP, ByVal PriorityBoost As Byte) +Private Declare PtrSafe Function IoCreateSymbolicLink Lib "ntoskrnl.exe" (SymbolicLinkName As UNICODE_STRING, DeviceName As UNICODE_STRING) As Long +Private Declare PtrSafe Function IoDeleteSymbolicLink Lib "ntoskrnl.exe" (SymbolicLinkName As UNICODE_STRING) As Long +Private Declare PtrSafe Sub IoDeleteDevice Lib "ntoskrnl.exe" (DeviceObject As Any) +Private Declare PtrSafe Sub RtlInitUnicodeString Lib "ntoskrnl.exe" (DestinationString As UNICODE_STRING, SourceString As Any) +Private Declare PtrSafe Sub CopyMemory Lib "ntoskrnl.exe" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr) + +Private Sub InitFuncs() + IOCTL_HWRLD_VERSION = CTL_CODE(FILE_DEVICE_UNKNOWN, &h801&, METHOD_BUFFERED, FILE_ACCESS_ANY) +End Sub + +Private Function NT_SUCCESS(ByVal Status As Long) As Boolean + NT_SUCCESS = Status >= STATUS_SUCCESS +End Function + +Private Function CTL_CODE(ByVal DeviceType As Long, ByVal lFunction As Long, ByVal Method As Long, ByVal Access As Long) As Long + CTL_CODE = ((DeviceType) << 16) Or ((Access) << 14) Or ((lFunction) << 2) Or (Method) +End Function + +Private Function IoGetCurrentIrpStackLocation(ByRef pIrp As IRP) As LongPtr + IoGetCurrentIrpStackLocation = pIrp.CurrentStackLocation +End Function + +Public Function DriverEntry(ByRef DriverObject As DRIVER_OBJECT, ByRef RegistryPath As UNICODE_STRING) As Long + InitDebugStrings + DbgPrint VarPtr(dbgsEntry) + InitUnicodeStrings + InitFuncs + + Dim ntStatus As Long + + ntStatus = IoCreateDevice(DriverObject, 0&, DeviceName, FILE_DEVICE_UNKNOWN, 0&, False, Device) + If NT_SUCCESS(ntStatus) Then + ntStatus = IoCreateSymbolicLink(DeviceLink, DeviceName) + If Not NT_SUCCESS(ntStatus) Then + IoDeleteDevice Device + DriverEntry = ntStatus + Exit Function + End If + + Dim i As Long + For i = 0 To IRP_MJ_MAXIMUM_FUNCTION + DriverObject.MajorFunction(i) = AddressOf OnOther + Next + DriverObject.MajorFunction(IRP_MJ_CREATE) = AddressOf OnCreate + DriverObject.MajorFunction(IRP_MJ_CLOSE) = AddressOf OnClose + DriverObject.MajorFunction(IRP_MJ_DEVICE_CONTROL) = AddressOf OnDeviceControl + + DriverObject.DriverUnload = AddressOf OnUnload + End If + + DriverEntry = ntStatus +End Function + +Public Function OnCreate(ByRef DriverObject As DRIVER_OBJECT, ByRef pIrp As IRP) As Long +'This seems so simple; I checked the alignments and everything is ok; but it's +'failing. So I'm disabling the check for now. Will look at for future version. +'From Geoff Chappell: + /* As explicitly recommended in the DDK documentation (but not done in + all of Microsoft's own drivers), check that the caller does indeed + mean to open a device. */ +' Dim lpStack As LongPtr +' Dim ioStack As IO_STACK_LOCATION +' Dim tFO As FILE_OBJECT +' lpStack = IoGetCurrentIrpStackLocation(pIrp) +' If lpStack Then +' CopyMemory ioStack, ByVal lpStack, LenB(ioStack) +' If ioStack.FileObject Then +' CopyMemory tFO, ByVal ioStack.FileObject, LenB(tFO) + Dim ntStatus As Long + ' If tFO.FileName.Length Then + ntStatus = STATUS_SUCCESS + ' Else + ' ntStatus = STATUS_INVALID_PARAMETER + ' End If + pIrp.IoStatus.Information = 0 + pIrp.IoStatus.StatusPointer = ntStatus + IoCompleteRequest pIrp, IO_NO_INCREMENT + OnCreate = ntStatus +' End If +' End If +End Function + +Public Function OnClose(ByRef DriverObject As DRIVER_OBJECT, ByRef pIrp As IRP) As Long + pIrp.IoStatus.Information = 0 + pIrp.IoStatus.StatusPointer = STATUS_SUCCESS + IoCompleteRequest pIrp, IO_NO_INCREMENT + OnClose = STATUS_SUCCESS +End Function + +Public Function OnDeviceControl(ByRef DriverObject As DRIVER_OBJECT, ByRef pIrp As IRP) As Long + DbgPrint VarPtr(dbgsDevIoEntry) + Dim lpStack As LongPtr + Dim ioStack As IO_STACK_LOCATION + Dim ntStatus As Long + + pIrp.IoStatus.Information = 0 + lpStack = IoGetCurrentIrpStackLocation(pIrp) + If lpStack Then + DbgPrint VarPtr(dbgsStackOk) + CopyMemory ioStack, ByVal lpStack, LenB(ioStack) + If (ioStack.DeviceIoControl.IoControlCode = IOCTL_HWRLD_VERSION) And (pIrp.AssociatedIrp <> 0) Then + DbgPrint VarPtr(dbgsCmdOk) + Dim tVer As HelloWorldVersion + Dim lpBuffer As LongPtr + Dim cbIn As Long, cbOut As Long + lpBuffer = pIrp.AssociatedIrp + cbIn = ioStack.DeviceIoControl.InputBufferLength + cbOut = ioStack.DeviceIoControl.OutputBufferLength + If (lpBuffer = 0&) Or (cbIn <> 4) Or (cbOut <> LenB(tVer)) Then + If (lpBuffer = 0&) Then + ntStatus = STATUS_BUFFER_ALL_ZEROS + DbgPrint VarPtr(dbgsNoBuffer) + ElseIf (cbOut <> LenB(tVer)) Then + ntStatus = STATUS_INVALID_BUFFER_SIZE + DbgPrint VarPtr(dbgsBadSize) + Else + ntStatus = STATUS_INVALID_DEVICE_REQUEST + DbgPrint VarPtr(dbgsNoVal) + End If + Else + DbgPrint VarPtr(dbgsCopyOut) + tVer.Major = 1 + tVer.Minor = 2 + tVer.Build = 3 + tVer.Revision = 4 + CopyMemory ByVal lpBuffer, tVer, cbOut + pIrp.IoStatus.Information = LenB(tVer) + End If + + pIrp.IoStatus.StatusPointer = ntStatus + IoCompleteRequest pIrp, IO_NO_INCREMENT + OnDeviceControl = ntStatus + Exit Function + Else + If pIrp.AssociatedIrp = 0 Then + DbgPrint VarPtr(dbgsCmdFailPtr) + Else + DbgPrint VarPtr(dbgsCmdFailCode) + End If + End If + Else + DbgPrint VarPtr(dbgsStackFail) + End If + + pIrp.IoStatus.Information = 0 + ntStatus = STATUS_INVALID_PARAMETER + pIrp.IoStatus.StatusPointer = ntStatus + IoCompleteRequest pIrp, IO_NO_INCREMENT + OnDeviceControl = ntStatus +End Function + +Public Function OnOther(ByRef DriverObject As DRIVER_OBJECT, ByRef pIrp As IRP) As Long + Dim ntStatus As Long + ntStatus = STATUS_INVALID_DEVICE_REQUEST + pIrp.IoStatus.Information = 0 + pIrp.IoStatus.StatusPointer = ntStatus + IoCompleteRequest pIrp, IO_NO_INCREMENT + OnOther = ntStatus +End Function + +Public Sub OnUnload(DriverObject As DRIVER_OBJECT) + If Device.Size = 0 Then Exit Sub + IoDeleteSymbolicLink DeviceLink + IoDeleteDevice ByVal DriverObject.DeviceObject +End Sub + +Private Sub InitDebugStrings() +dbgsEntry.aCh(0) = &h45: dbgsEntry.aCh(1) = &h6E: dbgsEntry.aCh(2) = &h74: dbgsEntry.aCh(3) = &h72: dbgsEntry.aCh(4) = &h79: dbgsEntry.aCh(5) = &h20 +dbgsEntry.aCh(6) = &h70: dbgsEntry.aCh(7) = &h6F: dbgsEntry.aCh(8) = &h69: dbgsEntry.aCh(9) = &h6E: dbgsEntry.aCh(10) = &h74: dbgsEntry.aCh(11) = &h20 +dbgsEntry.aCh(12) = &h73: dbgsEntry.aCh(13) = &h75: dbgsEntry.aCh(14) = &h63: dbgsEntry.aCh(15) = &h63: dbgsEntry.aCh(16) = &h65: dbgsEntry.aCh(17) = &h73 +dbgsEntry.aCh(18) = &h73 + +dbgsDevIoEntry.aCh(0) = &h44: dbgsDevIoEntry.aCh(1) = &h65: dbgsDevIoEntry.aCh(2) = &h76: dbgsDevIoEntry.aCh(3) = &h69: dbgsDevIoEntry.aCh(4) = &h63 +dbgsDevIoEntry.aCh(5) = &h65: dbgsDevIoEntry.aCh(6) = &h49: dbgsDevIoEntry.aCh(7) = &h6F: dbgsDevIoEntry.aCh(8) = &h43: dbgsDevIoEntry.aCh(9) = &h6F +dbgsDevIoEntry.aCh(10) = &h6E: dbgsDevIoEntry.aCh(11) = &h74: dbgsDevIoEntry.aCh(12) = &h72: dbgsDevIoEntry.aCh(13) = &h6F: dbgsDevIoEntry.aCh(14) = &h6C +dbgsDevIoEntry.aCh(15) = &h20: dbgsDevIoEntry.aCh(16) = &h72: dbgsDevIoEntry.aCh(17) = &h65: dbgsDevIoEntry.aCh(18) = &h63: dbgsDevIoEntry.aCh(19) = &h65 +dbgsDevIoEntry.aCh(20) = &h69: dbgsDevIoEntry.aCh(21) = &h76: dbgsDevIoEntry.aCh(22) = &h65: dbgsDevIoEntry.aCh(23) = &h64 + +dbgsStackOk.aCh(0) = &H56: dbgsStackOk.aCh(1) = &H61: dbgsStackOk.aCh(2) = &H6C: dbgsStackOk.aCh(3) = &H69: dbgsStackOk.aCh(4) = &H64: dbgsStackOk.aCh(5) = &H61 +dbgsStackOk.aCh(6) = &H74: dbgsStackOk.aCh(7) = &H65: dbgsStackOk.aCh(8) = &H64: dbgsStackOk.aCh(9) = &H20: dbgsStackOk.aCh(10) = &H73: dbgsStackOk.aCh(11) = &H74 +dbgsStackOk.aCh(12) = &H61: dbgsStackOk.aCh(13) = &H63: dbgsStackOk.aCh(14) = &H6B: dbgsStackOk.aCh(15) = &H20: dbgsStackOk.aCh(16) = &H70: dbgsStackOk.aCh(17) = &H74 +dbgsStackOk.aCh(18) = &H72 + +dbgsStackFail.aCh(0) = &H4E: dbgsStackFail.aCh(1) = &H6F: dbgsStackFail.aCh(2) = &H20: dbgsStackFail.aCh(3) = &H73: dbgsStackFail.aCh(4) = &H74: dbgsStackFail.aCh(5) = &H61 +dbgsStackFail.aCh(6) = &H63: dbgsStackFail.aCh(7) = &H6B: dbgsStackFail.aCh(8) = &H20: dbgsStackFail.aCh(9) = &H70: dbgsStackFail.aCh(10) = &H74: dbgsStackFail.aCh(11) = &H72 + +dbgsCmdOk.aCh(0) = &H56: dbgsCmdOk.aCh(1) = &H61: dbgsCmdOk.aCh(2) = &H6C: dbgsCmdOk.aCh(3) = &H69: dbgsCmdOk.aCh(4) = &H64: dbgsCmdOk.aCh(5) = &H61: dbgsCmdOk.aCh(6) = &H74 +dbgsCmdOk.aCh(7) = &H65: dbgsCmdOk.aCh(8) = &H64: dbgsCmdOk.aCh(9) = &H20: dbgsCmdOk.aCh(10) = &H63: dbgsCmdOk.aCh(11) = &H6D: dbgsCmdOk.aCh(12) = &H64: dbgsCmdOk.aCh(13) = &H63 +dbgsCmdOk.aCh(14) = &H6F: dbgsCmdOk.aCh(15) = &H64: dbgsCmdOk.aCh(16) = &H65: dbgsCmdOk.aCh(17) = &H20: dbgsCmdOk.aCh(18) = &H61: dbgsCmdOk.aCh(19) = &H6E: dbgsCmdOk.aCh(20) = &H64 +dbgsCmdOk.aCh(21) = &H20: dbgsCmdOk.aCh(22) = &H61: dbgsCmdOk.aCh(23) = &H73: dbgsCmdOk.aCh(24) = &H63: dbgsCmdOk.aCh(25) = &H49: dbgsCmdOk.aCh(26) = &H72: dbgsCmdOk.aCh(27) = &H70 + +dbgsCmdFailCode.aCh(0) = &H49: dbgsCmdFailCode.aCh(1) = &H6E: dbgsCmdFailCode.aCh(2) = &H76: dbgsCmdFailCode.aCh(3) = &H61: dbgsCmdFailCode.aCh(4) = &H6C: dbgsCmdFailCode.aCh(5) = &H69 +dbgsCmdFailCode.aCh(6) = &H64: dbgsCmdFailCode.aCh(7) = &H20: dbgsCmdFailCode.aCh(8) = &H63: dbgsCmdFailCode.aCh(9) = &H6D: dbgsCmdFailCode.aCh(10) = &H64: dbgsCmdFailCode.aCh(11) = &H63 +dbgsCmdFailCode.aCh(12) = &H6F: dbgsCmdFailCode.aCh(13) = &H64: dbgsCmdFailCode.aCh(14) = &H65 + +dbgsCmdFailPtr.aCh(0) = &H49: dbgsCmdFailPtr.aCh(1) = &H6E: dbgsCmdFailPtr.aCh(2) = &H76: dbgsCmdFailPtr.aCh(3) = &H61: dbgsCmdFailPtr.aCh(4) = &H6C: dbgsCmdFailPtr.aCh(5) = &H69 +dbgsCmdFailPtr.aCh(6) = &H64: dbgsCmdFailPtr.aCh(7) = &H20: dbgsCmdFailPtr.aCh(8) = &H61: dbgsCmdFailPtr.aCh(9) = &H73: dbgsCmdFailPtr.aCh(10) = &H63: dbgsCmdFailPtr.aCh(11) = &H49 +dbgsCmdFailPtr.aCh(12) = &H72: dbgsCmdFailPtr.aCh(13) = &H70 + +dbgsNoBuffer.aCh(0) = &H4E: dbgsNoBuffer.aCh(1) = &H6F: dbgsNoBuffer.aCh(2) = &H20: dbgsNoBuffer.aCh(3) = &H62: dbgsNoBuffer.aCh(4) = &H75: dbgsNoBuffer.aCh(5) = &H66 +dbgsNoBuffer.aCh(6) = &H66: dbgsNoBuffer.aCh(7) = &H65: dbgsNoBuffer.aCh(8) = &H72: dbgsNoBuffer.aCh(9) = &H20: dbgsNoBuffer.aCh(10) = &H70: dbgsNoBuffer.aCh(11) = &H74 +dbgsNoBuffer.aCh(12) = &H72 + +dbgsBadSize.aCh(0) = &H42: dbgsBadSize.aCh(1) = &H61: dbgsBadSize.aCh(2) = &H64: dbgsBadSize.aCh(3) = &H20: dbgsBadSize.aCh(4) = &H62: dbgsBadSize.aCh(5) = &H75 +dbgsBadSize.aCh(6) = &H66: dbgsBadSize.aCh(7) = &H66: dbgsBadSize.aCh(8) = &H65: dbgsBadSize.aCh(9) = &H72: dbgsBadSize.aCh(10) = &H20: dbgsBadSize.aCh(11) = &H73 +dbgsBadSize.aCh(12) = &H69: dbgsBadSize.aCh(13) = &H7A: dbgsBadSize.aCh(14) = &H65 + +dbgsNoVal.aCh(0) = &H46: dbgsNoVal.aCh(1) = &H61: dbgsNoVal.aCh(2) = &H69: dbgsNoVal.aCh(3) = &H6C: dbgsNoVal.aCh(4) = &H65: dbgsNoVal.aCh(5) = &H64: dbgsNoVal.aCh(6) = &H20 +dbgsNoVal.aCh(7) = &H69: dbgsNoVal.aCh(8) = &H6E: dbgsNoVal.aCh(9) = &H70: dbgsNoVal.aCh(10) = &H75: dbgsNoVal.aCh(11) = &H74: dbgsNoVal.aCh(12) = &H20: dbgsNoVal.aCh(13) = &H76 +dbgsNoVal.aCh(14) = &H61: dbgsNoVal.aCh(15) = &H6C: dbgsNoVal.aCh(16) = &H69: dbgsNoVal.aCh(17) = &H64: dbgsNoVal.aCh(18) = &H61: dbgsNoVal.aCh(19) = &H74: dbgsNoVal.aCh(20) = &H69 +dbgsNoVal.aCh(21) = &H6F: dbgsNoVal.aCh(22) = &H6E + +dbgsCopyOut.aCh(0) = &H41: dbgsCopyOut.aCh(1) = &H6C: dbgsCopyOut.aCh(2) = &H6C: dbgsCopyOut.aCh(3) = &H20: dbgsCopyOut.aCh(4) = &H6F: dbgsCopyOut.aCh(5) = &H6B +dbgsCopyOut.aCh(6) = &H2C: dbgsCopyOut.aCh(7) = &H20: dbgsCopyOut.aCh(8) = &H63: dbgsCopyOut.aCh(9) = &H6F: dbgsCopyOut.aCh(10) = &H70: dbgsCopyOut.aCh(11) = &H79 +dbgsCopyOut.aCh(12) = &H69: dbgsCopyOut.aCh(13) = &H6E: dbgsCopyOut.aCh(14) = &H67: dbgsCopyOut.aCh(15) = &H20: dbgsCopyOut.aCh(16) = &H6F: dbgsCopyOut.aCh(17) = &H75 +dbgsCopyOut.aCh(18) = &H74 + +End Sub + +Private Sub InitUnicodeStrings() +'\Device\TBHWldDrv +strName.D(0) = &h5C: strName.D(1) = &h44: strName.D(2) = &h65: strName.D(3) = &h76: strName.D(4) = &h69: strName.D(5) = &h63: strName.D(6) = &h65 +strName.D(7) = &h5C: strName.D(8) = &h54: strName.D(9) = &h42: strName.D(10) = &h48: strName.D(11) = &h57: strName.D(12) = &h6C: strName.D(13) = &h64 +strName.D(14) = &h44: strName.D(15) = &h72: strName.D(16) = &h76 +RtlInitUnicodeString DeviceName, strName + +'\DosDevices\TBHWldDrv +strLink.D(0) = &h5C: strLink.D(1) = &h44: strLink.D(2) = &h6F: strLink.D(3) = &h73: strLink.D(4) = &h44: strLink.D(5) = &h65: strLink.D(6) = &h76 +strLink.D(7) = &h69: strLink.D(8) = &h63: strLink.D(9) = &h65: strLink.D(10) = &h73: strLink.D(11) = &h5C: strLink.D(12) = &h54: strLink.D(13) = &h42 +strLink.D(14) = &h48: strLink.D(15) = &h57: strLink.D(16) = &h6C: strLink.D(17) = &h64: strLink.D(18) = &h44: strLink.D(19) = &h72: strLink.D(20) = &h76 +RtlInitUnicodeString DeviceLink, strLink +End Sub +End Module \ No newline at end of file