Application Security Testing: An Integral Part of DevOps
A couple of months ago, I wrote an article about a user control, and I touched a little bit on Components. With this article, I will demonstrate how to make your own usable component.
What Is a Component?
A component will be output as a DLL. If you do not know what a DLL file is, have a read through here. To understand what a component is, I cannot put it better than MSDN, here, from which I quote:
|.NET components provide a programmable interface that is accessed by consumer applications (often called client applications). The component interface consists of a number of properties, methods, and events that are exposed by the classes contained within the component. In other words, a component is a compiled set of classes that support the services provided by the component. The classes expose their services through the properties, methods, and events that comprise the component's interface.|
Creating Your Own Component
Let's do a project!
Start Visual Studio and create a new Class Library project, as shown in Figure 1.
Figure 1: Selecting the Class Library
I thought it best to keep it rather simple because this is just an example. The premise of today's project is to create a component that can change the mouse cursor to an animated cursor. Now, I know there are easier ways to set the mouse cursor, but hey, I always go overboard in my articles.... :)
Add the Namespaces we will need for the component:
Imports System.IO Imports System.Windows.Forms Imports System.Runtime.InteropServices
This imports Windows Forms capabilities, as well the ability to import external libraries, called APIs. If you haven't heard about APIs yet, have a read through here.
Now, add the APIs:
Private Declare Unicode Function LoadCursorFromFile Lib _ "user32.dll" Alias "LoadCursorFromFileW" (ByVal filename As String) As IntPtr <DllImport("user32", EntryPoint:="SetCursor", CharSet:=CharSet.Auto)> _ Private Shared Function SetCursor(ByVal hCursor As Integer) As Integer End Function <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ Public Shared Function LoadCursor( _ ByVal hInstance As IntPtr, _ ByVal lpCursorName As Integer) As IntPtr End Function
These APIs allow you to load and set a Windows mouse cursor. Add the variables we will be using:
Private Shared curCursor As Cursor Private Shared FileName As String
Here, I have created the cursor we will work with, as well as a FileName object which will represent the name of our animated cursor. We're almost done with the component. All you need now is a way to load the animated cursor from a file, as well as to display it, so add the next to methods:
Public Shared Sub ShowCursor() FileName = Path.Combine(Application.StartupPath, "dinosaur.ani") curCursor = CreateCursor(FileName) Dim lHandle As Integer lHandle = LoadCursor(0, curCursor.Handle) SetCursor(lHandle) End Sub Private Shared Function CreateCursor(ByVal filename As String) As Cursor Dim hCursor As IntPtr Dim result As Cursor = Nothing Try hCursor = LoadCursorFromFile(filename) If Not IntPtr.Zero.Equals(hCursor) Then result = New Cursor(hCursor) Else 'could not create cursor Throw New ApplicationException("Could not create cursor from file " & filename) End If Catch ex As Exception 'log exception End Try Return result End Function
CreateCursor reads the file, and converts what was read into a usable cursor. ShowCursor displays the cursor object. You can build this DLL by clicking the Build menu item.
You're halfway finished at this point.
Now, we need a project to use this component with. Add a Windows Forms project to the component project by clicking File, Add Project. Once the Windows Forms project has been created, right-click it inside the Solution Explorer and set it as the Startup Project. Your Solution Explorer should look something like what's shown in Figure 2:
Figure 2: Solution Explorer
Add a Reference to your component inside the Windows Forms Project by clicking Project Add reference, as shown in Figure 3.
Figure 3: Project Reference
Add the last piece of code to your Windows Form:
Imports Component_Example_DLL Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Component_Example_DLL.AnimatedCursor_Ex.ShowCursor() End Sub End Class
I hope this article has been insightful and that you have enjoyed it. Until next time, cheers!
About the Author
Hannes du Preez is a Microsoft MVP for Visual Basic for the seventh year in a row.