Using Packages to Build Assemblies

If you know how to create Object Pascal units and compile an application, you already know all you need to know to create assemblies using packages. Using the IDE Wizard to create a new package and adding a new unit to that package generates a file similar to the one shown in Listing 6.1. This file is viewable by right-clicking the package node in the Project Manager and selecting View Source from the local menu.

LISTING 6.1 A Sample Package File

8: {$EXTENDEDSYNTAX ON}

9: {$IMPORTEDDATA ON}

11: {$LOCALSYMBOLS ON}

12: {$LONGSTRINGS ON}

13: {$OPENSTRINGS ON}

14: {$OPTIMIZATION ON}

15: {$OVERFLOWCHECKS OFF}

16: {$RANGECHECKS OFF}

17: {$REFERENCEINFO ON}

18: {$SAFEDIVIDE OFF}

19: {$STACKFRAMES OFF}

20: {$TYPEDADDRESS OFF}

21: {$VARSTRINGCHECKS ON}

22: {$WRITEABLECONST OFF}

23: {$MINENUMSIZE 1}

24: {$IMAGEBASE $400000}

25: {$IMPLICITBUILD OFF} 26:

27: requires

28: borland.delphi;

30: [assembly: AssemblyDescription('')]

31: [assembly: AssemblyConfiguration('')]

32: [assembly: AssemblyCompany('')]

33: [assembly: AssemblyProduct('')]

34: [assembly: AssemblyCopyright('')]

35: [assembly: AssemblyTrademark('')]

39: // The Delphi compiler controls the AssemblyTitleAttribute via the

40: // ExeDescription. You can set this in the IDE via the Project Options.

41: // Manually setting the AssemblyTitle attribute below will override the

LISTING 6.1 Continued

42

//

43

//

44

45

46

//

47

//

lvalues:

48

//

49

//

50

//

51

//

52

//

53

//

54

//

55

//

56

57

[as

58

59

//

60

//

61

//

62

//

63

//

64

//

65

//

66

//

67

//

68

//

69

//

70

//

71

//

72

//

73

//

74

//

75

//

76

//

77

//

78

//

79

//

80

//

81

//

82

//

83

//

84

//

85

//

86

[as

87

[as

88

[as

89

// Version information for an assembly consists of the following four

Major Version Minor Version Build Number Revision as shown below:

assembly: AssemblyVersion('1.0.*')]

In order to sign your assembly you must specify a key to use. Refer to the Microsoft .NET Framework documentation for more information on assembly signing.

Use the attributes below to control which key is used for signing.

Notes:

If no key is specified, the assembly is not signed. KeyName refers to a key that has been installed in the Crypto Service Provider (CSP) on your machine. KeyFile refers to a file which contains a key.

If the KeyFile and the KeyName values are both specified, the following processing occurs:

  • 1) If the KeyName can be found in the CSP, that key is used.
  • 2) If the KeyName does not exist and the KeyFile does exist, the key in the KeyFile is installed into the CSP and used.

In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. When specifying the KeyFile, the location of the KeyFile should be relative to the project output directory. For example, if your KeyFile is located in the project directory, you would specify the AssemblyKeyFile attribute as

[assembly: AssemblyKeyFile('mykey.snk')], provided your output directory is the project directory (the default). Delay Signing is an advanced option - see the Microsoft .NET Framework documentation for more information on this.

Find the code on the CD: \Code\Chapter 06.

Examining this file from the top down, we can break it down as follows:

  • The package directive dictates that this module is a package assembly project (line 1).
  • The requires clause lists which assemblies are required by this package (lines 27-28).
  • The contains clause lists the units that will be compiled into this assembly (not shown in this listing).
  • Several assembly attributes allow the user to enter various bits of information about this assembly that will ultimately be placed in the manifest (lines 30-36).
  • The AssemblyTitle and AssemblyVersion assembly attributes (line 43 and line 57, respectively) enable the user to control this assembly's title and version number.
  • Finally, there are a few more assembly attributes that provide control over how the assembly is signed for strong naming. More on this is discussed shortly (lines 86-88).

NOTE

Irrespective of what assemblies are contained in the package's requires clause, all packages implicitly require the mscorlib.dll assembly.

Table 6.1 lists and describes the types of package-specific files based on their file extensions.

TABLE 6.1 Package Files

File Extension

File Type

Description

.dpk

Package source file

This file is created when you invoke the Package Editor. You

can think of this as you might think of the .dpr file for a

Delphi project.

.pas

Unit source file

A package consists of one or more Object Pascal units

containing the shared code.

.bdsproj

Project settings file

File used by the IDE to store project settings.

â–  cfg

Project configuration file

Configuration file generated for use by command line compiler.

.dcpil

IL compiled package

This is the IL compiled version of the package that

symbol file

contains the symbol information for the package and its units.

.dcuil

Compiled unit

An IL compiled version of a unit contained in a package. One

.dcuil file will be created for each unit contained in the

package.

.dll

Assembly

This is the final assembly file that contains code usable by any

.NET programming language.

Listings 6.2 and 6.3 show the .dpk and .pas files that make up a package assembly. This package will be used by other applications shortly.

LISTING 6.2 D8DG.TestPkg: A Test Package Project

1: package D8DG.TestPkg; 2:

CHAPTER 6 Assemblies: Libraries and Packages LISTING 6.2 Continued

{$ALIGN 0} {$ASSERTIONS ON} {$BOOLEVAL OFF} {$DEBUGINFO ON} {$EXTENDEDSYNTAX ON} {$IMPORTEDDATA ON} {$IOCHECKS ON} {$LOCALSYMBOLS ON} {$LONGSTRINGS ON} {$OPENSTRINGS ON} {$OPTIMIZATION ON} {$OVERFLOWCHECKS OFF} {$RANGECHECKS OFF} {$REFERENCEINFO ON} {$SAFEDIVIDE OFF} {$STACKFRAMES OFF} {$TYPEDADDRESS OFF} {$VARSTRINGCHECKS ON} {$WRITEABLECONST OFF} {$MINENUMSIZE 1} {$IMAGEBASE $400000} {$IMPLICITBUILD OFF}

requires Borland.Delphi;

contains

D8DG.PkgUnit in 'D8DG.PkgUnit.pas'

  • assembly: AssemblyDescription('')]
  • assembly: AssemblyConfiguration('')
  • assembly: AssemblyCompany('')]
  • assembly: AssemblyProduct('')]
  • assembly: AssemblyCopyright('')]
  • assembly: AssemblyTrademark('')]
  • assembly: AssemblyCulture('')]
  • assembly: AssemblyVersion('1.0.*')]
  • assembly: AssemblyDelaySign(false)]
  • assembly: AssemblyKeyFile('')]
  • assembly: AssemblyKeyName('')] end
  • Find the code on the CD: \Code\Chapter 06. LISTING 6.3 D8DG.PkgUnit: A Unit Within the Package unit D8DG.PkgUnit; interface

LISTING 6.3 Continued type

TBlandClass = class private

FTheString: string; public function TheMethod: Integer;

property TheString: string read FTheString write FTheString; end;

procedure IAmProcedural;

implementation procedure IAmProcedural; begin

  • do nothing end;
  • TBlandClass }

function TBlandClass.TheMethod: Integer; begin

end.

. Find the code on the CD: \Code\Chapter 06.

Note that the unit contains both a class (line 6) and a standalone procedure (line 14) that is not a member of a class.

Namespaces are created simply by using dot notation on a unit name.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment