Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

Reg-Free of .NET Components

1.00/5 (1 vote)
8 Sep 2015CPOL2 min read 8.3K  
This tip will describe each step involved in the implementation of Reg-Free approach of .NET Component.

Introduction

I will describe each step involving Reg-free approach that eliminates explicit registration of .NET Component when it is used by application built by something other than .NET Framework, i.e., C++, Powerbuilder Application, etc.

Background

The main theme of Reg-Free approach is to provide sufficient information to Client Application at launch time by using Microsoft Manifest technology.

Using the Code

The main components of Reg-free approach are listed below:

  1. .NET Class Library Project's Class
  2. Component Manifest file
  3. Resource file
  4. Make DLL
  5. Client Application manifest

1) .NET Class Library Project's Class

Make a class library project in the .NET framework and take two guid from online guid generator tool. Assign one guid at namespace & second at class.

Note: Don't build project. Otherwise an entry with this class id will be created on COM server and conflict with classid of DLL you make by command later.

2) Component Manifest File

Create file with test.manifest extension and add the following settings:

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
       <assemblyIdentity
                    name="myOrganization.myDivision.myManagedComp"
                    version="1.2.3.4" />
                    publicKeyToken="8275b28176rcbbef"
       />
       <clrClass
                    clsid="{65722BE6-3449-4628-ABD3-74B6864F9739}"
                    threadingModel="Both"
                    name="myManagedComp.testClass1">
       </clrClass>
</assembly>

3) Resource File

Create file with .rc extension and add the following commands:

#include <windows.h>
#define MANIFEST_RESOURCE_ID 1
MANIFEST_RESOURCE_ID RT_MANIFEST  test.manifest

Now compile this test.rc with the following command by using Visual Studio command prompt.

rc   test.manifest

This will create test.rec with embedded test.manifest.

4) Make DLL

Open command Prompt, set directory to C:\Windows\Microsoft.NET\Framework\v2.0.50727, and run the following command:

C:\Windows\Microsoft.NET\Framework\v2.0.50727   
vbc /t:library /out:C:\\test.dll /win32resource:C:\\test.res /rootnamespace:yournamespace Class1.vb

The above command will create .dll at the path you mentioned in command (in our case, path root directory is C:\\).

5) Client Application Manifest

Create another app.manifest file and it should look like this:

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity type="win32" 
                    name="myapp" 
                    version="1.0.0.0" 
                    processorArchitecture="x86" 
                    
  />
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" 
                    name="mynamespace.class" 
                    version="1.0.0.0" 
                    processorArchitecture="X86" 
      />
    </dependentAssembly>
  </dependency>
</assembly>

At this stage, you have completed all steps. Now, it is time to use DLL component alongwith client application and application manifest. Place DLL, client application (.exe), and client application manifest at the same path.

Summary

When client application launches, by default, it checks .manifest file having the same name as launching app. If found, it reads dependency node in .manifest file and loads dependency by using name & version values. Internally, it looks inside COM server and search classid.

Drawback

Whenever relevant dependency DLLs are not correctly produced by reg-free approach, the client application will fail to access that dependent DLL and the whole app will not be able to launch. The solution to this drawback is to simply rename manifest or remove that dependency in manifest file.

Advantage

No need to explicitly register .NET DLL which will not be successfully registered when permissions are not assigned to system login-ed user.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)