title: How to establish communication between the mouse filter driver and the MFC
body: <p>I don't know English, so I use google translate.
  The problem is this. I am writing a project - a mouse filter driver that passes the coordinates of the cursor to the program. The program, in turn, reads them and produces a file that can be sent to Excel, where you can see the trajectory of the mouse movement.
  But this is still a long way off, but for now I cannot simply transfer data to the program.</p>
  <pre><code>typedef struct _MOU_OUTPUT_DATA {
      USHORT LastX;
      USHORT LastY;
  } MOU_OUTPUT_DATA, *PMOU_OUTPUT_DATA;
  
  void CMFCApplication1Dlg::OnBnClickedButton1()
  {
      devicehandle = CreateFile(L&quot;\\\\.\\MyMouLink&quot;, GENERIC_, 0, 0, OPEN_EXISTING, 0, 0);
      if (devicehandle == INVALID_HANDLE_VALUE) {
          MessageBox(L&quot;not valid value&quot;, 0, 0);
          return;
      }
      MessageBox(L&quot;valid value&quot;, 0, 0);
  }
  
  void CMFCApplication1Dlg::OnBnClickedButton2()
  {
      if (devicehandle == INVALID_HANDLE_VALUE) {
          MessageBox(L&quot;Error&quot;, 0, 0);
      }
      else {
          CloseHandle(devicehandle);
          MessageBox(L&quot;Ok&quot;, 0, 0);
      }
  }
  </code></pre>
  <p>This is the code for my MFC program. Only 2 buttons connect and disconnect, and as it is clear from the code, <code>INVALID_HANDLE_VALUE</code> is always true.</p>
  <pre><code>NTSTATUS DispatchPass(PDEVICE_OBJECT DeviceObject, PIRP Irp) 
  {   
      NTSTATUS status;
      PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(Irp);
  
      switch (irpsp-&gt;Parameters.DeviceIoControl.IoControlCode)
      {
      case IRP_MJ_CREATE:
          KdPrint((&quot;Create request \r\n&quot;));
          break;
      case IRP_MJ_READ:
          KdPrint((&quot;Read request \r\n&quot;));
          break;
      case IRP_MJ_WRITE:
          KdPrint((&quot;Write request \r\n&quot;));
          break;
      default:
          status = STATUS_INVALID_PARAMETER;
          KdPrint((&quot;INVALID PARAMETER \r\n&quot;));
          break;
      }
      return IoCallDriver(((PDEVICE_EXTENSION)DeviceObject-&gt;DeviceExtension)-&gt;MyUSB_Device, Irp);
  }
  </code></pre>
  <p>This part of the code is called in the driver and called</p>
  <pre><code>for (int i = 0; i &lt; IRP_MJ_MAXIMUM_FUNCTION;i++) {
          DriverObject-&gt;MajorFunction[i] = DispatchPass;
      }
  </code></pre>
  <p>I hope you understand :D</p>
  <pre><code>NTSTATUS MyAttachDevice(PDRIVER_OBJECT DriverObject)
  {
  NTSTATUS status;
  UNICODE_STRING MCName = RTL_CONSTANT_STRING(L&quot;\\Driver\\mouclass&quot;);
  PDRIVER_OBJECT targetDriverObject = NULL;
  PDEVICE_OBJECT currentDeviceObject = NULL;
  PDEVICE_OBJECT myDeviceObject = NULL;
  status = ObReferenceObjectByName(&amp;MCName, OBJ_CASE_INSENSITIVE, NULL, 0, 
  *IoDriverObjectType, KernelMode, NULL, (PVOID*)&amp;targetDriverObject);
  
  if (!NT_SUCCESS(status)) {
      KdPrint((&quot;ObReference failed \r\n&quot;));
      return status;
  }
  
  ObDereferenceObject(targetDriverObject);
  
  currentDeviceObject = targetDriverObject-&gt;DeviceObject;
  
  while (currentDeviceObject) {
  
      status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), NULL, 
      FILE_DEVICE_MOUSE, 0, FALSE, &amp;myDeviceObject);
      if (!NT_SUCCESS(status)) {
          //do your work
          return status;
      }
      RtlZeroMemory(myDeviceObject-&gt;DeviceExtension, sizeof(DEVICE_EXTENSION));
      status = IoAttachDeviceToDeviceStackSafe(myDeviceObject, 
      currentDeviceObject, &amp;((PDEVICE_EXTENSION)myDeviceObject- 
      &gt;DeviceExtension)-&gt;LowerKbdDevice);
      
      if (!NT_SUCCESS(status)) {
          //IoDeleteDevice(myDeviceObject);
          //do your work
          return status;
      }
  
  
      myDeviceObject-&gt;Flags |= DO_BUFFERED_IO;
      myDeviceObject-&gt;Flags &amp;= ~DO_DEVICE_INITIALIZING;
  
      currentDeviceObject = currentDeviceObject-&gt;NextDevice;
  }
  
  return STATUS_SUCCESS;
  }
  </code></pre>