Showing posts with label Util. Show all posts
Showing posts with label Util. Show all posts

Monday, 6 February 2012

.NET 3rd party components

http://nuget.org/packages

  1. EntityFramework icon

    EntityFramework 4.2.0.0

    By:

    Entity Framework is Microsoft’s recommended data access technology for new applications.

    188,269 downloads

  2. jQuery icon

    jQuery

    By:

    jQuery is a new kind of JavaScript Library. jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

Monday, 28 February 2011

Merging .NET assemblies using ILMerge

Merging .NET assemblies using ILMerge


Introduction

As you know, traditional linking of object code is no longer necessary in .NET. A .NET program will usually consist of multiple parts. A typical .NET application consists of an executable assembly, a few assemblies in the program directory, and a few assemblies in the global assembly cache. When the program is run, the runtime combines all these parts to a program. Linking at compile time is no longer necessary.

But sometimes, it is nevertheless useful to combine all parts a program needs to execute into a single assembly. For example, you might want to simplify the deployment of your application by combining the program, all required libraries, and all resources, into a single .exe file.

A single project

If all parts of your program are written by yourself in the same language, you can obviously just add all source files to a single project. The result will be a single DLL or EXE containing all dependencies.

Collapse
csc /target:winexe /out:Program.exe
MainProgram.cs ClassLibrary1.cs ClassLibrary2.cs

However, if your program is written in multiple languages or if you are using binary third party libraries, you are out of luck.

.NET Modules

The .NET compilers already contain options for exactly this. If you compile a project, there is an option to create a module, which is similar to an assembly but without a manifest file. You can then use the al.exe tool to combine some of these modules to a single assembly. This feature makes it possible to create a single assembly that contains multiple languages.

First, you would compile the program and the class libraries to netmodules using the module target. Then you can use the assembly linker al.exe to combine these modules to a single assembly.

Collapse
csc /target:module /out:ClassLibrary1.netmodule ClassLibrary1.cs
vbc /target:module /out:ClassLibrary2.netmodule ClassLibrary2.vb
vbc /target:module /out:Program.netmodule Program.vb
al /target:winexe /out:Program.exe ClassLibrary1.netmodule
ClassLibrary2.netmodule Program.netmodule

But unfortunately, this method only works if you have all the required parts of your program either as source code or as .NET modules. If you are useing a third party class library in assembly form, you are again out of luck.

ILMerge

Since a .NET module is basically just an assembly without an assembly manifest, it should be possible to convert an assembly to a .NET module, at least that is what I thought. When researching this on Google, I found a tremendously useful tool on Microsoft research called ILMerge. This little gem makes it possible to link multiple assemblies to a single one.

First, you would compile your libraries to DLLs and your program to an EXE referencing the DLLs. This is exactly what Visual Studio would do if you had multiple libraries and a program referencing these libraries, so there is no need to do this on the command line.

Collapse
csc /target:library /out:ClassLibrary1.dll ClassLibrary1.cs
vbc /target:library /out:ClassLibrary2.dll ClassLibrary2.vb
vbc /target:winexe /out:Program.exe
/reference:ClassLibrary1.dll,ClassLibrary2.dll Program.vb

This will produce a normal .exe that requires the two DLLs in the program directory or in the global assembly cache to run.

Now you can link these parts to a single self-contained EXE, using ILMerge:

Collapse
ilmerge /target:winexe /out:SelfContainedProgram.exe
Program.exe ClassLibrary1.dll ClassLibrary2.dll

The nice thing about this is that you can also merge third party assemblies like commercial class libraries into your program. And you do not have to modify your build process. All you have to do is to merge the assemblies to a single EXE before deploying.

Conclusion

I found ILMerge tremendously useful, and I think that something like this should be a part of the .NET framework SDK. Maybe just enhance al.exe so that it can also link DLLs.

I have only scratched the surface of the .NET build process and the capabilities of ILMerge, and this article might contain many inaccuracies or even errors. But I found ilmerge.exe so useful that I just had to write about it.

Resources

  • ILMerge: The ILMerge utility from Michael Barnett of Microsoft Research.
  • ILMerge Task: A NAnt task for ILMerge.

ILMerge

Brief Description


ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly. It works on executables and DLLs alike and comes with several options for controlling the processing and format of the output. See the accompanying documentation for details.

Download link: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=22914587-b4ad-4eae-87cf-b14ae6a939b0&displaylang=en


Wednesday, 17 September 2008

How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005

How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005

Article ID:828736
Last Review:April 19, 2007
Revision:2.6

SUMMARY

This step-by-step article describes how to call a managed DLL from native C++ code. To do this, create a simple managed DLL by using Microsoft Visual C# .NET or Microsoft Visual C# 2005, and then call this managed DLL from native C++ code.

Back to the top

Requirements

This article assumes that you are familiar with the following topics:
•Writing Component Object Model (COM) clients
•COM interoperability in the Microsoft .NET Framework

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
•Visual C# .NET or Visual C# 2005
•Visual C++ .NET or Visual C++ 2005

Back to the top

Write a Managed DLL

To create a simple managed DLL that has a public method to add two numbers and return the result, follow these steps:
1.Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
2.On the File menu, point to New, and then click Project. The New Project dialog box opens.
3.Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types.
4.Under Templates, click Class Library.
5.In the Name text box, type sManagedDLL, and then click OK.
6.Open the Class1.cs file in Code view.
7.To declare a public interface that has a method to add two numbers, add the following code to the Class1.cs file:
// Interface declaration. public interface ICalculator {     int Add(int Number1, int Number2); };
8.To implement this public interface in a class, add the following code to the Class1.cs file:
// Interface implementation. public class ManagedClass:ICalculator {     public int Add(int Number1,int Number2)         {             return Number1+Number2;         } }
9.Click Start, and then point to Programs.
10.If you are using Microsoft Visual Studio .NET 2003, point to Microsoft Visual Studio .NET 2003, and then point toVisual Studio .NET Tools. If you are using Microsoft Visual Studio .NET 2002, point to Microsoft Visual Studio .NET, and then point to Visual Studio .NET Tools. If you are using Visual Studio 2005, point to Microsoft Visual Studio 2005, and then point to Visual Studio 2005 Tools.
11.If you are using Visual Studio .NET 2003, click Visual Studio .NET 2003 Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio .NET 2002, click Visual Studio .NET Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio 2005, click Visual Studio 2005 Command Promptto open a Visual Studio 2005 command prompt.
12.To create a strong name for your class library, type the following command at the Visual Studio .NET command prompt:
sn.exe -k MyKeyFile.SNK
13.Copy the MyKeyFile.SNK file to your project folder.
14.Double-click the AssemblyInfo.cs file to open the file in Solution Explorer.
15.Replace the following lines of code in the AssemblyInfo.cs file
[assembly: ComVisible(false)] [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")]
with the following.
[assembly: ComVisible(true)]  [assembly: AssemblyDelaySign(false)]  [assembly: AssemblyKeyFile("..\\..\\MyKeyFile.SNK")]
16.Press CTRL+SHIFT+B to generate the managed DLL.

Back to the top

Register the Managed DLL for Use with COM or with Native C++

To use the managed DLL with COM or with native C++, you must register the assembly information of your DLL in the Windows registry. To do this, follow these steps:
1.Click Start, and then point to Programs.
2.If you are using Microsoft Visual Studio .NET 2003, point to Microsoft Visual Studio .NET 2003, and then point to Visual Studio .NET Tools. If you are using Microsoft Visual Studio .NET 2002, point to Microsoft Visual Studio .NET, and then point to Visual Studio .NET Tools. If you are using Visual Studio 2005, point to Microsoft Visual Studio 2005, and then point to Visual Studio 2005 Tools
3.If you are using Visual Studio .NET 2003, click Visual Studio .NET 2003 Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio .NET 2002, click Visual Studio .NET Command Prompt to open a Visual Studio .NET command prompt. If you are using Visual Studio 2005, click Visual Studio 2005 Command Promptto open a Visual Studio 2005 command prompt.
4.At the Visual Studio .NET or Visual Studio 2005 command prompt, change the directory path of the directory that contains your managed DLL.
5.To register the assembly information of your managed DLL in the Windows registry and to create a corresponding type library, run the following command at the Visual Studio .NET command prompt:
RegAsm.exe ManagedDLL.dll /tlb:ManagedDLL.tlb /codebase

Back to the top

Call the Managed DLL from Native C++ Code

To call the managed DLL that you just created from native C++ code, follow these steps:
1.Start Visual Studio .NET or Visual Studio 2005.
2.On the File menu, point to New, and then click Project.
3.If you are using Visual C++ .NET 2003, click Visual C++ Projects under Project Types, and then click Win32 Console Project under Templates. If you are using Visual C++ .NET 2002, click Visual C++ Projects under Project Types, and then click Win32 Project under Templates. If you are using Visual C++ 2005, click Visual C++ underProject Types, and then click Win32 Project under Templates
4.In the Name text box, type CPPClient, and then click OK.
5.If you are using Visual C++ .NET 2003 or Visual C++ 2005, click Finish. If you are using Visual C++ .NET 2002, clickApplication Settings, click Console Application, and then click Finish.
6.Open the CPPClient.cpp file in Code view.
7.To import the type library that RegAsm.exe generates, add the following code to the CPPClient.cpp file:
// Import the type library. #import "..\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only
Change the path of the type library if the path on your computer differs from this path.
8.To declare the namespace to use, add the following code to the CPPClient.cpp file:
using namespace ManagedDLL;
9.To call the managed DLL, add the following code to the _tmain function:
// Initialize COM. HRESULT hr = CoInitialize(NULL);  // Create the interface pointer. ICalculatorPtr pICalc(__uuidof(ManagedClass));  long lResult = 0;  // Call the Add method. pICalc->Add(5, 10, &lResult);  wprintf(L"The result is %d", lResult);  // Uninitialize COM. CoUninitialize(); return 0;
10.Press CTRL+F5 to run the application.

Back to the top

Complete Code Listing

Managed DLL
// Class1.cs // A simple managed DLL that contains a method to add two numbers. using System;  namespace ManagedDLL {  // Interface declaration.     public interface ICalculator     {         int Add(int Number1, int Number2);     };      // Interface implementation.  public class ManagedClass:ICalculator  {        public int Add(int Number1,int Number2)             {                 return Number1+Number2;             }  } }
C++ Client
// CPPClient.cpp: Defines the entry point for the console application. // C++ client that calls a managed DLL.  #include "stdafx.h" #include "tchar.h" // Import the type library.  #import "..\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only using namespace ManagedDLL; int _tmain(int argc, _TCHAR* argv[]) {     // Initialize COM.     HRESULT hr = CoInitialize(NULL);      // Create the interface pointer.     ICalculatorPtr pICalc(__uuidof(ManagedClass));      long lResult = 0;      // Call the Add method.     pICalc->Add(5, 10, &lResult);      wprintf(L"The result is %d\n", lResult);       // Uninitialize COM.     CoUninitialize();     return 0; } 
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample. To do this, follow these steps:
1.Click Project, and then click ProjectName Properties.

Note ProjectName represents the name of the project.
2.Expand Configuration Properties, and then click General.
3.Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler options, visit the following Microsoft Web site:
/clr (Common Language Runtime Compilation)
http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

Back to the top

REFERENCES

For more information about exposing .NET Framework components to COM, visit the following Microsoft Developer Network (MSDN) Web site:

Back to the top


APPLIES TO
•Microsoft Visual C++ 2005 Express Edition
•Microsoft Visual C++ .NET 2003 Standard Edition
•Microsoft Visual C++ .NET 2002 Standard Edition
•Microsoft Visual C# 2005 Express Edition
•Microsoft Visual C# .NET 2003 Standard Edition
•Microsoft Visual C# .NET 2002 Standard Edition
•Microsoft Visual C++ 6.0 Service Pack 5
•Microsoft .NET Framework 1.1
•Microsoft .NET Framework 1.0