0

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?

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
deSTRUCTor
  • 13
  • 1
  • 1
    Are you using a C or C++ compiler? They are not the same. – Alan Stokes Jan 28 '16 at 20:29
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Stokes Jan 28 '16 at 20:30
  • You sure you have provided definition of function `BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo, VOID* Context);` in your program? – Angelus Mortis Jan 28 '16 at 20:35
  • 1
    Note: Casting a function pointer is always wrong. `(LPDIENUMDEVICESCALLBACKA) EnumDevicesCallback,` should not have the cast there - if it needs to cast to compile, then fix it so it doesn't. – user253751 Jan 28 '16 at 20:48
  • @Alan: not a duplicate, see immibis's answer. – Harry Johnston Jan 28 '16 at 21:02

2 Answers2

2

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.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

I dare say, the culprit is BOOL CALLBACK EnumDevicesCallback(const DIDEVICEINSTANCE* DeviceInfo, VOID* Context); - the function is likely a C-function, but you declare it in your .cpp file as C++-function.

Instead of doing that, include a proper .h file with the declaration, which is likely to have correct extern specified.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • ::slaps forehead:: Doh! I've only ever seen the callback function in example form before. It hadn't occurred to me that I needed to write my own. Thanks to everyone who pointed that out, it solved the compile problem. – deSTRUCTor Jan 30 '16 at 03:55