Importing outside DLLs into .Net platform and using them By Anupama Atmakur Overview What is a DLL ? Issues in using DLL :DLL hell. Subtle difference between a DLL and a dot net assembly What is an assembly? How to create a DLL? How is a DLL imported into .net? Demo showing how to import a DLL into .net? What is a DLL? “DLL” stands for dynamic-link library. It is an executable file that acts as a shared library of functions. The executable code for the functions are located in a DLL, which are compiled, linked, and stored separately from the processes that use them. What makes it dynamic? DLL is a file that is loaded, and its functions linked dynamically at run time. It allows an executable module to include only the information needed to locate the executable code for a DLL. Where as in static linking, the linker gets all of the referenced functions from the static link library and places it with in your code into your executable. Advantages… Save memory and disk space. Upgrade easier. Provide after-market support. Provide a mechanism to extend the MFC library classes. Support Multilanguage programs DLL Hell problem using DLLs… When a DLL is updated on a computer, the older version is removed and replaced with the newer version. If the new version is not compatible with the previous version, this usually crashes applications that use the component. Solution… The .NET Framework provides support for sideby-side execution, which allows multiple versions of an assembly to be installed on the same computer, at the same time. As a result managed applications can select which version to use without affecting other applications that use a different version. Shift from DLL to Dot Net Assemblies in Microsoft. NET DLL contains library code to be used by any program running on Windows. Dot net assemblies are understandable to only Microsoft. NET and can be used only in .NET managed applications. Assemblies can maintain versioning with the help of the manifest. Giving a Strong name to an assembly is a key to avoid DLL Hell. What is an assembly? Assemblies are the building blocks of .NET Framework applications. They contain the definition of types, versioning information for the type, meta-data, and manifest. They form the fundamental unit of deployment, version control, reuse and security permissions. How to create a DLL? A DLL can be created using the following command line statements: Single-File: csc /out:add.dll /target:library add.cs Multi-File: • csc /out:samlemodule.netmodule /target:module add.cs • al /out:MultiFileAssembly.dll /target:library samlemodule.netmodule Using a DLL… Using command line statement csc /target:library /reference:add.dll program.cs Using GUI Strong Name Shared assemblies must have a strong name as it is a unique identifier of an assembly. A strong name consists of the assembly's name, version and culture metadata, plus a cryptographic public key and a digital signature. Public key is used for validation. • • sn -k Keyfile.snk This command creates a new pair of keys and stores it in a filenamed Keyfile.snk. Creating an Assembly Strong Name The following attribute are added to the AssemblyInfo.cs using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyCulture(“en-us")] [assembly: AssemblyVersion(“1.0.0.0")] [assembly: AssemblyKeyFile(" Keyfile.snk")] Sharing an Assembly: The Global Assembly Cache Install an assembly in the GAC gacutil /i mult.dll Remove the assembly from the GAC gacutil /u math Demo to secure a dll using a keyfile. References http://msdn2.microsoft.com/enUS/library/3707x96z(VS.80).aspx http://support.microsoft.com/kb/81506 5 http://www.codeproject.com/dll/The_DL L_Hell.asp Questions??