|
Using Libraries |
|
|
The Windows controls featured in the .NET Framework are
highly varied and provide all the necessary regular functionality a normal
application would need. They do this through various properties and their
different methods. To enhance their functionality and speed up application
development, you can either create your own new library or use a library
created in another language.
|
If the .NET Framework doesn't have a class you are
looking for, you can create one and be able to use it over and over again in
different programs. You can even create a commercial class and be able to
distribute or sell it. To make this possible, you can "package" one or more
classes in a library. A library is a program that contains classes and/or
other resources that other programs can use. Such a program is created with
the same approach as the programs we have done so far. Because a library is
not an executable, it doesn't need the Main() function. A library
usually has the extension .dll.
A library can be made of a single file or as many files
as necessary. A file that is part of a library can contain one or more
classes. Each class should implement a behavior that can eventually be
useful and accessible to other classes. The classes in a library are created
exactly like those we have used so far. Everything depends on how you
compile it.
To create a library, start by typing its code in a text
file. Once the library is ready, to compile it, at the Command Prompt, you
would type:
csc /target:library NameOfFile.cs
and press Enter. After doing this, a library with the
name of the file and the extension .dll would be created. If you want a
custom name, use the following syntax:
csc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.cs
Using a Visual C++/CLI Library
|
|
One of the most important sought goals in .NET is to
allow different languages to collaborate, such as sharing code. One way this
can happen is to be able to use the functionality of one language into
another. As an illustration, we saw earlier that you could use the rich
library of Visual Basic functions in a C# application. As no library is ever
complete, you may still need functionality that is not easily found.
Furthermore, you may be working with a team of C++ programmers who have
already created a set of functions or complex operations. You should be able
to use that existing code.
In previous years, it used to be a challenge to create a
library, especially in C++. Fortunately, Microsoft Visual C++ now makes it
particularly easy to create one, because a wizard highly assists you. To
create a library, first display the New Project dialog box. After specifying
Visual C++, in the middle list, click Class Library and give it a name. In
the body of the file, you can create the classes and/or functions as you see
fit.
The Microsoft Windows operating system was originally
written in C, the parent language of C++ and C# (also of Java and
JavaScript). To allow programmers to create applications, Microsoft released
a library called Win32. This is a series of functions and classes, etc, that
you previously had to use. As time has changed, you don't need to
exclusively use Win32 anymore to create a Windows application. Nonetheless,
Win32 is still everywhere and it is not completely avoidable because many or
some of the actions you would want to perform in a Windows application are
still available only in Win32. Fortunately, in most cases, it is not always
difficult to use some of these functions in a C# applications, as long as
you observe some rules. Here is an example:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("Kernel32.dll")]
public static extern bool SetConsoleTitle(string strMessage);
static int Main()
{
SetConsoleTitle("C# Programming");
return 0;
}
}
|