Search code examples
c++cwindowsinputdirectinput

DirectInput Unresolved External Symbol


This one is driving me nuts. I've tried everything I can think of. Here are the relevant parts of the DirectInput code.

BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo,   VOID* Context);

if(DirectInput8Interface == DI_OK)
{
  DirectInput8InterfacePointer->EnumDevices(
    DI8DEVCLASS_GAMECTRL,
    (LPDIENUMDEVICESCALLBACKA) EnumDevicesCallback,
    NULL,
    DIEDFL_ATTACHEDONLY); 
}

When I try to compile, I get the error:

unresolved external symbol "int __stdcall EnumDevicesCallback(struct DIDEVICEINSTANCEA const *,void *)" (?EnumDevicesCallback@@YGHPBUDIDEVICEINSTANCEA@@PAX@Z) referenced in function _WinMain@16.

As you can see, the external symbol the compiler can't find is related to the DIDEVICEINSTANCE parameter of the EnumDevicesCallback function. That shouldn't be, because I've included dinput.h and linked to dinput8.lib and dxguid.lib. I even tried defining DIDEVICEINSTANCE in my own code and got a message that it conflicted with a previous definition.

What could that error message mean?


Solution

  • That is not how callbacks work.

    EnumDevicsCallback is not a function that exists. You're supposed to write your own function that EnumDevices will call for each device. Your function doesn't have to be called EnumDevicesCallback - that's an example.

    For example, if you just wanted to print the name of each device, you might write

    BOOL CALLBACK PrintDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo,   VOID* Context)
    {
        _tprintf("%s %s\n", DeviceInfo->tszProductName, DeviceInfo->tszProductName);
        return DIENUM_CONTINUE;
    }
    

    and then pass PrintDevicesCallback to EnumDevices.