Accessing COM Objects by Using C
Any COM interface method can be called from a C program. There are two things to remember when calling an interface method from C:
·The first parameter of the method always refers to the object that has been created and that invokes the method (the this argument).
·Each method in the interface is referenced through a pointer to the object's vtable.
The following example creates a surface associated with a DirectDraw object by calling the IDirectDraw2::CreateSurface method with the C programming language:
ret = lpDD->lpVtbl->CreateSurface (lpDD, &ddsd, &lpDDS,
NULL);
The lpDD parameter references the DirectDraw object associated with the new surface. Incidentally, this method fills a surface-description structure (&ddsd) and returns a pointer to the new surface (&lpDDS).
To call the IDirectDraw2::CreateSurface method, first dereference the DirectDraw object's vtable, and then dereference the method from the vtable. The first parameter supplied in the method is a reference to the DirectDraw object that has been created and which invokes the method.
To illustrate the difference between calling a COM object method in C and C++, the same method in C++ is shown below (C++ implicitly dereferences the lpVtbl parameter and passes the this pointer):
ret = lpDD->CreateSurface(&ddsd, &lpDDS, NULL)