All [i] computin [/i] will have you ever meet these nebulae libraries, probably get to hate them but the truth is that Visual Studio is an amazing experience to work with them. Let's see step by step how erect a library that step will help us to manage one of the conflicting Windows stuffed, as they are the installers
Such as in times of old, dirty and adorable (sniff!) Visual Basic (not of the makeup and elegant C# to Visual Basic .NET now call) create libraries with Visual Studio (in particular with the 2005 version or higher) is really simple and useful. A library encapsulates a series of elements that can be consumed from almost any source, so that copying it and pasting it directly might make you a series of libraries that you will use from different applications and you could reuse code even more easy, versatile and neatly.
In short, a library will be namespace, such as the space that encapsulates all the elements of our main application, the library will contain its own elements, classes, structures, enumerations, and more.
The current article is intended to show the basis of the creation and use of libraries DLL (class library) however the pretext of this tutorial will be create an application that will allow us to manage our applications (especially the list of add or remove programs).
The idea is to create a modern program that will allow us to "remove" elements installed with the security of being able to restore them later. The application simply obscure records that refer to the facilities at will of the user.
The library shall administer all actions with the registry, providing lists of installers and routines necessary to hide them and restore them. Thus our application will be just the same interface, to check it also I will implement one executable command line that will use the same library.
Getting started with the library
To begin with our library the only thing we have to do is select "class library" as a new project.
At this moment, we will create a structure that will represent an installer and a list which will be of the same filtering options.
To then use the library into another project, simply we add it as a "Reference" to our new project:
So we could use it as if it were a class, first calling it by its name, i.e.:
The code shown in the article is written in a simplified manner.
Then our structure is similar to:
Filtering options have a particularity:
The secret behind the powers of two
In the previous enumeration, the [Flags] attribute which has a close relation with the powers of two is above the Declaration of the same.
This relationship is often used in all the libraries of the system and although in this particular case it has one greater significance, it is important to explain its function. Imagine a text properties:
We could also combine their properties:
Then how could we now call a function that applies text formatting?
But, and if you would like to just use a variable?:
Normal would be to make a table for each of the combinations, by associating them with a number, indeed we would get something like this:
Now, returning to the first proposal, would have to do two powers here?:
Do you notice?, values that are powers of two are unique characteristics that define only a bit, instead composite are the sum of characteristics only. So when specific [Flags] in the enumeration you am indicating the compiler to try this way the elements of the same enumeration, thus with OR can generate configurations composed, obviously:
Then we already have the main definitions, now would be the body of the code. As proof I will only show one of the most relevant:
Also, secretly, I wanted to also show them how they can invoke other builders to avoid having to repeat several times the same classic assignments builders, in fact, like that in other languages, if the constructor is empty you could omit and compile the class anyway.
Information about the facilities
To access information about the uninstallers listed in the Add / Remove Programs, I rely on the following registry keys:
In this way, that I will make will only take the subkeys in search of all those that contain a specific record, called "DisplayName", generally, although it is customizable.
So the code is quite explicit:
Hiding facilities right?
An important part of the process will be hiding facilities (in fact, is the main purpose of the program). This task has a secret, Visual Studio does not directly provide functions to copy nor then move complete registry branches have to work first in implementing this functionality and can then hide facilities.
Do we need to move or copy a full registry branch to hide an installation?
Because it is the easiest way to do so, the other possibility is remove the privileges of the key reading, however this could bring serious consequences later.
So what I'll do simply will be to create a key sister to the container for the key that you want to "hide" and I will move the complete branch towards the 'sister', so also can restore the hidden installers easily repeating the process in reverse.
Copying and moving branches complete the registration
The task of copying and moving are very similar, in fact are the same, the only detail is that when you move, copy and delete the copied content. This similarity is very present in the users of Linux, especially those accustomed to using the system terminal.
In this way we could cover this procedure, using the command "reg" very dirty, export, modify and reimport the registry to copy it. These solutions are good for a goal, but our application will be a work, we will learn and so here is the next brushstroke on our canvas in Ethereal:
Do you understand?, I first open the keys of origin and target, then I copy all the current route registry keys. For subkeys I again call the same function of the parameters of the same subkey copy, repeating the process, recursively.
Now, to move a key is the same but removing the key to origin (more tracks its implementation within the code).
After the small break education, we can now enter full to hide the applications installed on the keys mentioned above.
First, when the user "uncheck" Installer, we will call our library which uses an extremely simple function:
And now comes the hard part... I lied!, there are no tricky part, just a bit of logic and as always a satisfactory result.
Well and... would the library?
Clear, clear, we did a library which at the moment is serving the same function as if had been a class within the original program... so what is grace?
Let's see, we create another project in the Visual Studio solution, now a console application:
In the same way we refer to our library (if not the library not outside the same solution would have to refer to the DLL).
Now use the library from our console application!, what could be more elegant?:
Tiring, isn't it so?
Code and executable
The preview for the undecided:
By the way, previously not commented them on the possibility of personalizing this small software using the configuration file that includes, it is indeed not so interesting to see it in the Visual Studio Designer:
We could in fact get to hide almost any registration key. I think the ideal would be a little more work on the same and achieve "universal", but clear, will not be for this episode.
Another example of the entertaining and productive programming in this environment, especially by the funny thing that might be for someone related to computing this world of libraries that Windows introduces us, particularly with less force than before with the famous hell of the DLLs.
Clearly our library is far from being a product professional and optimal, even away from the low level presenting the system libraries, however, does not remove the same utility.