DeviceIoControl not working

This is a discussion on DeviceIoControl not working within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to make my driver accept ioctls, but it doesn't even call the IO Control function, the debug output ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    30

    DeviceIoControl not working

    I'm trying to make my driver accept ioctls, but it doesn't even call the IO Control function, the debug output isn't there. I can see in DbgView that it's loaded and executed fine, but ioctls don't seem to be working. Below is the driver and userspace code.

    Code:
    #include <ntddk.h>
    
    #define MY_IOCTL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
    
    /*****************************************
    * Unloading function					 *
    *****************************************/
    
    void DriverUnload(PDRIVER_OBJECT DriverObject)
    {    
        UNICODE_STRING PipeName;
        
        DbgPrint("Driver Unloading...\n");
        
        RtlInitUnicodeString(&PipeName, L"\\DosDevices\\x");
        IoDeleteSymbolicLink(&PipeName);
    
        IoDeleteDevice(DriverObject->DeviceObject);
    }
    
    /*****************************************
    * TestFunction() function				 *
    *****************************************/
    
    void TestFunction(PIRP Irp, PIO_STACK_LOCATION pIoStackIrp) {}
    
    /*****************************************
    * I/O Functions							 *
    *****************************************/
    
    NTSTATUS PipeOpened(PDEVICE_OBJECT DeviceObject, PIRP Irp)
    {
        DbgPrint("Pipe to me was opened!\n");
        return((NTSTATUS)STATUS_SUCCESS);
    }
    
    NTSTATUS PipeClosed(PDEVICE_OBJECT DeviceObject, PIRP Irp)
    {
        DbgPrint("Pipe to me was closed!\n");
        return((NTSTATUS)STATUS_SUCCESS);
    }
    
    NTSTATUS IoCtl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
    {
    	PIO_STACK_LOCATION pIoStackIrp = NULL;
        DbgPrint("IoCtl called!\n");
    	
    	pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);
    	
    	if(pIoStackIrp)
    	{
    		switch(pIoStackIrp->Parameters.DeviceIoControl.IoControlCode)
    		{
    			case MY_IOCTL:
    				TestFunction(Irp, pIoStackIrp);
    				break;
    		}
    	}
    	else
    		DbgPrint("IoGetCurrentIrpStackLocation() returned NULL!");
    		
    	Irp->IoStatus.Status = ((NTSTATUS)STATUS_SUCCESS);
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    	
        return((NTSTATUS)STATUS_SUCCESS);
    }
    
    NTSTATUS UnsupportedFunction(PDEVICE_OBJECT DeviceObject, PIRP Irp)
    {
    	DbgPrint("UnsupportedFunction called!\n");
    	return((NTSTATUS)STATUS_SUCCESS);
    }
    
    NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath)
    {
        NTSTATUS ntstatus = STATUS_SUCCESS;
        PDEVICE_OBJECT pDeviceObject = NULL;
        UNICODE_STRING DriverName, DosDeviceName;
    	int i;
    	
    	DbgPrint("Driver initializing...\n");
    
        RtlInitUnicodeString(&DriverName, L"\\Device\\x");
        RtlInitUnicodeString(&DosDeviceName, L"\\DosDevices\\x"); 
    
        IoCreateDevice(pDriverObject, 0, &DriverName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
    	IoCreateSymbolicLink(&DosDeviceName, &DriverName);
    	
    	for(i=0;i<IRP_MJ_MAXIMUM_FUNCTION;i++)
    		pDriverObject->MajorFunction[i] = UnsupportedFunction;
            
    	pDriverObject->MajorFunction[IRP_MJ_CREATE]            = PipeOpened;
    	pDriverObject->MajorFunction[IRP_MJ_CLOSE]             = PipeClosed;
    	pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]    = IoCtl;
    
    	pDriverObject->DriverUnload =  DriverUnload; 
    	
    	pDeviceObject->Flags |= DO_DIRECT_IO;
    	pDeviceObject->Flags &= (~DO_DEVICE_INITIALIZING);
    
        return((NTSTATUS)STATUS_SUCCESS);
    }
    And the userspace code:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    #define MY_IOCTL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
    
    int main(int argc, char **argv)
    {
    	unsigned long int i;
    	unsigned char string[255];
    	HANDLE hFile = CreateFile("\\Device\\x", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    	if(hFile == NULL)
    	{
    		printf("Failed.\n");
    		return(0);
    	}
    	DeviceIoControl(hFile, MY_IOCTL, "1248", sizeof("1248"), NULL, 0, &i, NULL);
    	return(0);
    }
    Last edited by Wolf`; 08-08-2011 at 03:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why its not working?
    By Dulus in forum C Programming
    Replies: 7
    Last Post: 11-01-2010, 09:43 AM
  2. Replies: 9
    Last Post: 03-30-2009, 05:09 AM
  3. Win32 Serial Communication & DeviceIoControl()
    By button_basher in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-01-2005, 09:37 PM
  4. DeviceIOControl()
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2005, 03:26 PM
  5. working out
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-29-2003, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21