3

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.

3
  • possible duplicate of How to use tlb file in C? Commented Dec 17, 2010 at 3:42
  • Is there a reason you can't use C++ in your module. There shouldn't be any compatibility problems. Commented Dec 17, 2010 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 Commented Dec 17, 2010 at 5:45

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Whole article is the relevant portion. Did you look at there?
(you did not quote anything, so there was no need to compare against linked content)
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.

4 Comments

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: 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.
@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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.