Automatically generate C# wrapper class from dll in Visual Studio 2010 Express?
Asked Answered
S

3

26

I was told by a colleague of mine that Visual Studio allows one to point to a .dll and auto-magically generate a C# wrapper class. Is this really possible? And if so, how does one go about achieving this? I've browsed the web, but have failed to come up with anything!

Thanks all!


Figured I'd share these resources as well,

Sall answered 26/7, 2012 at 12:6 Comment(4)
possible duplicate of Tool for creating .NET wrappers for a COM DLL?; also see Auto wrap c++ dll into c#Arrowy
I suspect they were talking about the Type Library Importer (tlbimp.exe) that Hans mentions in his answer. I'm not sure though, I'm assuming you're dealing with a COM DLL, and that isn't exactly built into VS Express...Arrowy
@CodyGray, Yeah, I want to communicate with a device using the dlls of the installed driver.Sall
What kind of dll are you talking about? COM, plain c or what?Levey
R
38

3 cases:

  1. The DLL represents a managed assembly => you directly reference it in your project and use it
  2. The DLL represents a COM object => you could use the tlbimp.exe utility to generate a managed wrapper
  3. The DLL represents an unmanaged library with some exported functions. That's the toughest one. There are no tools. You will have to consult the documentation of the library to know the exported function names and parameters and build managed P/Invoke wrappers. You could use the dumpbin.exe utility to see a list of exported functions. Here's an article on MSDN about the different steps.
Reynolds answered 30/7, 2012 at 12:19 Comment(0)
D
8

This certainly isn't possible with any DLL. Just a very specific kind, one that implements a COM server. The converter needs a good description of the exported types, that's provided for such servers by a type library.

A type library is the exact equivalent to metadata in a managed assembly. While it starts life as a standalone file, a .tlb file, it often gets embedded as a resource in the DLL. Good place for it, keeps the type descriptions close to the code that implements it. Just like the metadata in a .NET assembly.

Some tooling to play with to see type libraries (not sure if it works in Express): in Visual Studio use File + Open + File and pick, say, c:\windows\system32\shell32.dll. You'll see the resources in that DLL, note the TYPELIB node. That's the type library. It is binary so actually reading it isn't practical. For that, run OleView.exe from the Visual Studio Command Prompt. File + View Typelib and select the same DLL. That decompiles the type library back into IDL, the Interface Description Language that was originally used to create the type library. Highly readable, you'll have little trouble understanding the language. And can easily see how the .NET Tlbimp.exe can translate that type library into equivalent C# declarations.

Type libraries are old, they have been around since 1996. Originally designed by the Visual Basic team at Microsoft, as a replacement for VBX, the 16-bit VB extensibility model. They have been very successful, practically any Windows compiler supports them. But they are limited in expressive power, there is no support for things like generics and implementation inheritance. Notable is that the Windows 8 team has replaced type libraries for WinRT. They picked the .NET metadata format.

Donall answered 30/7, 2012 at 14:4 Comment(0)
T
0

I know this question is fairly old and seems to have been answered sufficiently, but I just want to add one thought I think might be important. I could be totally wrong, so please take my answer with a grain of salt and correct me on this if I am.

To be able to call members/fields in a DLL, the information needed to call them must be accessible in some form. That information should be all you need to write a wrapper. With that, you can determine all members/fields "form" aka method headers and so on.

In C# it is possible to load DLLs via reflection and get that information. I dont know about different DLL-Types as described above, but as I said, to call the members/fields this information has to be there in some form. So using reflection to get that Information, you could generate a new class e.g. "prefixOriginalname" and have it have the same members/fields as your original class, calling the members/fields of your original class and adding your desired extra functionality.

So

  1. Every DLL (or peripheral document) gives you the information need to use its types. Namely everything that is implemented as "public"
  2. You can access this needed information via reflection
  3. Given 1. and 2., you can create a program to extract the needed information from DLL and generate wrappers accordingly.

As I said, I am not 100% sure on this one, because the other answers make it sound to me like that might be too difficult or even impossible for some reason.

Tract answered 18/7, 2018 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.