what's the difference between CreateFile and WdfIoTargetOpen

Newbie in windows world, I can’t understand the difference between CreateFile and WdfIoTargetOpen.
I have two drivers in my system. I want 1st driver to send IOCTL(asynchronously) to 2nd Driver. I was planning to open handle and then send request to the driver.
I found two ways to do it:
First way:
a. Open handle using Createfile
b. Send async IO using DeviceIOControl function with OVERLAPPED structure

Second way
a. Open handle using WdfIoTargetOpen
b. For sending async IO, use combination of WdfIoTargetFormatRequestForRead/Write, WdfRequestSetCompletionRoutine and finally WdfRequestSend.

When do we use CreateFile vs WdfIoTargetOpen. Their purpose looks same to me such as opening a gateway to driver which can be then used to pass request to driver.

CreateFile is for applications. WdfIoTargetOpen is for drivers. It’s just that simple.

1 Like

So, createfile can only be used when opening an handle to driver from usermode applications, while WdfIoTargetOpen is used for driver to driver communication?

Yup.

Note that they both return a handle, but they instantiate entirely different objects, right? CreateFile instantiates a FILE_OBJECT (and returns a File Handle that you can use for operations on that object); WfdIoTargetOpen instantions a WDFIOTARGET object (and returns a WDFIOTARGET handle, that you can use with subsequent calls to WdfIoTargetXxxx).

1 Like

I had originally typed “CreateFile is for user-mode, WdfIoTargetOpen is for kernel-mode”, but since UMDF 2 came out, it’s not quite that simple. Technically speaking, you COULD use CreateFile in a UMDF 2 driver, but then you wouldn’t be able to send IRPs through that handle.

Also, technically speaking, there is a version of CreateFile (with a different name) that works in kernel mode, but its use is rather specialized, and it has the same limitation that you can’t send IRPs through the handle.

Just remember that the “Wdf” in the name stands for “Windows Driver Foundation”. Those APIs are all for writing drivers.

1 Like