sveerap
sveerap

Reputation: 841

How to call a COM component in C

I have to call a COM component from C done in Visual Studio. I am able to call it from Visual C++ using the '#import' directive by specifying the .tlb file. But I don't see #import directive in the C language. Is there an alternative I can use instead of that?

I cannot write in C++ as I have to create the module in pure C only.

Upvotes: 3

Views: 2219

Answers (2)

michal-mad
michal-mad

Reputation: 432

Ok, you should (in brief):

  1. If you would like to call COM created from .NET then you should prepare those classes in the right way (GUID, ComVisible etc.)
  2. Generate appropriate headers for C (OleView.exe can do that)
  3. In C use those stub files to call ctor's, properties, methods etc.

Here it is the complete, step by step solution for your question: http://www.codeproject.com/Articles/632616/How-to-use-NET-Csharp-COM-objects-in-plain-C

Upvotes: 1

sharptooth
sharptooth

Reputation: 170489

The type library in that component likely comes from compiling a IDL file by MIDL. MIDL produces three files - .tlb, .c and .h. .c and .h files contain interface definitions for C and C++ - there's a gazillion of #ifdef __cplusplus to provide identical definitions - one set is for C and another one is for C++.

You have to get those .c and .h files and include into your project. If you have the COM component sources - get those files after building the component. If it's shipped by a third party - contact them and ask them to publish those files.

Upvotes: 0

Related Questions