0

I'm trying to create a filter driver. I installed the Windows DDK and the new templates show up in Visual Studio Community 2022. But when I create an XPS print filter driver, it doesn't compile. Either I'm missing definitions or stuff is redefined. ChatGPT is no help and giving up isn't an option.

I created an empty kernel mode driver project. I added a DLLMain and a DriverEntry. I include the following

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <Windows.h>
#include <ntddk.h>

#include "Logger.h"

Logger.h only has a bare class without even a defined constructor. If I remove Windows.h, I'm missing defines. ntddk.h is necessary for the driver project. with them both, other stuff is redefined. What is the proper includes?

I don't know if it's relevant, but here is my DriverEntry

#include "Framework.h"
#include "DriverBase.h"

NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)
{
    UNREFERENCED_PARAMETER(RegistryPath);

    DbgPrint("Print Filter Driver: DriverEntry called\n");

    // Register the driver's major function callbacks
    DriverObject->MajorFunction[IRP_MJ_CREATE] = PrintFilterCreate;
    DriverObject->MajorFunction[IRP_MJ_CLOSE] = PrintFilterClose;
    DriverObject->MajorFunction[IRP_MJ_CLEANUP] = CleanupDispatch;
    DriverObject->MajorFunction[IRP_MJ_WRITE] = PrintFilterWrite;

    DriverObject->DriverUnload = PrintFilterUnload;

    return STATUS_SUCCESS;
}

NTSTATUS PrintFilterCreate(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    DbgPrint("Print Filter Driver: IRP_MJ_CREATE called\n");

    // Perform any necessary processing for the IRP_MJ_CREATE operation

    // Complete the IRP
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return STATUS_SUCCESS;
}

NTSTATUS PrintFilterClose(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    DbgPrint("Print Filter Driver: IRP_MJ_CLOSE called\n");

    // Perform any necessary processing for the IRP_MJ_CLOSE operation

    // Complete the IRP
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return STATUS_SUCCESS;
}

NTSTATUS CleanupDispatch(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    DbgPrint("Print Filter Driver: IRP_MJ_CLEANUP called\n");

    // Perform any necessary cleanup operations

    // Complete the IRP
    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return STATUS_SUCCESS;
}

NTSTATUS PrintFilterWrite(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp)
{
    UNREFERENCED_PARAMETER(DeviceObject);

    DbgPrint("Print Filter Driver: IRP_MJ_WRITE called\n");

    NTSTATUS status = STATUS_SUCCESS;
    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);

    // Process the write operation
    // Access the data buffer through Irp->AssociatedIrp.SystemBuffer
    // and the length of the data through irpStack->Parameters.Write.Length

    // Complete the IRP
    Irp->IoStatus.Status = status;
    Irp->IoStatus.Information = irpStack->Parameters.Write.Length;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return status;
}

VOID PrintFilterUnload(_In_ PDRIVER_OBJECT DriverObject)
{
    DbgPrint("Print Filter Driver: Unload called\n");



    DbgPrint("Print Filter Driver: Unload complete\n");
}
user9778277
  • 153
  • 1
  • 11
  • *"it doesn't compile"* - What's the error you are getting? – IInspectable Jun 26 '23 at 22:07
  • Of course when you include *windows.h* and *ntddk.h* at once and without different namespace you got huge count of errors. Include only *ntifs.h* . Also you say *ddk* ? Such name already not used many years – RbMm Jun 27 '23 at 05:21
  • I started by selecting the XPS Filter Driver template in VS 2022. It started with ntddk.h. – user9778277 Jun 27 '23 at 07:50
  • @IInspectable with #include commented out, I get errors like the following: Severity Code Description Project File Line Suppression State Error C2061 syntax error: identifier 'PVECTORED_EXCEPTION_HANDLER' PB2020 C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um\errhandlingapi.h 136 with both windows.h AND ntddk.h I get redefine errors: Severity Code Description Project File Line Suppression State Error C2011 '_PROCESSOR_NUMBER': 'struct' type redefinition PB2020 C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared\ntdef.h 778 – user9778277 Jun 27 '23 at 07:54
  • I didn't post all of them because in either case, there are more than 100 – user9778277 Jun 27 '23 at 07:55
  • @RbMm, it still fails with over 100 errors of : Severity Code Description Project File Line Suppression State Error C2011 '_GROUP_AFFINITY': 'struct' type redefinition PB2020 C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared\ntdef.h 789 #pragma once #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include //#include //#include #include #include "Logger.h" – user9778277 Jun 27 '23 at 07:57
  • @user9778277 - of course it fail. and you nothing understand. – RbMm Jun 27 '23 at 09:23
  • @RbMm, dear God. I literally tried your suggestion and it failed. You really do NOT have to be so snarky about it. I miss the earlier days of this site when this was a community for developers by developers to ask questions and gain understanding, not just be told I don't understand (that's why I'm here), or "it's complex" (so not helpful). I wonder where I would be as a developer if when I was first learning C on Windows or Object Oriented Programming and I got stuck if someone would have just dismissed me instead of helping me. – user9778277 Jun 27 '23 at 18:24
  • OK, for the record, I solved this myself. I have the DriverEntry stuff in DriverBase.c and a DLLMain in DLLMain.cpp. By including windows.h for DllMain and ntddk.h in DriverBase.c, it compiles fine. – user9778277 Jun 27 '23 at 18:25
  • @user9778277 - no , you not try my suggestion . re-read **when you include windows.h and ntddk.h at once and without different namespace you got huge count of errors.** but you still include *windows.h* .. simply remove it. include **only** *ntifs.h* – RbMm Jun 27 '23 at 20:09

0 Answers0