0

I'm trying connect with my driver where i created the SymbolicLink this way:

#include <ntifs.h>
#include <ntddk.h>
#include <windef.h>
#include <stdlib.h>
    
PDEVICE_OBJECT pDeviceObject;
UNICODE_STRING dev, dos;
    
void MyUnloadProc(IN PDRIVER_OBJECT DriverObject)
{
    IoDeleteSymbolicLink(&dos);
    IoDeleteDevice(DriverObject->DeviceObject);
    
    DbgPrint("Goodbye from driver \n");
}
    
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
    RtlInitUnicodeString(&dev, L"\\Device\\Foo");
    RtlInitUnicodeString(&dos, L"\\??\\Foo");
    
    IoCreateDevice(DriverObject, 0, &dev, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
    IoCreateSymbolicLink(&dos, &dev);
    
    DriverObject->DriverUnload = MyUnloadProc;
    
    DbgPrint("Hello from driver \n");
    
    return STATUS_SUCCESS;
}

All that is made above is successful, but when tried a connection always receives the INVALID_HANDLE_VALUE error.

Why this is happening?

Here is only code that call CreateFile():

program Project1;
    
{$APPTYPE CONSOLE}
    
{$R *.res}
    
uses
  Windows,
  SysUtils;
    
begin
  try
    if CreateFile('\\.\Foo', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) = INVALID_HANDLE_VALUE then
      Writeln('Ivalid handle');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

Edit:

Test enviroment:

  • VMWare with Windows 7 Professional 32 bits
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • When `CreateFile()` returns `INVALID_HANDLE_VALUE`, what does `GetLastError()` report afterwards? – Remy Lebeau Jun 30 '20 at 16:13
  • Do what the documentation tells you, and call `GetLastError` after `CreateFile` fails. – David Heffernan Jun 30 '20 at 16:13
  • you forget remove `DO_DEVICE_INITIALIZING`. as result you must got `STATUS_NO_SUCH_DEVICE` status (win32 lost info and return 2 - file not found) – RbMm Jun 30 '20 at 16:14
  • @RemyLebeau, `GetLastError()` = **Incorrect function** > **1** –  Jun 30 '20 at 16:42
  • @Davison - need call `RtlGetLastNtStatus()` but not `GetLastError`. i not note that you not initialize `MajorFunction` in driver object too. as result you got `STATUS_INVALID_DEVICE_REQUEST` converted to 1 by win32 layer. too many errors in small code – RbMm Jun 30 '20 at 17:12
  • @RbMm, `GetLastError()` was called on usermode, not kernel mode. –  Jun 30 '20 at 17:19
  • you need call `RtlGetLastNtStatus()` in user mode of course. this is user mode api. and you got `STATUS_INVALID_DEVICE_REQUEST` – RbMm Jun 30 '20 at 17:32
  • @RbMm, *`"i not note that you not initialize MajorFunction in driver object too"`* <-- **Solution**. Thank you very much. –  Jul 01 '20 at 06:53

0 Answers0