Registering and unregistered a .net dll using regasm

Wednesday, 7 May 2014

Registering and unregistered a .net dll using regasm

Registering and unregistered a .net dll  using regasm



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.
Microsoft class reference :  RegAsm.exe utility.
RegAsm.exe file comes with .Net framework installation and can be found in Microsoft.NET framework folder. There are different versions of RegAsm.exe.
Note : Use Set path if you are trying to register dll compiles in older versions :
SET PATH "%PATH%;%SystemRoot%\Microsoft.NET\Framework\v4.0.30319 (change versions of .net framework here)
or use batch file
path = %path%;C:\Windows\Microsoft.NET\Framework\v4.0.30319
regasm myProject\bin\Debug\myDLL.dll /tlb:myDLL.tlb /codebase
pause

Batch File to Un-register and register

Source Code
  1. commands statements
  2. @echo off
  3. echo Please enter to un-register the components first
  4. Pause
  5. :: Unregister
  6. "C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" "C:\xpst\XPST.dll" /tlb:XPST.tlb /codebase /u
  7. echo Please enter to register HtmlAgilityPack and XPST COM
  8. pause
  9. :: Register
  10. "C:\Windows\Microsoft.NET\Framework\v4.0.30319 \RegAsm.exe" "C:\xpst\XPST.dll" /tlb:XPST.tlb /codebase
  11. echo enter to close command tool batch file
  12. pause
    1. .Net framework 2.0, 3.0, and 3.5 use the same RegAsm.exe which locates in the .Net framework V2.0 folder.C:\WINNT\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe OrC:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
    2. .Net framework 4.0 uses a new RegAsm.exe which locates in the .Net framework V4.0 folder.C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe OrC:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe
    RegAsm.exe and DLL mapping
    If you receive this error “RegAsm : error RA0000 : Failed to load ‘c:\winnt\system32\YourDLLFile.dll’ because it is not a valid .NET assembly”, you are probably trying to use a .Net framework 2 RegAsm.exe to register a DLL that is created by using .Net framework 4.
    .Net Framework RegAsm.exe default installation path Your DLL should be created by
    .Net framework 2.0/3.0/3.5 C:\WINNT\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe .Net framework 2.0/3.0/3.5
    .Net framework 4.0 C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe .Net framework 4.0
    So when registering DLL assemblies that are created by .Net framework 4, we must NOT use RegAsm.exe that comes in .Net framework 2.0/3.0/3.5 folder.
    How to run RegAsm.exe
    To execute RegAsm.exe, open a command prompt window, and navigate to the folder where RegAsm.exe is located and run it (otherwise you will get “RegAsm is not recognized as internal or external command, operable program or batch file” error message).
    Assume I have already added my DLL to folder C:\WINNT\system32 then I can run the following command:
    C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe myDLL.dll /codebase
    Note that you don’t need to specify C:\WINNT\system32 in the command as it’s a system folder. RegAsm.exe will automatically look up myDLL.dll in C:\WINNT\system32 directory.
    The /codebase parameter is an optional parameter that adds information about the DLL to the Windows registry which specifies the assembly’s path on the disk.
    Regasm can also be used to unregister a DLL.
    If the DLL you got does not have type library file associated with it, one can be generated by using the Regasm utility and the /tlb option.
    C:\WINNT\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe myDLL.dll /tlb:myDLL.tlb
    Note that, to export a type library from the DLL, you need administrator privileges on the computer, or you will receive this type of error “RegAsm : error RA0000 : An error occurred while saving the exported type library: Access is denied…” because the account under which you run regasm.exe doesn’t have rights to write to the folder.
    Note that you can create environmental variables for the .Net framework RegAsm.exe to myDLL DLL registration.
    Process of registering and GAC-ing my dll
    gacutil /i MyDll.dll
    I copied the most recent versions of gacutil.exe and RegAsm.exe (with their corresponding .config files) into this directory.
    Next, I added a file into that directory called RegDll.cmd. This file will register the dll with its tlb and then install it in the GAC. This is useful when needing to consume an assembly in the VB6 development environment via COM interop.
    after registering the dll this message will shown
     Microsoft .NET Framework Assembly Registration Utility version 4.0.30319.17929 for Microsoft .NET Framework version 4.0.30319.17929 Copyright (C) Microsoft Corporation. All rights reserved. RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can ca use your assembly to interfere with other applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed assemblies. Please give your assembly a strong name and re-register it. Types registered successfully Assembly exported to 'path where the dll exist', and the type library was registered successfully
    Enjoy…