问题
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.
回答1:
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.
回答2:
Ok, you should (in brief):
- If you would like to call COM created from .NET then you should prepare those classes in the right way (GUID, ComVisible etc.)
- Generate appropriate headers for C (OleView.exe can do that)
- 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
来源:https://stackoverflow.com/questions/4467439/how-to-call-a-com-component-in-c