普通派发函数
R3
HANDLE deviceHandle = NULL;void CDeviceUserCtlDlg::OnBnClickedButton1(){// TODO: Add your control notification handler code hereif (deviceHandle!=INVALID_HANDLE_VALUE){CloseHandle(deviceHandle);}}void CDeviceUserCtlDlg::OnBnClickedButton2(){// TODO: Add your control notification handler code heredeviceHandle =CreateFile(L"\\\\.\\mydevicelink123",GENERIC_ALL,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM,0);if (deviceHandle==INVALID_HANDLE_VALUE){MessageBox(L"vaild value",0,0);return;}MessageBox(L"not valid value",0,0);
R0
#include <ntddk.h>UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\mydevice123");UNICODE_STRING SymLinkName = RTL_CONSTANT_STRING(L"\\??\\mydevicelink123");PDEVICE_OBJECT DeviceObject = NULL;VOID Unload(IN PDRIVER_OBJECT DriverObject) {IoDeleteSymbolicLink(&SymLinkName);IoDeleteDevice(DeviceObject);KdPrint(("driver unload \r\n"));}NTSTATUS DispatchPassThru(PDEVICE_OBJECT DeviceObject,PIRP Irp) {PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);NTSTATUS status = STATUS_SUCCESS;switch (irpsp->MajorFunction){case IRP_MJ_CREATE:KdPrint(("create request \r\n"));break;case IRP_MJ_CLOSE:KdPrint(("close request\r\n"));break;case IRP_MJ_READ:KdPrint(("read quest\r\n"));break;default:break;}Irp->IoStatus.Information = 0;Irp->IoStatus.Status = status;IoCompleteRequest(Irp,IO_NO_INCREMENT);return status;}NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {int i;NTSTATUS status;DriverObject->DriverUnload = Unload;status = IoCreateDevice(DriverObject,0,&DeviceName,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&DeviceObject);if (!NT_SUCCESS(status)){KdPrint(("creating devce failed \r\n"));return status;}IoCreateSymbolicLink(&SymLinkName,&DeviceName);if (!NT_SUCCESS(status)){KdPrint(("creating symbolic link failed\r\n"));IoDeleteDevice(DeviceObject);return status;}for ( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++){DriverObject->MajorFunction[i] = DispatchPassThru;}KdPrint(("driver load \r\n"));return status;}
deviceControl
R0
#include <ntddk.h>#define DEVICE_SEND CTL_CODE(FILE_DEVICE_UNKNOWN,0X801,METHOD_BUFFERED,FILE_WRITE_DATA)#define DEVICE_REC CTL_CODE(FILE_DEVICE_UNKNOWN,0X802,METHOD_BUFFERED,FILE_READ_DATA)//0X800-0XFFF buffercopy,UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\mydevice123");UNICODE_STRING SymLinkName = RTL_CONSTANT_STRING(L"\\??\\mydevicelink123");PDEVICE_OBJECT DeviceObject = NULL;VOID Unload(IN PDRIVER_OBJECT DriverObject) {IoDeleteSymbolicLink(&SymLinkName);IoDeleteDevice(DeviceObject);KdPrint(("driver unload \r\n"));}NTSTATUS DispatchPassThru(PDEVICE_OBJECT DeviceObject,PIRP Irp) {PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);NTSTATUS status = STATUS_SUCCESS;switch (irpsp->MajorFunction){case IRP_MJ_CREATE:KdPrint(("create request \r\n"));break;case IRP_MJ_CLOSE:KdPrint(("close request\r\n"));break;default:status = STATUS_INVALID_PARAMETER;break;}Irp->IoStatus.Information = 0;Irp->IoStatus.Status = status;IoCompleteRequest(Irp,IO_NO_INCREMENT);return status;}NTSTATUS DispatchDevCTL(PDEVICE_OBJECT DeviceObject,PIRP Irp){PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);NTSTATUS status = STATUS_SUCCESS;ULONG returnLength;PVOID buffer = Irp->AssociatedIrp.SystemBuffer;ULONG inLength = irpsp->Parameters.DeviceIoControl.InputBufferLength;ULONG outLength = irpsp->Parameters.DeviceIoControl.OutputBufferLength;WCHAR* demo = L"sample returned from driver";switch (irpsp->Parameters.DeviceIoControl.IoControlCode){case DEVICE_SEND:KdPrint(("recv data is%ws \r\n", buffer));returnLength = (wcsnlen(buffer, 511) + 1) * 2;break;case DEVICE_REC:KdPrint(("sending data\r\n "));wcsncpy(buffer, demo, 511);returnLength = (wcsnlen(buffer,511)+1)*2;break;default:status = STATUS_INVALID_PARAMETER;break;}Irp->IoStatus.Status = status;Irp->IoStatus.Information = returnLength;IoCompleteRequest(Irp, IO_NO_INCREMENT);return status;}NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {int i;NTSTATUS status;DriverObject->DriverUnload = Unload;status = IoCreateDevice(DriverObject,0,&DeviceName,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&DeviceObject);if (!NT_SUCCESS(status)){KdPrint(("creating devce failed \r\n"));return status;}IoCreateSymbolicLink(&SymLinkName,&DeviceName);if (!NT_SUCCESS(status)){KdPrint(("creating symbolic link failed\r\n"));IoDeleteDevice(DeviceObject);return status;}for ( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++){DriverObject->MajorFunction[i] = DispatchPassThru;}DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDevCTL;KdPrint(("driver load \r\n"));return status;}
R3
需要包含 winio.h
定义control码
// DeviceUserCtlDlg.cpp : implementation file//#include "pch.h"#include "framework.h"#include "DeviceUserCtl.h"#include "DeviceUserCtlDlg.h"#include "afxdialogex.h"//引入winio.H#include "Winioctl.h"#ifdef _DEBUG#define new DEBUG_NEW#endif#define DEVICE_SEND CTL_CODE(FILE_DEVICE_UNKNOWN,0X801,METHOD_BUFFERED,FILE_WRITE_DATA)#define DEVICE_REC CTL_CODE(FILE_DEVICE_UNKNOWN,0X802,METHOD_BUFFERED,FILE_READ_DATA)HANDLE deviceHandle = NULL;// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogEx{public:CAboutDlg();// Dialog Data#ifdef AFX_DESIGN_TIMEenum { IDD = IDD_ABOUTBOX };#endifprotected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support// Implementationprotected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX){}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)END_MESSAGE_MAP()// CDeviceUserCtlDlg dialogCDeviceUserCtlDlg::CDeviceUserCtlDlg(CWnd* pParent /*=nullptr*/): CDialogEx(IDD_DEVICEUSERCTL_DIALOG, pParent){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CDeviceUserCtlDlg::DoDataExchange(CDataExchange* pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CDeviceUserCtlDlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, &CDeviceUserCtlDlg::OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2, &CDeviceUserCtlDlg::OnBnClickedButton2)ON_BN_CLICKED(IDC_BUTTON3, &CDeviceUserCtlDlg::OnBnClickedButton3)ON_BN_CLICKED(IDC_BUTTON4, &CDeviceUserCtlDlg::OnBnClickedButton4)END_MESSAGE_MAP()// CDeviceUserCtlDlg message handlersBOOL CDeviceUserCtlDlg::OnInitDialog(){CDialogEx::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != nullptr){BOOL bNameValid;CString strAboutMenu;bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);ASSERT(bNameValid);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}void CDeviceUserCtlDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialogEx::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CDeviceUserCtlDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialogEx::OnPaint();}}// The system calls this function to obtain the cursor to display while the user drags// the minimized window.HCURSOR CDeviceUserCtlDlg::OnQueryDragIcon(){return static_cast<HCURSOR>(m_hIcon);}void CDeviceUserCtlDlg::OnBnClickedButton1(){// TODO: Add your control notification handler code hereif (deviceHandle!=INVALID_HANDLE_VALUE){CloseHandle(deviceHandle);}}void CDeviceUserCtlDlg::OnBnClickedButton2(){// TODO: Add your control notification handler code heredeviceHandle =CreateFile(L"\\\\.\\mydevicelink123",GENERIC_ALL,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM,0);if (deviceHandle==INVALID_HANDLE_VALUE){MessageBox(L"vaild value",0,0);return;}MessageBox(L"not valid value",0,0);}void CDeviceUserCtlDlg::OnBnClickedButton3(){// TODO: Add your control notification handler code here//send data;WCHAR* message = L"hello message from userApp";ULONG returnLength = 0;char wr[4] = {0};if (deviceHandle != INVALID_HANDLE_VALUE && deviceHandle!=NULL){if (!DeviceIoControl(deviceHandle,DEVICE_SEND,message,(wcslen(message)+1)*2,NULL,0,&returnLength,0)){MessageBox(L"device control error",0,0);}else{_itoa_s(returnLength, wr, 10);MessageBoxA(0,wr,0,0);}}}void CDeviceUserCtlDlg::OnBnClickedButton4(){// TODO: Add your control notification handler code hereWCHAR message[1024] = {0};ULONG returnLength = 0;if (deviceHandle != INVALID_HANDLE_VALUE && deviceHandle != NULL){if (!DeviceIoControl(deviceHandle, DEVICE_REC, NULL, 0, message, 1024, &returnLength, 0)){MessageBox(L"device control error", 0, 0);}else{MessageBox(message,0,0);}}}

