c++xinput

What is the 3rd parameter meant to look/be like XInputGetBatteryInformation()


Sorry if I messed up this post or something, my first time ever posting on Stackoverflow, I'd usually ask a discord server for help but they don't do C++, I'm pretty new to C++ and I am trying to develop a Bakkesmod plugin, it uses XInput, the idea is that it updates you when the controller battery level changes, though I'm not sure how I would get the battery information for it, it mentions to send a pointer but that's a bit tricky for me, I'd love if someone explained it. API Reference


Solution

  • You need to create a variable of type XINPUT_BATTERY_INFORMATION and pass a pointer to it.

    XINPUT_BATTERY_INFORMATION my_battery;
    XInputGetBatteryInformation(user_index, dev_type, &my_battery);
    // Now my_battery contains the battery information
    

    If you or I were writing this function in a high-level language, we would probably write something more like

    XINPUT_BATTERY_INFORMATION XInputGetBatteryInformation(DWORD, BYTE);
    

    i.e. we would return the structure. However, what you see here is a common idiom in old C code, such as the WinAPI (And yes, the WinAPI is a C library, which you happen to be calling from C++ code). In older C code, functions that want to return structures larger than a word will often take an output parameter of that type instead.

    DWORD XInputGetBatteryInformation(DWORD, BYTE, XINPUT_BATTERY_INFORMATION*);
    

    (the DWORD return value is an error code)

    This is for a couple of reasons. These functions are designed to be used in low-level environments, so by providing the caller with the ability to control where the result goes, we give them more control over their memory management, rather than forcing them to potentially copy a value several times to get it where it needs to be.