Search code examples
cwindowscom

Is there a way to use/call COM interfaces in C?


MSDN has reference that C language can also be used to create and use COM objects.

I went through CodeProject here and found that relevant header files are created and they will be included in whichever C application that calls these COM interfaces.

Will all COM objects from Windows will have similar relevant header files?

Or is there any other way to call COM interfaces from C ?


Solution

  • Will all COM objects from Windows will have similar relevant header files?

    All header files generated from a Microsoft Interface Definition Language file using the MIDL compiler will contain definitions for use by C programs. See C/C++-Compiler Considerations:

    The generated .h file has both C-style and C++-style definitions for interfaces.

    I don't know if all COM objects that ship with Windows or the Windows SDK come with pre-built headers for use by C programs, but it certainly would be reasonable to assume so. In case you find an interface for which there is no C-style interface definition, you can always run the MIDL compiler against the .idl file.

    Or is there any other way to call COM interfaces from C ?

    Some COM interface implementations also come as type libraries (.tlb). You can #import the TLB into your C++ code. This generates a primary header file (.tlh) you could use to reconstruct a C-style interface definition.