Introduction
This article demonstrates the installation of Side-By-Side assembly execution using native dll(s). Also demonstrates how to consume these sxs native dll(s) from MC++.
Implementing SxS assemblies
Here I will consider the requirement to be 1) creating a MC++ wrapper to a native dll and 2)call the MC++ wrapper from C# as the framework is built in native C++. Below are the steps to do it.
1.Create a native dll, build a assembly manifest and a catalog file.
2.Install the native dll, manifest and catalog to WinSxS using Windows Installer.
3.Create a MC++ dll, extract the assembly identity from the native dll’s manifest and specify it for Additional Manifest Dependencies property under Linker option. Sign this assembly and place it in the GAC.
4.Create a C# executable (.exe) and add reference to the MC++ assembly from the GAC. Built the application and run the C# executable.
Creating Manifest and Catalog files
1.Create a Win32 or MFC DLL with Visual C++ and name it as “UnmanagedCPP.dll”.
2.Get a pair of certificate and private key say “mycert.cer” and “mycert.pvk”. As a requirement for WinSxS, the certificate key must be at least 2048 bits.
makecert -pe -ss MY -$ individual -n ”CN= certificate_name” -len 2048 –r
Store the .cer, .pvk and the .dll file in the same folder.
If a certificate with 2048 bits key is available then install it to the system and export it, choose “Yes, export the private key” option then a .pfx file could be created and stored. Once this is done you can ignore step 7.
3.Using Visual Studio 2005 Command Prompt issue the following command to obtain the publicKeyToken from the .cer certificate:
pktextract mycert.cer
The output would be as below:
Microsoft ® Side-By-Side Public Key Token Extractor 1.1.3.0
Copyright (C) Microsoft Corporation 2000-2002. All Rights Reserved
Certificate: ”CATry-Catch” - 2048 bits long
publicKeyToken=”4b762499fc143588”
4.Use Notepad to create a manifest file "UnmanagedCPP.manifest", with the following content:
<?xml version=”1.0” encoding=”UTF-8” standalone=”yes”?>
<assembly xmlns=”urn:schemas-microsoft-com:asm.v1” manifestVersion=”1.0”>
<assemblyIdentity type=”win32” name=”UnmanagedCPP” version=”1.0.0.0”
processorArchitecture=”x86”; publicKeyToken=”4b762499fc143588”/>
<file name=”UnmanagedCPP.dll” hashalg=”SHA1”/>
</assembly>
Public key token extracted from the previous step have to be inserted into the manifest.
5.Manifest file should be updated with a hash value, update it using the below command:
mt.exe -manifest UnmanagedCPP.manifest -hashupdate -makecdfs
The manifest file “UnmanagedCPP.manifest” will contain the hash for the DLL “UnmanagedCPP.dll”.
The -makecdfs option generates a file named “UnmanagedCPP.manifest.cdf” that describes the contents of the security catalog that will be used to validate the manifest.
6.A verification catalog must be built, execute the below command to build one:
makecat UnmanagedCPP.manifest.cdf
7.Use the “pvkimprt” utility to generate a “.pfx” file with the “.pvk” and “.cer” file:
pvkimprt -pfx mycert.cer mycert.pvk
Name the PFX format file as “mycert.pfx”.
8.Sign the catalog with the certificate using SignTool as shown below:
signtool sign /f mycert.pfx /p password_for_private_key /du
http:
http:
Please replace "password_for_private_key" with the actual password.
The 3 files required for deploying the shared Side-By-Side Assembly would be as below:
UnmanagedCPP.dll
UnmanagedCPP.cat
UnmanagedCPP.manifest
Creating Install file
9. These files are deployed to the WinSxS using Windows Installer:
• Use Visual Studio .Net to create a Setup project. Add the 3 files above into the project and build the project.
• Use Orca to open the built MSI file for further editing.
• The “File” table contains 3 rows, each’s “FileName” column points to one of the 3 files namely to “UnmanagedCPP.dll”, “UnmanagedCPP.cat” and “UnmanagedCPP.manifest”. The rows for “UnmanagedCPP.cat” and “UnmanagedCPP.manifest” have to edited, by replacing their “Component_” column value with the value from the “Component_” column of the “UnmanagedCPP.dll” row. This effectively assigns the 3 files into the same component originally used by “UnmanagedCPP.dll”. Please take a note of the 2 component names (e.g. the original values from the “Component_” column of “UnmanagedCPP.manifest” and “UnmanagedCPP.cat” rows) being replaced.
• The “Component” table contains 3 rows. Delete the 2 rows whose “Component” columns values corresponds to files “UnmanagedCPP.manifest” and “UnmanagedCPP.cat”.
• Browse the “FeatureComponent” table, repeat step 4 and delete the unnecessary component rows for “UnmanagedCPP.manifest” and “UnmanagedCPP.cat” files.
• Browse the “MsiAssembly” table, add a new row:
• Component_: The value taken from the “File” table, “Component” column for “UnmanagedCPP.dll”, “UnmanagedCPP.manifest” or “UnmanagedCPP.cat” (these 3 “Component” columns should contain the same value after step 3)
• Feature_: DefaultFeature(This is the only feature name in Visual Studio 2005/2003 Setup Project. It can also be found from the “Feature” table or “FeatureComponent” table.)
• File_Manifest: The value taken from the “File” table, “File” column for the file “UnmanagedCPP.manifest
• File_Application:
• Attribute: 1
• Browse the “MsiAssemblyName” table, add 5 rows, whose “Component_” columns are all the value taken from the “File” table, “Component” column for “UnmanagedCPP.dll”. The “Name” columns and “Value” columns of the 5 rows are taken from the content of the manifest file:
Name ----- Value
type: win32
name: UnManagedCPP
version: 1.0.0.0
processorArchitecture: x86
publicKeyToken: 4b762499fc143588
• Save the MSI file and exit Orca. This updated MSI file should be able to install “UnmanagedCPP.dll” as side by side shared assembly into the WinSxS folder.
Creating MC++ dll referencing native dll's manifest
10. Create ManagedCPP.dll using UnmanagedCPP.lib and UnmanagedCPP.h and specify the additional manifest reference property under Linker option with the assembly identity of UnmanagedCPP.manifest. Sign the ManagedCpp.dll and install it to GAC.
Creating C# code referencing MC++ dll
11. Create CSharp.exe referencing ManagedCPP.dll.
Note: To generate a manifest file make use of any available tools as it is easy to use and configure to build a suitable type of manifest file.