2

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.

  • possible duplicate of How to use tlb file in C? – Joe Dec 17 '10 at 3:42
  • In one word: horrible. – ruslik Dec 17 '10 at 3:45
  • Is there a reason you can't use C++ in your module. There shouldn't be any compatibility problems. – shf301 Dec 17 '10 at 4:04
  • The module which I have to develop has to be in C only as this c module will in turn has to be integrated into a legacy ERP product UNify Vision – sveerap Dec 17 '10 at 5:45
0

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.

  • Thanks for the response. I have few questions. Can you please clarify. The COM component is .Net component which is developed internally. So can I use the MIDL on the .tlb file to generate .h and .c files?. Also, do I need to do any additional linking or will the compiler automatically links the required COM registered in the registry?. – sveerap Dec 17 '10 at 10:48
  • @sveerap: I didn't think it could be a .NET component. I guess in this case you're out of luck. The way I described will only work once you have a .idl file and compile it with MIDL. You could still try to use the technique described here: stackoverflow.com/questions/4312711/how-to-use-tlb-file-in-c/… but it'll be very painful. – sharptooth Dec 17 '10 at 10:56
  • @sharptooh : The approach you have suggested worked .I could create a sample hello world application which communicates from C to C#. I generated the IDL from .tbl file usign oleviewer and then used MIDL to generate the .h and .c file . Using them in the project, I could run it succesfully. The below link provided help on this. faculty.cascadia.edu/mpanitz/com_tutorial/comclientinc.html – sveerap Dec 18 '10 at 16:56
  • @sveerap: Great! I didn't think of doing those steps in sequence primarily because you'll have to redo them once the component interface changes which is not very convenient. Good it works for you. – sharptooth Dec 20 '10 at 6:57
0

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

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.