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);
}