CreateFile issue

Hello all,

I have written a sample device driver in KMDF. I am using 6001.18002 WDK. I ahve able to successfully able to load my sample driver.

Now I am in the process of writing windows console application to access that driver.
For using CreateFile I need to pass the device file name but i don’t have any idea from where I can get the device name and how to access the sample driver.

Regards,
Nilam

Did you enable a device interface or use a hard coded name when creating the wdfdevice? Busenum.exe in the toaster sample shows how to enumerate instances of a device interface guid and open them. A hard coded name is easier, you just pass \.\thehardcodedname To CreateFile

d

Sent from my phone with no t9, all spilling mistakes are not intentional.

-----Original Message-----
From: xxxxx@tcs.com
Sent: Monday, June 29, 2009 4:49 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] CreateFile issue

Hello all,

I have written a sample device driver in KMDF. I am using 6001.18002 WDK. I ahve able to successfully able to load my sample driver.

Now I am in the process of writing windows console application to access that driver.
For using CreateFile I need to pass the device file name but i don’t have any idea from where I can get the device name and how to access the sample driver.

Regards,
Nilam


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

I am new to the windows driver and don’t get very good information. I know that using IoCreateDevice we can give the driver name and create a symbolic link for the device name but I am using WdfDeviceCreate and don’t have any idea of how to the device name when creating the
wdfdevice. Below is the code for the DeviceAdd function.

NTSTATUS EvtDeviceAdd(
IN WDFDRIVER Driver,
IN PWDFDEVICE_INIT DeviceInit
)
{
NTSTATUS status;
WDFDEVICE device;
PDEVICE_CONTEXT devCtx = NULL;
WDF_OBJECT_ATTRIBUTES attributes;
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
WDF_IO_QUEUE_CONFIG ioQConfig;

UNREFERENCED_PARAMETER(Driver);

KdPrint((__DRIVER_NAME “–> EvtDeviceAdd\n”));

/*set the callback functions that will be executed on PNP and Power events*/
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceD0Entry = EvtDeviceD0Entry;
pnpPowerCallbacks.EvtDeviceD0Exit = EvtDeviceD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoBuffered);

/*initialize storage for the device context*/
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT);

/*create a device instance.*/
status = WdfDeviceCreate(&DeviceInit, &attributes, &device);
if(!NT_SUCCESS(status))
{
KdPrint((__DRIVER_NAME
“WdfDeviceCreate failed with status 0x%08x\n”, status));
return status;
}

devCtx = GetDeviceContext(device);

/*create the default IO queue. this one will be used for ioctl requests*/
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQConfig,
WdfIoQueueDispatchSequential);
ioQConfig.EvtIoDefault = EvtDeviceIoDefault;
status = WdfIoQueueCreate(device,
&ioQConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&devCtx->IoDefaultQueue);
if(!NT_SUCCESS(status))
{
KdPrint((__DRIVER_NAME
“WdfIoQueueCreate failed with status 0x%08x\n”, status));
return status;
}

status = WdfDeviceCreateDeviceInterface(device, &GUID_DEV_IF_BASIC, NULL);
if(!NT_SUCCESS(status))
{
KdPrint((__DRIVER_NAME
“WdfDeviceCreateDeviceInterface failed with status 0x%08x\n”, status));
return status;
}

KdPrint((__DRIVER_NAME “<– EvtDeviceAdd\n”));
return status;
}

Can you please tell me how to give the device name so that i can use it in CreateFile().

Thanks & regards,
Nilam

As suggested, passing a hardcodedname to CreateFile is easier. You can also consider the following.

In your sample drivers EvtDeviceAdd function, you are enabling device interface GUID_DEV_IF_BASIC using WdfDeviceCreateDeviceInterface.

In the console application, you need to use SetupAPI functions to enumerate this device interface and obtain the handle to the device in the order mentioned below:

  1. SetupDiGetClassDevs
  2. SetupDiEnumDeviceInterfaces
  3. SetupDiGetDeviceInterfaceDetail
  4. CreateFile

Please kindly search for these functions to know their functionality. You can easily use \WinDDK\6000\src\kmdf\usbsamp\ source to achieve the required.

In usbsamp\device.c UsbSamp_EvtDeviceAdd function, the device interface GUID_CLASS_USBSAMP_USB is created using WdfDeviceCreateDeviceInterface which is analogous to your sample driver’s GUID_DEV_IF_BASIC.

In the console application \WinDDK\6000\src\kmdf\usbsamp\exe\testapp.c, the device handle is obtained by above mentioned setupapi calls. Note the usage of same guid GUID_CLASS_USBSAMP_USB in both driver and application. It is defined in public.h header.

Regards.

Make it easy on yourself…

The call you’re looking for is WdfDeviceInitAssignName. It allows you to assign a name to your WDF Device. Just call this before calling WdfDeviceCreate:

DECLARE_CONST_UNICODE_STRING(MyDeviceName, L"\Device\MyDevice") ;

status = WdfDeviceInitAssignName(DeviceInit, &MyDeviceName);

if (!NT_SUCCESS(status)) {
… (handle the error case)…
}

In your user-mode code call CreateFile with \.\MyDevice (you’ll need to “escape” the backslashes in the C string, obviously).

Done.

Forget the whole GUID thing… at least until you know why you might need it,

Peter
OSR

Thanks a lot.

This information is very helpful to me.

Regards,
Nilam