Simulating mouse input

I am working on a fairly large project where I make devices simulate other devices. In this instance I am trying to simulate mouse movement with a joy stick but my current question is how can I simulate mouse packets. I am using a mouse filter driver because I will also need to read and manipulate mouse packets along with simulating mouse packets. This may be easier if I just made a device function driver that simulates mouse input but I am desperately trying to cut down on the quantity of drivers so being able to shove as much functionality into a single driver is my top priority.

Anyways so far I followed the Microsoft Moufilter Example and I finally was able to successfully make a filter driver that can intercept and view USB mouse input via MouFilter_ServiceCallback but I dont know where to go from here. Just to clarify my goal here is to create some sort of function that has the ability to send real mouse input similar to what the MouFilter_ServiceCallback reports. Later I will then need to call these functions from another driver but that is the second step after I figure this part out.

If you want to send mouse input whenever you want, just call the upper service callback function that is stored in your device extension in whatever context you need. Make sure you call it at DISPATCH_LEVEL as that is the contract.

Given you description of the problem you are solving, a mouse filter might not be the best solution. To send your own mouse input, you should not rely on another mouse being present. Instead, you can write a HID mini port driver (there are samples, including a UMDF version) and just report the mouse data through HID any time you want. The one gotcha is you said you are also manipulating mouse input. Are you manipulating your own simulated mouse input stream or are you truly manipulating other mice input streams?

At the risk of pointing out the obvious, by far the easiest way to inject HID events into the input system is to use the user mode SendInput function, https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput. No kernel programming required.

@Doron_Holan said:
If you want to send mouse input whenever you want, just call the upper service callback function that is stored in your device extension in whatever context you need. Make sure you call it at DISPATCH_LEVEL as that is the contract.

Given you description of the problem you are solving, a mouse filter might not be the best solution. To send your own mouse input, you should not rely on another mouse being present. Instead, you can write a HID mini port driver (there are samples, including a UMDF version) and just report the mouse data through HID any time you want. The one gotcha is you said you are also manipulating mouse input. Are you manipulating your own simulated mouse input stream or are you truly manipulating other mice input streams?

How would I go about doing that? For example inside of the MouFilter_EvtIoInternalDeviceControl function it assigns MouFilter_ServiceCallback to the PCONNET_DATA struct. Then Windows handles calling that call back when mouse input arrives so how would I call it my self? The callback is just assigned to a PVOID pointer inside of the PCONNET_DATA struct. How exactly do I correctly call it? Also will do I will make sure its only called at DISPATCH_LEVEL.

This line in MouFilter_ServiceCallback calls the upper service callback to report the data.

    (*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
        devExt->UpperConnectData.ClassDeviceObject,
        InputDataStart,
        InputDataEnd,
        InputDataConsumed
        );

you can invoke (*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService) in any function you want outside of MouFilter_ServiceCallback

@Doron_Holan said:
This line in MouFilter_ServiceCallback calls the upper service callback to report the data.

    (*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService)(
        devExt->UpperConnectData.ClassDeviceObject,
        InputDataStart,
        InputDataEnd,
        InputDataConsumed
        );

you can invoke (*(PSERVICE_CALLBACK_ROUTINE) devExt->UpperConnectData.ClassService) in any function you want outside of MouFilter_ServiceCallback

Yea that makes sense but how should I get the DeviceObject? I assume I should create a function like the following. Creating a new instance of PMOUSE_INPUT_DATA and PULONG should be easy but how do I get the PDEVICE_OBJECT struct? I know it should not be a function parameter so please disregard that but I am not sure how to get PDEVICE_OBJECT since its only ever passed in as a parameter to the ServiceCallback function. That object is normally passed into the call back function by windows so how should I go about doing it my self?

VOID Manual_Callback(IN PDEVICE_OBJECT DeviceObject) {
    PDEVICE_EXTENSION devExt;
    WDFDEVICE hDevice;

    hDevice = WdfWdmDeviceGetWdfDeviceHandle(DeviceObject);
    devExt = FilterGetData(hDevice);

    (*(PSERVICE_CALLBACK_ROUTINE)devExt->UpperConnectData.ClassService)
        (devExt->UpperConnectData.ClassDeviceObject, InputDataStart, InputDataEnd, InputDataConsumed);
}