InitializeInterface

Updated: April 19, 2023

The InitializeInterface interface allows DLLs to choose interfaces at runtime instead of compile time.

Note: The Unloading interface has been replaced with AODeInitializer. This example uses only the InitializeInterface interface.

The interface itself is a pointer to a (void* (*)(AOInterface_t *)) function. This function is called automatically to set the address of any interface pointer whose initial value was NULL.

Let's say we have an addon that supports two different pieces of hardware. In this case, we want two different HardwareControl interfaces but we want to use the one suitable for the hardware that the user has installed. So, we set the interface pointer for the HardwareControl interface to NULL and create an InitializeInterface interface that returns the correct hardware interface.

static void *InitializeInterface( AOInterface_t *i )
{
    // we only initialize the "HardwareControl" interface
    if ( strcmp( i->name, "HardwareControl" ) != 0 )
        return 0;
    
    // return the HardwareControlA/BInterface if either hardware type is found
    if ( hardware_is_type_a )
        return HardwareControlAInterface;
    else if ( hardware_is_type_b )
        return HardwareControlBInterface;
    
    // return 0 if neither piece of hardware is found
    return 0;
}

AOInterface_t my_hardware_interface[] =
{
    { "Name", 0, "my_hardware" },
    { "Description", 0, "Plugin for my hardware" },
    { "HardwareControl", 0, NULL },
    { "InitializeInterface", 0, InitializeInterface },
    ...
    { 0, 0, 0 },
};

The first time an application requests the HardwareControl interface for the above addon, the AOI library sees that the interface pointer is NULL and calls the InitializeInterface() function with the HardwareControl AOInterface_t structure as its parameter. The function recognizes the HardwareControl interface, checks which hardware is available, and returns the appropriate interface pointer.