Home > OS >  How to use actually use abstract classes/interface in Windows API?
How to use actually use abstract classes/interface in Windows API?

Time:10-29

I'm aware that there's no actually built-in concepts of interfaces in C , so in order to implement it one must use abstract classes which only contains pure virtual functions.

Now, In Microsoft Windows' API list, some of the classes there like enter image description here

Thinking that I need to sub-class the Interfaces, I tried the following code below but it seems that I'm wrong about the sub-classing:

#include <iostream>
#include <propidl.h>
#include <objidl.h>

class PropertyStorage : IPropertyStorage {};

int main(int argc, char* argv[]){

    PropertyStorage ips();   <- function returning abstract class "PropertyStorage" is not allowed

}

Having said those things, I'd like to reiterate

My question is: How do you actually use the interfaces from Microsoft Windows' API?

CodePudding user response:

How do you actually use the interfaces from Microsoft Windows' API?

That's easy: You acquire a pointer to an interface, and start using it. And when done, you Release() it. That's COM in a nutshell.

On to the harder question then: How do you actually get hold of a COM interface pointer? Essentially, there are two ways to do so:

  1. Call a factory function that supplies a COM object through an interface pointer. The "standard" way is by calling CoCreateInstance. The less "standard" (albeit increasingly common) way is to call a dedicated factory function. To get an IPropertyStorage interface you can call StgCreatePropStg (or similar), for example.

    This addresses the use case where you are consuming a COM interface that the system (or a library) provides for client use.

  2. Implement the interface, and use whatever means you see fit to instantiate this concrete implementation. When using a COM-capable C compiler (such as MSVC) this amounts to providing an implementation for a class that derives from the interface.

    This is useful for cases where you need to author an interface, generally to be consumed by someone else. Examples include IStream or IEnumString. This is not the case for the interfaces asked for in the question.

  • Related