What Is A .dll File?
DLL files are files that contain instructions, which can be called by other programs to run specific tasks. This way, several programs can share the abilities programmed into a single file, and even do so simultaneously. A .dll file can have many different purposes and today I will be showing you how to use C++ CLI to create a .dll with an easily customizable GUI.
In order to create a C++ CLI .dll file, you must open the Visual Studio installer and verify that the “C++/CLI Support” option is enabled as seen in the picture below.
This option is not enabled by default so if you already have Visual Studios installed, open the installer, enable “C++/CLI Support” and then press the modify button to update your Visual Studio features.
Creating The Project
In order to create the project so that it compiles as a .dll file make sure to select “CLR Empty Project” on the “New Project” page. The example shown below is done in Visual Studios 2017 Community Edition but it will be the same for the 2019 Community Edition as well.
From here you can name your project and then press the “OK” button to create the project.
Now that you have created the project, it is time to change the project’s properties so that it correctly compiles as our .dll file.
You can see in the picture above that the properties are set with the “Configuration Type” of “Application(.exe)” and the “Target Extension” is “.exe”. We also see that the “Character Set” is using “Unicode”. Let's change these settings so that the “Configuration Type” and “Target Extension” are both set to “.dll” and the “Character Set” is set to “Not Set” as seen below.
Remember to select the correct configuration at the top of the properties window accordingly.
Creating The GUI
This part is, in my opinion, the most fun to do because it is where you can let your creativity show. Any time you want to add a Textbox or Button to your GUI, you can simply drag and drop it onto the Windows Form and the code will be added for you automatically! To get started, create a new file, and select UI -> Windows Form as the file type.
After you click the “Add” button you may see a screen like the one below
On the right, we can see that Visual Studios create both a “MyForm.h” as well as a “MyForm.cpp” file. On the left, we see an error message, but don’t worry because we can easily resolve that issue by reloading / reopening our project.
Now that we have reopened our project we can see a small Windows Form GUI as well as the properties of the GUI on the right side, such as the window’s main text, the icon, and the default font. Once we see this we can add elements to the window like a button.
Simply search for “button” in the Visual Studio Toolbox and then drag and drop it onto your GUI.
After playing around with the GUI you will notice how fun and easy it is to make a good-looking GUI without the hard coding work.
In this example you can see that I changed the window’s name to “Medium Example” and I hid the icon as well as changed the button’s text to “Show Message Box”.
Creating The “Entry Point”
The “entry point” is where the first instructions of a program are executed, and where the program has access to command-line arguments. Since our file type is not an executable we have to manually define our program's “entry point”. To do this, create a new file with the file type set to “.cpp”. In most examples, you will see the file be named something like “dll.cpp” or “dllmain.cpp”
Inside of our “dllmain.cpp” file, we will add some basic DLL entry code. Unfortunately, I won’t be explaining it here but you can check https://docs.microsoft.com/en-us/windows/win32/dlls/using-dynamic-link-libraries out for more information as well as the different ways of linking a dll.
After we have added our entry point code in the “dllmain.cpp” file we need to make sure the Form will appear/show whenever the .dll file is injected into an application. Inside of the “MyForm.cpp” file that was automatically created for us earlier, we can add the following code so that the Form will appear when injected.
Now if you try to compile the .dll file you will see that it compiles without any errors.
Now that the file was created, let’s try to inject it into another application like “notepad.exe”
We will inject the .dll file into an application using a program known as a “.dll injector”. Injectors can be found online, free to download, with a variety of features.
As we can see the file injects and our GUI appears. We were able to create a .dll that uses Windows Forms as the GUI in less than 50 lines of code.
The final step of this journey is adding the functionality of the program. Although I may have changed the button’s text to say “Show Message Box” when we click on the button, nothing will happen. To resolve this we need to go back to our code and add that functionality. To code what happens when the button is pressed, simply double click the button on the main Form and it will take you to the button's “on-press” code.
From here we can add the code into the button, re-compile the file, and re-inject to test if our code worked!
After clicking the button we see that the message box does appear.
Congratulations! You just created a simple .dll file in which you can easily style using the Visual Studios Toolbox without having to take time and code the visuals manually. You can check out some helpful resources below to learn more about how powerful this can be.