title: Can`t open device by CreateFile, ERROR_INVALID_FUNCTION
body: <p>I try to open device object and send request to my driver by CreateFile and DeviceIoControl, but CreateFile fails with ERROR_INVALID_FUNCTION, also if I try to open device in WinObj - I get the same error, so how to solve this and what is it mean?</p>
  
  <p>test_driver.c</p>
  
  <pre><code>#include "test_driver.h"
  
  NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) {
      NTSTATUS status;
      PDEVICE_OBJECT deviceObject;
      UNICODE_STRING dNameString, dLinkString;
  
      RtlInitUnicodeString(&amp;dNameString, NT_DEVICE_NAME);
      status = IoCreateDevice(DriverObject, sizeof(65533), &amp;dNameString, FILE_DEVICE_UNKNOWN, 0, FALSE, &amp;deviceObject);
  
      if (!NT_SUCCESS(status)) {
          return status;
      }
  
      RtlInitUnicodeString(&amp;dLinkString, DOS_DEVICE_NAME);
      status = IoCreateSymbolicLink(&amp;dLinkString, &amp;dNameString);
  
      if (!NT_SUCCESS(status)) {
          IoDeleteDevice(deviceObject);
          return status;
      }
  
      DriverObject -&gt; MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverDeviceControl;
      DriverObject -&gt; DriverUnload = DriverUnload;
  
      DbgPrint("Driver loaded!");
      return STATUS_SUCCESS;
  }
  
  NTSTATUS DriverDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
      ULONG ioControlCode;
      PULONG ioBuffer;
      PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  
      ioBuffer = Irp -&gt; AssociatedIrp.SystemBuffer;
      ioControlCode = irpStack -&gt; Parameters.DeviceIoControl.IoControlCode;
      Irp -&gt; IoStatus.Status = STATUS_SUCCESS;
      Irp -&gt; IoStatus.Information = 0;
  
      switch (ioControlCode) {
          case TEST_SMTH:
              ioBuffer[0] = (ULONG)DriverEntry;
              Irp -&gt; IoStatus.Information = 4;
              break;
  
          default:
              Irp -&gt; IoStatus.Status = STATUS_INVALID_PARAMETER;
              break;
      }
  
      IoCompleteRequest(Irp, IO_NO_INCREMENT);
      DbgPrint("Request complete!");
      return Irp -&gt; IoStatus.Status;
  }
  
  VOID DriverUnload(IN PDRIVER_OBJECT DriverObject) {
      UNICODE_STRING dLinkString;
  
      RtlInitUnicodeString(&amp;dLinkString, DOS_DEVICE_NAME);
      IoDeleteSymbolicLink(&amp;dLinkString);
      IoDeleteDevice(DriverObject -&gt; DeviceObject);
      DbgPrint("Driver unloaded!");
  }
  </code></pre>
  
  <p>test_driver.h</p>
  
  <pre><code>#include &lt;ntddk.h&gt;
  
  #define NT_DEVICE_NAME  L"\\Device\\testDrv" 
  #define DOS_DEVICE_NAME L"\\DosDevices\\testDrv"
  
  #define FIRST_IOCTL_INDEX 0x800
  #define FILE_DEVICE_testDRV 0x00008000
  #define TEST_SMTH CTL_CODE(FILE_DEVICE_testDRV, FIRST_IOCTL_INDEX + 101, METHOD_BUFFERED, FILE_ANY_ACCESS)
  
  NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
  NTSTATUS DriverDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
  VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);
  </code></pre>
  
  <p>From program source:</p>
  
  <pre><code>HANDLE device = CreateFile("\\\\.\\testDrv", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  </code></pre>
  
  <p>I know that to open objects from NT space it is necessary to use NtCreateFile, but I created a symbolic link in my driver, so CreateFile must works well.</p>