Accessing COM Objects Using C

Any COM interface method can be called from a C program. There are two things you need to remember when calling an interface method from C:

·The first parameter of the method is always a reference to the object that has been created and is invoking 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 IDirectDraw::CreateSurface method using the C programming language:

ret = lpDD->lpVtbl->CreateSurface (lpDD, &ddsd, &lpDDS,

NULL);

The DirectDraw object associated with the new surface is referenced by the lpDD parameter. Incidentally, this method fills in a surface description structure (&ddsd) and returns a pointer the new surface (&lpDDS).

To call the IDirectDraw::CreateSurface method, you first dereference the DirectDraw object's vtable, 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 is invoking 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 vtbl pointer and passes the this pointer):

ret = lpDD->CreateSurface(&ddsd, &lpDDS, NULL)