To use following example, simply create new class in your VB.NET project and paste following code.

You can also download this example.

  1. Imports System.IO
  2. Imports System.Security.Cryptography
  3. Imports TreeksLicensingLibrary
  4.  
  5. ''' <summary>
  6. ''' Contains functions to work with TLL
  7. ''' </summary>
  8. ''' <remarks></remarks>
  9. Public Class TLLInterface
  10.  
  11.     Private Const MyPublicKey As String = "Put your PUBLIC key XML here." 'Just paste from toolbox
  12.    Private Const MyLicenseXML As String = "Put your license from us here"
  13.     Private verification As New LicenseVerification(MyPublicKey)
  14.  
  15.     ''' <summary>
  16.    ''' Call this function to verify user's license. Optionally supply string and License object to get more information (error message, license details)
  17.    ''' </summary>
  18.    ''' <param name="ErrorMessage">Gives you more information if license validation failed</param>
  19.    ''' <param name="LicenseObject">Contains license details</param>
  20.    ''' <returns>Bool: True if license was successfully verified</returns>
  21.    ''' <remarks></remarks>
  22.    Public Function IsLicenseValid(Optional ByRef ErrorMessage As String = "", Optional ByRef LicenseObject As TreeksLicensingLibrary.License = Nothing) As Boolean
  23.  
  24.         Dim LicenseFilePath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location) & "\registration.lic"
  25.  
  26.         If ValidateLicensingLibrary() Then
  27.             If IO.File.Exists(LicenseFilePath) Then
  28.                 If verification.VerifyLicenseFile(LicenseFilePath, ErrorMessage, LicenseObject) Then
  29.                     Return True
  30.                 Else
  31.                     Return False
  32.                 End If
  33.             Else
  34.                 ErrorMessage = "License file not present."
  35.                 Return False
  36.             End If
  37.         Else
  38.             ErrorMessage = "Library integrity check failed."
  39.             Return False
  40.         End If
  41.     End Function
  42.  
  43.     ''' <summary>
  44.    ''' Return actual hardware ID
  45.    ''' </summary>
  46.    ''' <returns>String: Actual hardware ID</returns>
  47.    ''' <remarks></remarks>
  48.    Public Function GetAactualHardwareID() As String
  49.         Return HardwareID.ActualHardwareID
  50.     End Function
  51.  
  52.     ''' <summary>
  53.    ''' Validates if TLL was not modified by unathorized user
  54.    ''' </summary>
  55.    ''' <returns>Bool: True if file was not modified</returns>
  56.    ''' <remarks></remarks>
  57.    Private Function ValidateLicensingLibrary() As Boolean
  58.  
  59.         Dim sha As New SHA1
  60.         Dim path As String = LibraryDLLPath()
  61.  
  62.         Dim current_hash As String = sha.hashOfFile(path)
  63.         Dim valid_hash As String = "7e4b6c0c073853205dbb92bf0798e977a36dbf75"
  64.  
  65.         If valid_hash = current_hash Then
  66.             Return True
  67.         Else
  68.             Return False
  69.         End If
  70.  
  71.     End Function
  72.  
  73.     ''' <summary>
  74.    ''' Disables TLL demo message
  75.    ''' </summary>
  76.    ''' <remarks></remarks>
  77.    Private Sub SetLicenseInformation()
  78.         TreeksLicensingLibrary.DeveloperLicense.LicenseXML = MyLicenseXML
  79.     End Sub
  80.  
  81.     ''' <summary>
  82.    ''' Returns path of TLL to verify integrity
  83.    ''' </summary>
  84.    ''' <returns>String: Treek's Licensing Library DLL path</returns>
  85.    ''' <remarks></remarks>
  86.    Private Function LibraryDLLPath() As String
  87.  
  88.         Dim dirpath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location)
  89.         If Not dirpath.EndsWith("\") Then dirpath &= "\"
  90.  
  91.         Dim path As String = dirpath & "TreeksLicensingLibrary.dll"
  92.  
  93.         Return path
  94.  
  95.     End Function
  96.  
  97. End Class
  98.  
  99. ''' <summary>
  100. ''' Contains interface to work with SHA1
  101. ''' </summary>
  102. ''' <remarks></remarks>
  103. Public Class SHA1
  104.     Implements IHasher
  105.  
  106.     ''' <summary>
  107.    ''' Returns SHA1 hash of file
  108.    ''' </summary>
  109.    ''' <param name="fileToHash">Filesystem path to hashed file</param>
  110.    ''' <returns>String</returns>
  111.    ''' <remarks></remarks>
  112.    Public Function hashOfFile(ByVal fileToHash As String) As String Implements IHasher.hashOfFile
  113.         Dim rdr As FileStream
  114.         Dim sha As New SHA1CryptoServiceProvider
  115.         Dim bytes() As Byte
  116.         Dim rtn As String = ""
  117.         If File.Exists(fileToHash) Then
  118.             rdr = New FileStream(fileToHash, FileMode.Open, FileAccess.Read)
  119.             bytes = sha.ComputeHash(rdr)
  120.             rtn = ByteArrayToString(bytes)
  121.         End If
  122.         Return rtn
  123.     End Function
  124.  
  125.     ''' <summary>
  126.    ''' Returns SHA1 hash of string
  127.    ''' </summary>
  128.    ''' <param name="stringToHash">String to be hashed</param>
  129.    ''' <returns>String</returns>
  130.    ''' <remarks></remarks>
  131.    Public Function hashOfString(ByVal stringToHash As String) As String Implements IHasher.hashOfString
  132.         Dim sha As New SHA1CryptoServiceProvider
  133.         Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(stringToHash)
  134.         bytesToHash = sha.ComputeHash(bytesToHash)
  135.         Dim strResult As String = ""
  136.         For Each b As Byte In bytesToHash
  137.             strResult += b.ToString("x2")
  138.         Next
  139.         Return strResult
  140.     End Function
  141.  
  142.     Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
  143.         Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
  144.         For i As Integer = 0 To arrInput.Length - 1
  145.             sb.Append(arrInput(i).ToString("X2"))
  146.         Next
  147.         Return sb.ToString().ToLower
  148.     End Function
  149.  
  150.     Private Interface IHasher
  151.         Function hashOfString(ByVal stringToHash As String) As String
  152.         Function hashOfFile(ByVal fileToHash As String) As String
  153.     End Interface
  154. End Class