"I cannot find import library for my GUIDs!"
Sunday, 27. August 2006, 12:00:00
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.









