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
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.
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.
- Open a new dotnet project (Class Library)
- Add a class to the project called
MyClass
as below - {
- public MyClass()
- {
- }
- public int Add (int i , int j)
- { return i +j;
- }
- public int Mul (int i , int j)
- { return i *j; } }
- Strong name your assembly, compile the project and create the assembly
- Go to the Visual Studio command prompt and type
tlbexp /?
to explore all the options. I have enlisted them below: - /nologo Prevents TlbExp from displaying logo
- /silent Prevents TlbExp from displaying success message
- /verbose Displays extra information
- /names:FileName A file in which each line specifies the captialization of a name in the type library.
- /? or /help Displays this usage message
- To create a
typelibrary
from it type the following commandtlbexp
e.g..dll tlbexp tlbexp.dll
- If the export was successful a success message diplaying the entire path and the name of the .tlb will be shown to you
- You can also use the
/verbose
option to check out the details of the classes that were exported - Although our
typelibrary
is created, our assembly is not yet registered. You can register the assembly using regasm tool - Go to the Visual Studio command prompt and type
regasm /?
to explore all the options. I have enlisted them below: - /regfile[:FileName] Generate a reg file with the specified name instead of registering the types. This option cannot be used with the /u or
- /tlb options
- /codebase Set the code base in the registry
- /registered Only refer to already registered type libraries
- /nologo Prevents RegAsm from displaying logo
- /silent Silent mode. Prevents displaying of success messages
- /verbose Displays extra information
- /? or /help Display this usage message
- To only register the assembly, use the following command:
.dll e.g. regasm TlbExp.dll - You can also create a .reg file containing all the registry entries:
- You also create
typelibrary
from assembly using regasm tool by using/tlb
option - 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 - You can now use this assembly from VB 6.0
- Open a VB std project. Go to references. Select the .tlb file that we generated from the list
- Yes, you can now use your .NET assembly from VB 6.0. The sample code of using your .NET assembly from VB:odeDim tlbExp As tlbExp.MyClass
- Set tlbExp = New tlbExp.MyClass
- Label1.Caption = tlbExp.Add(1, 2)
No comments:
Post a Comment