How to use tlb file in C?

2019-02-27 16:06发布

tlb file(using regasm) is made from a dll(using c# code), and i can use this in c++ using #import. and everything is working fine. Is there a way, i can use this in C language? I just found that #import is c++ specific. So, can someone please tell me how can i use in my C program?

my main intention is to use COM DLL developed in C# in my C program.

Thanks & Rgds, ~calvin

标签: c# c dll com import
4条回答
成全新的幸福
2楼-- · 2019-02-27 16:40

Neither C nor C++ have an #import preprocessor directive.

查看更多
唯我独甜
3楼-- · 2019-02-27 16:49

COM programming in C is very painful, but not impossible. The buck stops here though. The point of a type library is to have a tool auto-generate the COM interface and co-class declarations so that you can use them in your code. Quite similar to a .h file, but language independent. The .NET equivalent is the metadata in an assembly.

Problem is, the tooling isn't available to convert a .tlb to C declarations. I'm sure you are familiar with #import, that's what gets used in MSVC. But it generates C++ code, smart pointers that help you create the COM object, call its interface methods and deal with errors. If there is a tool available that generates C then it is a very well hidden secret.

One trick jumps to mind, you can use OleView.exe, File + View TypeLib to view the contents of the type library. This view is decompiled into IDL, the interface definition language. You can copy and paste this text into an .idl file and compile it with midl.exe with the /header command line option. This generates a .h file that contains both C++ and the C declarations for the interfaces. Ought to get you close, just make sure that the type library is reasonably stable so you don't have to do this very often.

查看更多
\"骚年 ilove
4楼-- · 2019-02-27 16:50

You can use regasm with /tlb option to register the types in windows registry. After that, you can create your instances like regular COM calls from C++ code.

From MSDN:

When you specify the /tlb option, Regasm.exe generates and registers a type library describing the types found in the assembly. Regasm.exe places the generated type libraries in the current working directory or the directory specified for the output file. Generating a type library for an assembly that references other assemblies may cause several type libraries to be generated at once. You can use the type library to provide type information to development tools like Visual Studio 2005. You should not use the /tlb option if the assembly you are registering was produced by the Type Library Importer (Tlbimp.exe). You cannot export a type library from an assembly that was imported from a type library. Using the /tlb option has the same effect as using the Type Library Exporter (Tlbexp.exe) and Regasm.exe, with the exception that Tlbexp.exe does not register the type library it produces. If you use the /tlb option to registered a type library, you can use /tlb option with the /unregister option to unregistered the type library. Using the two options together will unregister the type library and interface entries, which can clean the registry considerably.

查看更多
Ridiculous、
5楼-- · 2019-02-27 16:51

I also faced the same problem as I need to use tlb file in C. And I think you can check the below link which helps

How to call a COM component in C

Thanks, sveerap

查看更多
登录 后发表回答