A Beginner's Tutorial on Creating a DLL
DLL stands for "Dynamic-link Library", in this tutorial, I am going to show you how to create a DLL to store methods that you may use many times in different applications.
First, open visual studio (or any other .NET IDE), and create a new project, you will want to select "Class Library" as your project type, once you have created your project, the code shown should look something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstDLL
{
public class Class1
{
}
}
(I used the name 'myfirstDll' for the project name.)
It is actually rather simple to add methods to this, let's say I want to include a function that shows how many characters are in a string (this snippet), first we would create a public static string GetCharacters(string input), this would be the method used. Note that the method declaration is like a normal one, but with the added static parameter.
Note: you must declare the method as 'public' or else it will not work right!
Next, we would add our description to the method:
/// <summary>
/// Counts the number of times a character appears in a string.
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns></returns>
public static string GetCharacters(string input)
{
}
Now that we have that set up, we will add the code (this is just from the snippet, but modified to work with a DLL!)
/// <summary>
/// Counts the number of times a character appears in a string.
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns></returns>
public static string GetCharacters(string input)
{
string[,] alphanumerics = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } };
string result = "";
int FI = 0;
int LI = 0;
int counts = 0;
for (int i = 0; i < 35; i++)
{
//Get the lower-case characters first//
FI = input.IndexOf(alphanumerics[0, i]);
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i], LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i] + "'s.\n";
}
//Count upper-case characters//
FI = input.IndexOf(alphanumerics[0, i].ToUpper());
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i].ToUpper(), LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i].ToUpper() + "'s.\n";
}
counts = 0;
}
return result;
}
Now, we are done with the DLL! With it all completed, it should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyFirstDLL
{
public class Class1
{
/// <summary>
/// Counts the number of times a character appears in a string.
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns></returns>
public static string GetCharacters(string input)
{
string[,] alphanumerics = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } };
string result = "";
int FI = 0;
int LI = 0;
int counts = 0;
for (int i = 0; i < 35; i++)
{
//Get the lower-case characters first//
FI = input.IndexOf(alphanumerics[0, i]);
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i], LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i] + "'s.\n";
}
//Count upper-case characters//
FI = input.IndexOf(alphanumerics[0, i].ToUpper());
while (FI > -1)
{
counts++;
LI = FI + 1;
FI = input.IndexOf(alphanumerics[0, i].ToUpper(), LI);
}
if (counts > 0)
{
result += "There are " + counts + " " + alphanumerics[0, i].ToUpper() + "'s.\n";
}
counts = 0;
}
return result;
}
}
}
Compile it and start another project, but this time a windows forms app or a console application.
Calling the methods in the DLL from another program
When we need to call that function, we will just need to add a reference to that DLL in our project (the project that will need to access the methods in the DLL).
To do this, (in VS), click Project->Add References on the menu bar, when the 'add references' dialog appears, click 'browse', select the compiled DLL (Usually in the 'bin/release' folder under the DLL Project files) and click OK.
Once this is done, you can call the method like this:
Console.WriteLine(MyFirstDLL.Class1.GetCharacters("The quick brown fox jumped over the lazy Dog!"));
// OR:
MessageBox.Show(MyFirstDLL.Class1.GetCharacters("Using DLLs is easy!"));
Assuming you have correctly built the DLL, it should write out the count of each characters in the string!
And that's it! Using DLL to store methods in this manner is actually quite easy with C#!
I hope this tutorial was useful, and thanks for reading
- AJ32






MultiQuote








|