C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Compiling source files and building assemblies from command prompt


Posted Date: 21 May 2004    Resource Type: Articles    Category: .NET Framework
Author: Gaurav sainiMember Level: Bronze    
Rating: 1 out of 5Points: 10



Creating and Using DLL in Csharp

A dynamic linking library (DLL) is linked to your program at run time. To
demonstrate building and using a DLL, consider the following scenario:

  • FirstLib.DLL: The
    library file that contains the methods to be called at run time. In this
    example, the DLL contains two methods, Add and Multiply.
  • Add.cs: The source
    file that contains the method Add(long i, long j).
    It returns the sum of its parameters. The class AddClass
    that contains the method Add is a member of the
    namespace MyMethods.
  • Mult.cs: The source
    code that contains the method Multiply(long x, long y).
    It returns the product of its parameters. The class MultiplyClass
    that contains the method Multiply is also a member
    of the namespace MyMethods.
  • UseFirstLib.cs: The file
    that contains the Main method. It uses the methods
    in the DLL file to calculate the sum and the product of the run-time
    arguments.



Source files

File: Add.cs

// Add two numbers
using System;
namespace MyMethods
{
public class AddClass
{
public static long Add(long i, long j)
{
return(i+j);
}
}
}


File: Mult.cs

// Multiply two numbers
using System;
namespace MyMethods
{
public class MultiplyClass
{
public static long Multiply(long x, long y)
{
return (x*y);
}
}
}


File: UseFirstLib.cs

// Calling methods from a DLL file
using System;
using MyMethods;
class UseFirstLib
{
public static void Main(string[] args)
{
Console.WriteLine("Calling methods from FirstLib.DLL:");
if (args.Length != 2)
{
Console.WriteLine("Usage: UseFirstLib <num1> <num2>");
return;
}
long num1 = long.Parse(args[0]);
long num2 = long.Parse(args[1]);
long sum = AddClass.Add(num1, num2);
long product = MultiplyClass.Multiply(num1, num2);
Console.WriteLine("The sum of {0} and {1} is {2}",
num1, num2, sum);
Console.WriteLine("The product of {0} and {1} is {2}",
num1, num2, product);
}
}


This file contains the algorithm that uses the DLL methods, Add
and Multiply. It starts with parsing the arguments
entered from the command line, num1 and num2.

Then it calculates the sum by using the Add method on
the AddClass class, and the product by using the Multiply method on the MultiplyClass class.


Notice that the using directive at the beginning of the file allows
you to use the unqualified class names to reference the DLL methods at compile
time, for example:


MultiplyClass.Multiply(num1, num2); 

Otherwise, you have to use the fully qualified names, for example:


MyMethods.MultiplyClass.Multiply(num1, num2); 


Compilation

To build the file FirstLib.DLL, compile the two files Add.cs and Mult.cs using
the following command line:


	csc /target:library /out:FirstLib.DLL Add.cs Mult.cs

The /target:library compiler option tells the compiler
to output a DLL instead of an EXE file. The /out
compiler option followed by a file name is used to specify the DLL file name.
Otherwise, the compiler uses the first file (Add.cs) as
the name of the DLL.


To build the executable file, UseFirstLib.exe, use the
following command line:


	csc /out:UseFirstLib.exe /reference:FirstLib.DLL UseFirstLib.cs

The /out compiler option tells the compiler to output
an EXE file and specifies the name of the output file (UseFirstLib.exe).
This compiler option is optional. The /reference
compiler option specifies the DLL file(s) that this program uses.



Execution

To run the program, enter the name of the EXE file, followed by two numbers,
for example:


	UseFirstLib 1234 5678

Output


Calling methods from FirstLib.DLL:
The sum of 1234 and 5678 is 6912
The product of 1234 and 5678 is 7006652


/target:library Option

The /target:library option causes the compiler to create a
dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be
created with the .dll extension.


Unless otherwise specified with the /out
option, the output file name takes the name of the first input file.


When specified at the command line, all files up to the next /out, /target:winexe,
or /target:exe
option are used to create the .dll file.


When building a .dll file, a Main
method is not required.


To set this compiler option in the Visual Studio
development environment


  1. Open the project's Property Pages dialog box.
  2. Click the Common Properties folder.
  3. Click the General property page.
  4. Modify the Output Type property.



Example

Compile in.cs, creating in.dll:



csc /target:library in.cs





Responses

Author: K K Venkatesh    07 Jul 2004Member Level: Bronze   Points : 0
Really good one. it is very helpul.


Author: abhishek    20 Oct 2004Member Level: Bronze   Points : 0
i have used microsoft.web.ui.controls.dll in my .cs file now when I compile it using command prompt it gives me an error
could not find library microsoft.web.ui.controls.dll

please help me urgently


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Working with embedded resource in dot net assembly.
Previous Resource: Garbage Collection(GC) - An introduction
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use