11

I'm trying to capture an event and reference a GUID to see which event it is. The code is below:

DWORD WINAPI AvisionEventProc(LPVOID lpParam){
    //HANDLE hEvent = * (HANDLE *) lpParam;   // This thread's read event
    STINOTIFY pStiNotify;

    if (debug){
        wprintf(L"Avision Event\n");
    }
    while(true){
        WaitForSingleObject(hAvisionEvent, INFINITE);
        wprintf(L"Event");
        pStiDevice->GetLastNotificationData(&pStiNotify);
        if (pStiNotify.guidNotificationCode == GUID_STIUserDefined1){
            wprintf(L"User defined 1");
        }else if (pStiNotify.guidNotificationCode == GUID_STIUserDefined2){
            wprintf(L"User defined 2");
        }else if (pStiNotify.guidNotificationCode == GUID_STIUserDefined3){
            wprintf(L"User defined 3");
        }

        ResetEvent(hAvisionEvent);
    }
    return 1;
}

This compiles just fine but I get the errors below when linking:

1>sti.obj : error LNK2001: unresolved external symbol _GUID_STIUserDefined3
1>sti.obj : error LNK2001: unresolved external symbol _GUID_STIUserDefined2
1>sti.obj : error LNK2001: unresolved external symbol _GUID_STIUserDefined1

The strange thing is that sti.h is linked in as I am pulling other constants from it. I did notice by the GUID decleration the following:

#if defined( _WIN32 ) && !defined( _NO_COM)

/*
 * Class IID's
 */

// B323F8E0-2E68-11D0-90EA-00AA0060F86C
DEFINE_GUID(CLSID_Sti, 0xB323F8E0L, 0x2E68, 0x11D0, 0x90, 0xEA, 0x00, 0xAA, 0x00, 0x60, 0xF8, 0x6C);

/*
 * Interface IID's
 */

// {641BD880-2DC8-11D0-90EA-00AA0060F86C}
DEFINE_GUID(IID_IStillImageW, 0x641BD880L, 0x2DC8, 0x11D0, 0x90, 0xEA, 0x00, 0xAA, 0x00, 0x60, 0xF8, 0x6C);

<snip>

/*
 * Standard event GUIDs
 */

// {740D9EE6-70F1-11d1-AD10-00A02438AD48}
DEFINE_GUID(GUID_DeviceArrivedLaunch, 0x740d9ee6, 0x70f1, 0x11d1, 0xad, 0x10, 0x0, 0xa0, 0x24, 0x38, 0xad, 0x48);

<snip>

#endif

Does the "if defined" line stop the GUIDs being referenced (I am writing a win32 console app) or is there something more fundamental I have wrong here to do with a lack of understanding on GUIDs?

Thanks, in advance for your help.

Cheers,

Neil

Neil Benn
  • 904
  • 1
  • 12
  • 31
  • I think generally GUIDs are declared in the header files but defined in an external library. To fix the problem you just need to link the library into your program. Another option is to include before including that header file; this changes the DEFINE_GUID macro to actually define the GUIDs inline rather than as extern. – Luke Jun 11 '12 at 14:01

2 Answers2

43

#include <initguid.h>should be added. That will help.

Alex K
  • 546
  • 5
  • 2
  • worked, eventhough it was marked as possibly unused include directive – serup Jun 06 '18 at 15:03
  • Note that it should be added in one and only one compilation unit, otherwise you'll start getting multiple definition errors. – Ben Voigt Oct 08 '20 at 16:25
  • @ben The GUIDs are defined as constants with external linkage. I believe those can be introduced into any number of compilation units without causing ODR violations, so long as they all agree. – IInspectable Oct 08 '20 at 19:05
  • @IInspectable: External linkage doesn't create an exception to the rule "Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement", and while being constant sometimes results in being odr-unused, GUIDs frequently are passed by address. – Ben Voigt Oct 08 '20 at 19:40
  • @IInspectable: The reason that it ends up being permitted is that `initguid.h` adds the `DECLSPEC_SELECTANY` macro to the definition of `DEFINE_GUID`. – Ben Voigt Oct 08 '20 at 19:41
9

The DEFINE_GUID macro either defines a named GUID as a static, or just does a forward declaration to actually be done somewhere else, depending on if #include <initguid.h> has been previously declared. Your code perhaps only have the latter, and the symbols don't have actual initialization within the project.

See:

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Roman R.
  • 68,205
  • 6
  • 94
  • 158