普通派发函数

read,write

R3

  1. HANDLE deviceHandle = NULL;
  2. void CDeviceUserCtlDlg::OnBnClickedButton1()
  3. {
  4. // TODO: Add your control notification handler code here
  5. if (deviceHandle!=INVALID_HANDLE_VALUE)
  6. {
  7. CloseHandle(deviceHandle);
  8. }
  9. }
  10. void CDeviceUserCtlDlg::OnBnClickedButton2()
  11. {
  12. // TODO: Add your control notification handler code here
  13. deviceHandle =CreateFile(L"\\\\.\\mydevicelink123",GENERIC_ALL,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM,0);
  14. if (deviceHandle==INVALID_HANDLE_VALUE)
  15. {
  16. MessageBox(L"vaild value",0,0);
  17. return;
  18. }
  19. MessageBox(L"not valid value",0,0);

image.png

R0

  1. #include <ntddk.h>
  2. UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\mydevice123");
  3. UNICODE_STRING SymLinkName = RTL_CONSTANT_STRING(L"\\??\\mydevicelink123");
  4. PDEVICE_OBJECT DeviceObject = NULL;
  5. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  6. IoDeleteSymbolicLink(&SymLinkName);
  7. IoDeleteDevice(DeviceObject);
  8. KdPrint(("driver unload \r\n"));
  9. }
  10. NTSTATUS DispatchPassThru(PDEVICE_OBJECT DeviceObject,PIRP Irp) {
  11. PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);
  12. NTSTATUS status = STATUS_SUCCESS;
  13. switch (irpsp->MajorFunction)
  14. {
  15. case IRP_MJ_CREATE:
  16. KdPrint(("create request \r\n"));
  17. break;
  18. case IRP_MJ_CLOSE:
  19. KdPrint(("close request\r\n"));
  20. break;
  21. case IRP_MJ_READ:
  22. KdPrint(("read quest\r\n"));
  23. break;
  24. default:
  25. break;
  26. }
  27. Irp->IoStatus.Information = 0;
  28. Irp->IoStatus.Status = status;
  29. IoCompleteRequest(Irp,IO_NO_INCREMENT);
  30. return status;
  31. }
  32. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  33. int i;
  34. NTSTATUS status;
  35. DriverObject->DriverUnload = Unload;
  36. status = IoCreateDevice(DriverObject,0,&DeviceName,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&DeviceObject);
  37. if (!NT_SUCCESS(status))
  38. {
  39. KdPrint(("creating devce failed \r\n"));
  40. return status;
  41. }
  42. IoCreateSymbolicLink(&SymLinkName,&DeviceName);
  43. if (!NT_SUCCESS(status))
  44. {
  45. KdPrint(("creating symbolic link failed\r\n"));
  46. IoDeleteDevice(DeviceObject);
  47. return status;
  48. }
  49. for ( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
  50. {
  51. DriverObject->MajorFunction[i] = DispatchPassThru;
  52. }
  53. KdPrint(("driver load \r\n"));
  54. return status;
  55. }

deviceControl

R0

  1. #include <ntddk.h>
  2. #define DEVICE_SEND CTL_CODE(FILE_DEVICE_UNKNOWN,0X801,METHOD_BUFFERED,FILE_WRITE_DATA)
  3. #define DEVICE_REC CTL_CODE(FILE_DEVICE_UNKNOWN,0X802,METHOD_BUFFERED,FILE_READ_DATA)
  4. //0X800-0XFFF buffercopy,
  5. UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\mydevice123");
  6. UNICODE_STRING SymLinkName = RTL_CONSTANT_STRING(L"\\??\\mydevicelink123");
  7. PDEVICE_OBJECT DeviceObject = NULL;
  8. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  9. IoDeleteSymbolicLink(&SymLinkName);
  10. IoDeleteDevice(DeviceObject);
  11. KdPrint(("driver unload \r\n"));
  12. }
  13. NTSTATUS DispatchPassThru(PDEVICE_OBJECT DeviceObject,PIRP Irp) {
  14. PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);
  15. NTSTATUS status = STATUS_SUCCESS;
  16. switch (irpsp->MajorFunction)
  17. {
  18. case IRP_MJ_CREATE:
  19. KdPrint(("create request \r\n"));
  20. break;
  21. case IRP_MJ_CLOSE:
  22. KdPrint(("close request\r\n"));
  23. break;
  24. default:
  25. status = STATUS_INVALID_PARAMETER;
  26. break;
  27. }
  28. Irp->IoStatus.Information = 0;
  29. Irp->IoStatus.Status = status;
  30. IoCompleteRequest(Irp,IO_NO_INCREMENT);
  31. return status;
  32. }
  33. NTSTATUS DispatchDevCTL(PDEVICE_OBJECT DeviceObject,PIRP Irp)
  34. {
  35. PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);
  36. NTSTATUS status = STATUS_SUCCESS;
  37. ULONG returnLength;
  38. PVOID buffer = Irp->AssociatedIrp.SystemBuffer;
  39. ULONG inLength = irpsp->Parameters.DeviceIoControl.InputBufferLength;
  40. ULONG outLength = irpsp->Parameters.DeviceIoControl.OutputBufferLength;
  41. WCHAR* demo = L"sample returned from driver";
  42. switch (irpsp->Parameters.DeviceIoControl.IoControlCode)
  43. {
  44. case DEVICE_SEND:
  45. KdPrint(("recv data is%ws \r\n", buffer));
  46. returnLength = (wcsnlen(buffer, 511) + 1) * 2;
  47. break;
  48. case DEVICE_REC:
  49. KdPrint(("sending data\r\n "));
  50. wcsncpy(buffer, demo, 511);
  51. returnLength = (wcsnlen(buffer,511)+1)*2;
  52. break;
  53. default:
  54. status = STATUS_INVALID_PARAMETER;
  55. break;
  56. }
  57. Irp->IoStatus.Status = status;
  58. Irp->IoStatus.Information = returnLength;
  59. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  60. return status;
  61. }
  62. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  63. int i;
  64. NTSTATUS status;
  65. DriverObject->DriverUnload = Unload;
  66. status = IoCreateDevice(DriverObject,0,&DeviceName,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&DeviceObject);
  67. if (!NT_SUCCESS(status))
  68. {
  69. KdPrint(("creating devce failed \r\n"));
  70. return status;
  71. }
  72. IoCreateSymbolicLink(&SymLinkName,&DeviceName);
  73. if (!NT_SUCCESS(status))
  74. {
  75. KdPrint(("creating symbolic link failed\r\n"));
  76. IoDeleteDevice(DeviceObject);
  77. return status;
  78. }
  79. for ( i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
  80. {
  81. DriverObject->MajorFunction[i] = DispatchPassThru;
  82. }
  83. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDevCTL;
  84. KdPrint(("driver load \r\n"));
  85. return status;
  86. }

R3

需要包含 winio.h
定义control码

  1. // DeviceUserCtlDlg.cpp : implementation file
  2. //
  3. #include "pch.h"
  4. #include "framework.h"
  5. #include "DeviceUserCtl.h"
  6. #include "DeviceUserCtlDlg.h"
  7. #include "afxdialogex.h"
  8. //引入winio.H
  9. #include "Winioctl.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #endif
  13. #define DEVICE_SEND CTL_CODE(FILE_DEVICE_UNKNOWN,0X801,METHOD_BUFFERED,FILE_WRITE_DATA)
  14. #define DEVICE_REC CTL_CODE(FILE_DEVICE_UNKNOWN,0X802,METHOD_BUFFERED,FILE_READ_DATA)
  15. HANDLE deviceHandle = NULL;
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialogEx
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. #ifdef AFX_DESIGN_TIME
  23. enum { IDD = IDD_ABOUTBOX };
  24. #endif
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  27. // Implementation
  28. protected:
  29. DECLARE_MESSAGE_MAP()
  30. };
  31. CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
  32. {
  33. }
  34. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  35. {
  36. CDialogEx::DoDataExchange(pDX);
  37. }
  38. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  39. END_MESSAGE_MAP()
  40. // CDeviceUserCtlDlg dialog
  41. CDeviceUserCtlDlg::CDeviceUserCtlDlg(CWnd* pParent /*=nullptr*/)
  42. : CDialogEx(IDD_DEVICEUSERCTL_DIALOG, pParent)
  43. {
  44. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  45. }
  46. void CDeviceUserCtlDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CDialogEx::DoDataExchange(pDX);
  49. }
  50. BEGIN_MESSAGE_MAP(CDeviceUserCtlDlg, CDialogEx)
  51. ON_WM_SYSCOMMAND()
  52. ON_WM_PAINT()
  53. ON_WM_QUERYDRAGICON()
  54. ON_BN_CLICKED(IDC_BUTTON1, &CDeviceUserCtlDlg::OnBnClickedButton1)
  55. ON_BN_CLICKED(IDC_BUTTON2, &CDeviceUserCtlDlg::OnBnClickedButton2)
  56. ON_BN_CLICKED(IDC_BUTTON3, &CDeviceUserCtlDlg::OnBnClickedButton3)
  57. ON_BN_CLICKED(IDC_BUTTON4, &CDeviceUserCtlDlg::OnBnClickedButton4)
  58. END_MESSAGE_MAP()
  59. // CDeviceUserCtlDlg message handlers
  60. BOOL CDeviceUserCtlDlg::OnInitDialog()
  61. {
  62. CDialogEx::OnInitDialog();
  63. // Add "About..." menu item to system menu.
  64. // IDM_ABOUTBOX must be in the system command range.
  65. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  66. ASSERT(IDM_ABOUTBOX < 0xF000);
  67. CMenu* pSysMenu = GetSystemMenu(FALSE);
  68. if (pSysMenu != nullptr)
  69. {
  70. BOOL bNameValid;
  71. CString strAboutMenu;
  72. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  73. ASSERT(bNameValid);
  74. if (!strAboutMenu.IsEmpty())
  75. {
  76. pSysMenu->AppendMenu(MF_SEPARATOR);
  77. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  78. }
  79. }
  80. // Set the icon for this dialog. The framework does this automatically
  81. // when the application's main window is not a dialog
  82. SetIcon(m_hIcon, TRUE); // Set big icon
  83. SetIcon(m_hIcon, FALSE); // Set small icon
  84. // TODO: Add extra initialization here
  85. return TRUE; // return TRUE unless you set the focus to a control
  86. }
  87. void CDeviceUserCtlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  88. {
  89. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  90. {
  91. CAboutDlg dlgAbout;
  92. dlgAbout.DoModal();
  93. }
  94. else
  95. {
  96. CDialogEx::OnSysCommand(nID, lParam);
  97. }
  98. }
  99. // If you add a minimize button to your dialog, you will need the code below
  100. // to draw the icon. For MFC applications using the document/view model,
  101. // this is automatically done for you by the framework.
  102. void CDeviceUserCtlDlg::OnPaint()
  103. {
  104. if (IsIconic())
  105. {
  106. CPaintDC dc(this); // device context for painting
  107. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  108. // Center icon in client rectangle
  109. int cxIcon = GetSystemMetrics(SM_CXICON);
  110. int cyIcon = GetSystemMetrics(SM_CYICON);
  111. CRect rect;
  112. GetClientRect(&rect);
  113. int x = (rect.Width() - cxIcon + 1) / 2;
  114. int y = (rect.Height() - cyIcon + 1) / 2;
  115. // Draw the icon
  116. dc.DrawIcon(x, y, m_hIcon);
  117. }
  118. else
  119. {
  120. CDialogEx::OnPaint();
  121. }
  122. }
  123. // The system calls this function to obtain the cursor to display while the user drags
  124. // the minimized window.
  125. HCURSOR CDeviceUserCtlDlg::OnQueryDragIcon()
  126. {
  127. return static_cast<HCURSOR>(m_hIcon);
  128. }
  129. void CDeviceUserCtlDlg::OnBnClickedButton1()
  130. {
  131. // TODO: Add your control notification handler code here
  132. if (deviceHandle!=INVALID_HANDLE_VALUE)
  133. {
  134. CloseHandle(deviceHandle);
  135. }
  136. }
  137. void CDeviceUserCtlDlg::OnBnClickedButton2()
  138. {
  139. // TODO: Add your control notification handler code here
  140. deviceHandle =CreateFile(L"\\\\.\\mydevicelink123",GENERIC_ALL,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM,0);
  141. if (deviceHandle==INVALID_HANDLE_VALUE)
  142. {
  143. MessageBox(L"vaild value",0,0);
  144. return;
  145. }
  146. MessageBox(L"not valid value",0,0);
  147. }
  148. void CDeviceUserCtlDlg::OnBnClickedButton3()
  149. {
  150. // TODO: Add your control notification handler code here
  151. //send data;
  152. WCHAR* message = L"hello message from userApp";
  153. ULONG returnLength = 0;
  154. char wr[4] = {0};
  155. if (deviceHandle != INVALID_HANDLE_VALUE && deviceHandle!=NULL)
  156. {
  157. if (!DeviceIoControl(deviceHandle,DEVICE_SEND,message,(wcslen(message)+1)*2,NULL,0,&returnLength,0))
  158. {
  159. MessageBox(L"device control error",0,0);
  160. }
  161. else
  162. {
  163. _itoa_s(returnLength, wr, 10);
  164. MessageBoxA(0,wr,0,0);
  165. }
  166. }
  167. }
  168. void CDeviceUserCtlDlg::OnBnClickedButton4()
  169. {
  170. // TODO: Add your control notification handler code here
  171. WCHAR message[1024] = {0};
  172. ULONG returnLength = 0;
  173. if (deviceHandle != INVALID_HANDLE_VALUE && deviceHandle != NULL)
  174. {
  175. if (!DeviceIoControl(deviceHandle, DEVICE_REC, NULL, 0, message, 1024, &returnLength, 0))
  176. {
  177. MessageBox(L"device control error", 0, 0);
  178. }
  179. else
  180. {
  181. MessageBox(message,0,0);
  182. }
  183. }
  184. }

image.png

cpu温度监控