Monday, March 9, 2015

Difference between regasm and regsrv32, tlbimp.exe,tlbexp.exe


Regasm.exe(Register Assembly)
regasm.exe is used to create COM Callable Wrapper (CCW) around .NET assemblies. .NET managed assemblies(EXEs, DLLs) are different from COM DLLs (which are unmanaged, ie. they interact with the OS directly). So to register an unmanaged DLL you use regsvr32.exe.

But if you have a managed .NET assembly and want you COM components to use it as if it were a COM assembly, then you need to use regasm.exe.It can generate a .tlb file given the assembly only - it inspects the type infromation stored in the assembly and includes the COM-exposed entities into the type library.

“Regasm.exe, the Assembly Registration tool that comes with the .NET SDK,  reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. Once a class is registered, any COM client can use it as though the class were a COM class. The class is registered only once, when the assembly is installed. Instances of classes within the assembly cannot be created from COM until they are actually registered.“


Regsvr32
regsvr32 will load the library and try to call the DllRegisterServer() from that library. It doesn't care what DllRegisterServer() actually does - it just calls that function and checks the returned value. You use it to register COM servers in unmanaged DLLs. It can't generate a .tlb file.To register the .dll for the Active Directory Schema

"Regsvr32 is the command-line tool that registers dynamic-link libraries (dlls) and ActiveX controls in the registry.
Tlbexp.exe
The Type Library Importer converts the type definitions found within a COM type library into equivalent definitions in a common language runtime assembly. The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library. You can examine this file with tools such as Ildasm.exe.

Tlbexp.exe generates a type library that contains definitions of the types defined in the assembly. Applications such as Visual Basic 6.0 can use the generated type library to bind to the .NET types defined in the assembly.The entire assembly is converted at once. You cannot use Tlbexp.exe to generate type information for a subset of the types defined in an assembly.

The entire assembly is converted at once. You cannot use Tlbexp.exe to generate type information for a subset of the types defined in an assembly.

You cannot use Tlbexp.exe to produce a type library from an assembly that was imported using the Type Library Importer (Tlbimp.exe). Instead, you should refer to the original type library that was imported with Tlbimp.exe. You can export a type library from an assembly that references assemblies that were imported using Tlbimp.exe.

Tlbexp.exe generates a type library but does not register it. This is in contrast to the Assembly Registration tool (Regasm.exe), which both generates and registers a type library. To generate and register a type library with COM, use Regasm.exe.

Type Library Importer(Tlbimp.exe)
Tlbimp.exe (Type Library Importer) is used to create a Runtime Callable Wrapper around a COM component so that the unmanaged COM library can be used in .NET.
When do we use Tlbimp and not regasm or regsvr32?

Tlbimp.exe performs conversions on an entire type library at one time. You cannot use the tool to generate type information for a subset of the types defined within a single type library.

TlbExp.exe and Regasm.exe tools aid us in exporting assembly information to a type library so that non .NET Applications or unmanaged code use this type library information to call .NET assembly.
Just like tlbimp which works on the entire COM Component tlbexp works on the entire assembly.
The entire assembly is converted at the same time. You cannot use it to generate type information for a subset of the types.
Tbexp only generates the type library information for an assembly but it does not register the type libraries unlikeregasm.exe

Regasm.exe creates a type library as well as registers it whereas tlbexp.exe only creates a CCW but does not register it.
Tlbimp.exe (Type Library Importer) is used to create a Runtime Callable Wrapper around a COM component so that the unmanaged COM library can be used in .NET.
Let us now create an assembly for which we will create a type library and use it through Visual Basic.
  1. Open a new dotnet project (Class Library)
  2. Add a class to the project called MyClass as below
    public class MyClass 
  3. public MyClass() 
  4. public int Add (int i , int j)
  5.  { return i +j;
  6.  } 
  7. public int Mul (int i , int j) 
  8. { return i *j; } }
  9. Strong name your assembly, compile the project and create the assembly
  10. Go to the Visual Studio command prompt and type tlbexp /? to explore all the options. I have enlisted them below:
    /out:FileName File name of type library to be produced 
  11. /nologo Prevents TlbExp from displaying logo
  12.  /silent Prevents TlbExp from displaying success message
  13.  /verbose Displays extra information 
  14. /names:FileName A file in which each line specifies the captialization of a name in the type library. 
  15. /? or /help Displays this usage message
  16. To create a typelibrary from it type the following command tlbexp .dll e.g.tlbexp tlbexp.dll
  17. If the export was successful a success message diplaying the entire path and the name of the .tlb will be shown to you
    Assembly exported to F:\Net\tlbexp\bin\Debug\tlbexp.tlb
  18. You can also use the /verbose option to check out the details of the classes that were exported
  19. Although our typelibrary is created, our assembly is not yet registered. You can register the assembly using regasm tool
  20. Go to the Visual Studio command prompt and type regasm /? to explore all the options. I have enlisted them below:
    /unregister Unregister types /tlb[:FileName] Export the assembly to the specified type library and register it 
  21. /regfile[:FileName] Generate a reg file with the specified name instead of registering the types. This option cannot be used with the /u or 
  22. /tlb options 
  23. /codebase Set the code base in the registry 
  24. /registered Only refer to already registered type libraries 
  25. /nologo Prevents RegAsm from displaying logo 
  26. /silent Silent mode. Prevents displaying of success messages 
  27. /verbose Displays extra information 
  28. /? or /help Display this usage message
  29. To only register the assembly, use the following command: regasm .dll e.g. regasm TlbExp.dll
  30. You can also create a .reg file containing all the registry entries: regasm TlbExp.dll /regfile:myTlbExp.dll .reg
  31. You also create typelibrary from assembly using regasm tool by using /tlb option
  32. To use this component from VB 6.0 put the assembly in GAC. You can do this by either using gacutil or just dragging and dropping the assembly in /winnt/assembly folder
  33. You can now use this assembly from VB 6.0
  34. Open a VB std project. Go to references. Select the .tlb file that we generated from the list
  35. Yes, you can now use your .NET assembly from VB 6.0. The sample code of using your .NET assembly from VB:
    Dim tlbExp As tlbExp.MyClass 
  36. Set tlbExp = New tlbExp.MyClass
  37. Label1.Caption = tlbExp.Add(1, 2)

No comments:

Followers

Link