Skip navigation.

Ambassador

The Royal C++ Embassy

Posts tagged with "uuidof"

"I cannot find import library for my GUIDs!"

, , ,

Visual C++ has got a keyword, specifically designed for COM projects; the "__uuidof". So, for example, if you want to instantiate a class with CLSID_Application and you need ISomeInterface, you'd normally do:
ISomeInterface *psi;
psi = NULL;

HRESULT hr = CoCreateInstance(CLSID_Application, NULL, 
                              CLSCTX_INPROC_SERVER,
                              IID_ISomeInterface,
                              (void**)&psi);

If you get a link error for IID_ISomeInterface and CLSID_Application despite the fact that you have essentially tried to link all import libraries suggested in any piece of documentation, try:
HRESULT hr = CoCreateInstance(__uuidof(Application),
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              __uuidof(ISomeInterface),
                              (void**)&psi);

Trick is that this operator returns GUID of given expression, if any provided. Of course, I am talking about "normal" scenerios, like:
#if defined(__cplusplus) && !defined(CINTERFACE)
  MIDL_INTERFACE("00000000-0000-0000-C000-000000000046")
  IUnknown
  {
  public:
    BEGIN_INTERFACE
    virtual HRESULT STDMETHODCALLTYPE QueryInterface
            (REFIID riid, void **ppvObject) = 0;
    virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0;
    virtual ULONG STDMETHODCALLTYPE Release(void) = 0;
    END_INTERFACE
  };
#else   /* C style interface */
  typedef struct IUnknownVtbl
  {
    BEGIN_INTERFACE
    HRESULT (STDMETHODCALLTYPE *QueryInterface)(IUnknown *This,
             REFIID riid, void **ppvObject);
    ULONG (STDMETHODCALLTYPE *AddRef)(IUnknown *This);
    ULONG (STDMETHODCALLTYPE *Release)(IUnknown *This);
    END_INTERFACE
  } IUnknownVtbl;

  interface IUnknown
  {
    CONST_VTBL struct IUnknownVtbl *lpVtbl;
  };
#endif

The "MIDL_INTERFACE" macro specifies GUID for the interface (which is IID_IUnknown).
Or (stolen from Microsoft's sample, see critics below):
//  expre_uuidof.cpp 
//  compile  with:  ole32.lib 
#include  "stdio.h" 
#include  "windows.h" 

[emitidl]; 
[module(name="MyLib")]; 
[export] 
struct  stuff  { 
    int  i; 
}; 

 int  main()  { 
    LPOLESTR  lpolestr; 
    StringFromCLSID(__uuidof(MyLib),  &lpolestr); 
    wprintf_s(L"%s",  lpolestr); 
    CoTaskMemFree(lpolestr); 
}

Found a few problems with the sample:
1. C or C++ code cannot be compiled with a lib, but can be linked. So instead of "compile with", it should be "link with"
2. #include "stdio.h" does not seem good. It's supposed to be #include <stdio.h> (mind <>, please)

Or, alternatively, if you import:
#import "SomeLibrary.dll" no_namespace raw_interfaces_only

In fact, as far as I remember, when you import, you can specify "named_guids" option to import directive, which will generate CLSID_SomeStuff in type library header. However, I don't remember whether this requires import library at link stage.
Download Opera, the fastest and most secure browser
December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31