A huge concern for me when creating the install application for Serial Key Maker was making the installation process as error proof as possible.
If the installation process fails, or is clunky, my potential clients are never going to see the benefits my application has, and won’t trust it enough to hand over their money for it.
It thus became important for me to reduce the number of dependencies in the project. I wanted to reduce the complexity involved in getting the application working, and in referencing the API from their applications.
Recall, Serial Key Maker is made up of two related products. The GUI which allows the developer to generate product keys and the API which they use in their code to validate the product keys.
I had a logging object that encapsualtes error trapping and general logging functionality. It is used in both the GUI and in the API. I didn’t want to duplicate the logic in both places, so I made a separate DLL that both projects referenced.
+1 for code reuse
-2 for complexity
To reference my API to validate product keys, the developer had to include the logging DLL AND the API DLL into the project. A small thing, but I obcess on making this product SIMPLE to implement, and referencing TWO DLL’s feels like over-complication when one will suffice.
I explored various options:
- Duplicate the code in both places
- Embed the logging DLL into the GUI and the API projects.
There are trade off’s with each solution: duplication makes it more difficult to maintain the code. Using an embedded DLL, while elegant, turns out to be fairly slow performing.
I eventually decided to put the logic for logging into a separate class and use that class in each project. The duplication requires me to be carefull, but I figured it was more important to affect the user less.
I did learn by figuring out how to embed a DLL and get access to its methods, so the effort was not wasted.
This post is a description of how to embed a DLL into a compiled application and then reference it via the application’s resource manifest.
To show how to use an embedded DLL, I have created a project that demonstrates the technique. [Download Embedding Sample Code]
Note: The sample solution was created with VS2008, and is written in VB.Net.
The Code Explained
I have encapsulated most of the code we need to handle
Embedded DLL’s into a shared class. [EmbeddedAssemblyHelper.vb]
Step 1 – Embedd the DLL
The first thing to do is to embed the DLL into your project, and flag it to reside in the application’s resource data. Do the following:
- Right click on your project, and select “Add” and then “Exisiting Item”. Note: Do NOT use the “Add Reference” option
- Navigate to, and select, the DLL you want to embed. The DLL will show up in the file list of your application.
- Select the DLL in Solution Explorer, and open the property page for the DLL.
- The first property is “Build Action”. Change the drop down option to be “Embedded Resource”
Now when you compile your project, the DLL will be built into your binary file for use within your application
Step 2 – Initialize the Event Handler
When we reference the embedded DLL within the project, the framework will go through its regular discovery process to try find the DLL, and it will raise an error – it is not in any of its search locations (GAC, local etc).
We need to set up a handler to intercept this error. To do that, make a call to the “Initialize” method.
Note: You need to call “Initialize” as early in your application’s startup as possible. Also, you cannot reference the embedded DLL in the same method you make the call to “Initialize”.


When the framework raises an error, we intercept it and run the “LoadEmbeddedAssembly” method.
The framework passes the name of the assembly it failed to resolve. We use that name (our Embedded Assembly) and load it manually.
We call out to the EmbeddedAssembly_LoadManually method, which then uses the “GetManifestResourceStream” method to extract the data embedded in our application’s resource data.
We then use that stream to create an Assembly by calling “System.Reflection.Emit.AssemblyBuilder.Load”

Referencing the Embedded DLL
In the sample provided, I made a DLL (“GP_EmbeddedTestDLL”) that has a worker class (“classWorkerTest”). The class contains 3 complex methods that I will use to show the various ways to access the methods ont his embedded DLL
The code examples I found on the web that address this issue only showed the most basic way to call a method – methods that had no parameters, and returned no data.
I have made methods that contain parameters, and in one case returns a user defined data type (“GetPersonObject”).
Step 3.1 – Creating a Reference to the Embedded Assembly
Using reflection we do an Assembly load:

This triggers our event and returns our embedded assembly reference.
Step 3.2 – Create an Instance of the Assembly
Use the CreateInstance method on our assembly to create an instance of our object:

Step 3.3 – Invoke the Method
3.3.1 The first example of a method to call has no paramters and returns a string (“GetCurrentTime”):

3.3.2 The second example of a method to call has a paramter (diameter) and returns a string (“CalculateCircumference”):

3.3.3 The last example of a method to call has multiple paramters and returns a user defined class. I made a “Person” object which is a class inside the “GP_EmbeddedTestDLL” object we embedded in our application.
It is a class that has properties about the person, and is returned by the function call “GetPersonObject”.
- We invoke the method as usual, passing in the array of parameters.
- We then use the “GetProperty” on the type to get at the property values of the class returned.

As mentioned, I decided not to use embedding in Serial Key Maker due to performance concerns I had, however, I think this is a powefult technique and will use it when it makes sense to do so.
I’d love to hear from anyone who is using this technique in their applications. Please email me at
grant@serialkeymaker.com
Thank you for reading,
Grant

Posted by serialkeymaker
Posted by serialkeymaker
Posted by serialkeymaker