KMDF C++ support in 2022

Hi,

At the moment I’m porting a driver from C to C++ (which has been C++ in the old days of Compuware Driver::Works).

Work is done, but the interface to KMDF callbacks still looks clumsy.

At the moment I have a C/C++ mix like …

<c-cpp-mix>

// All Logic in my class
class MyDevice {
    ...
    NTSTATUS   EvtDeviceD0Entry(IN WDF_POWER_DEVICE_STATE PreviousState) ;
   ...
} ;


// The WDF DeviceContext just contains my device 
typedef struct  {
    MyDevice    mydevice ; // context wraps class
} MyDevice_CONTEXT;

WDF_DECLARE_CONTEXT_TYPE(MyDevice_CONTEXT);

// The Wdf callbacks are just a wrappers to methods of my device
NTSTATUS        MyDevice__EvtDeviceD0Entry( IN WDFDEVICE wdfDevice,
    IN WDF_POWER_DEVICE_STATE PreviousState);
{
    MyDevice *mydevice = &(WdfObjectGet_MyDevicee_CONTEXT(wdfDevice)->mydevice) ;
    return mydevice->EvtDeviceD0Entry(PreviousState) ;
}

</c-cpp-mix>

I was hoping for …

<pure-cpp>

// WDF defines adevice base class "WdfDevice"
class MyDevice: WdfDevice {
    ...
    // mydevice implements the D0Entry callback
    virtual NTSTATUS   EvtDeviceD0Entry(IN WDF_POWER_DEVICE_STATE PreviousState) ;
    ...
}

</pure-cpp>

I remember seeing some MicroSoft docs showing C++ code examples, but somehow my Google Bubble is supressing these now.

I’d be happy to have a doc explaining current and future C++ support in KMDF.

thanks and kind regards,

Joerg Hoppe

Though written in (a highly restrained variant of) C++, KMDF provides a C Language interface to match the rest of the Window OS.

KMDF is mature, being more than 15 years old, and to the best of my knowledge there are no future plans to drastically change the interface. The old version of UMDF has a C++ interface based on the COM programming model, but this predictably proved… ah… “unpopular” for a long list of reasons.

I’m far more familiar with DriverWorks than I’d prefer to be, and… My best suggestion to you is not to propagate the mess that you have, and instead rewrite your driver as a proper KMDF driver… in proper C or using modern C++ as a better C.

Sorry for asking a question here that I think is related to this one.

Now, 2023, does the kernel mode driver development supports C++20 and C17? A few years ago I search about that, I vaguely remember that even the STL was not fully supported. I wonder if things have changed over the years?

I don’t really understand whether the OP’s question is related to the language version. If my question is repeated, please forgive me.

Exceptions are not supported. Exceptions require runtime support that is not available in the kernel. That’s what eliminates most of STL.

C++17 and C++20 compiler features that don’t require exceptions should be just fine. However, many of the improvements were in the library, and that’s not going to work. There’s no in the kernel. for example.