Results 1 to 3 of 3

Thread: 30 Day Trial Example Update

  1. #1
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    30 Day Trial Example Update

    Hi this is a simple bit of code to allow you to add 30 day trial to your programs.
    Hope you may find it usfull.
    Comments and suggestions welcome.

    Hi this is a new update the code now been converted to a class with a few fixes to the code.

    class code

    vbnet Code:
    1. Option Explicit On
    2. Imports System.IO
    3.  
    4. Public Class cTrial
    5.  
    6.     Private m_DaysUsed As Integer = 0
    7.     Private m_MaxDays As Integer = 30
    8.     Private m_Ended As Boolean = False
    9.     Private m_Filename As String = vbNullString
    10.  
    11.     Public ReadOnly Property DaysUsed As Integer
    12.         Get
    13.             'Return number of days used.
    14.             Return m_DaysUsed
    15.         End Get
    16.     End Property
    17.  
    18.     Public ReadOnly Property DaysLeft As Integer
    19.         Get
    20.             'Return number of days left.
    21.             Return (MaxDays - m_DaysUsed)
    22.         End Get
    23.     End Property
    24.  
    25.     Public Property Filename As String
    26.         Get
    27.             Return m_Filename
    28.         End Get
    29.  
    30.         Set(ByVal value As String)
    31.             'Set filename.
    32.             m_Filename = value
    33.             'Check if trial file is found.
    34.             If Not File.Exists(Filename) Then
    35.                 'Create trial file.
    36.                 If Not UpdateFile(Now.Date.AddDays(MaxDays), 0) Then
    37.                     Throw New FileNotFoundException
    38.                     Exit Property
    39.                 End If
    40.             End If
    41.  
    42.             'Check the date.
    43.             CheckDate(Now.Date)
    44.         End Set
    45.     End Property
    46.  
    47.     Public ReadOnly Property HasEnded As Boolean
    48.         Get
    49.             Return m_Ended
    50.         End Get
    51.     End Property
    52.  
    53.     Public Property MaxDays As Integer
    54.         Get
    55.             Return m_MaxDays
    56.         End Get
    57.         Set(ByVal value As Integer)
    58.             m_MaxDays = value
    59.         End Set
    60.     End Property
    61.  
    62.     Private Sub CheckDate(ByVal Date1 As DateTime)
    63.         Dim sr As StreamReader = Nothing
    64.         Dim Date2 As DateTime = Nothing
    65.         Dim Flag As Integer = 0
    66.         Dim Tmp As String = vbNullString
    67.  
    68.         Try
    69.             sr = New StreamReader(Filename)
    70.             'Read in file contents.
    71.             Tmp = XorString(sr.ReadToEnd())
    72.             'Close file.
    73.             sr.Close()
    74.  
    75.             'Read trial flag.
    76.             Flag = Integer.Parse(Tmp(0))
    77.             'Read date.
    78.             Date2 = Tmp.Substring(1)
    79.  
    80.             'Check if trial finished.
    81.             If (Flag <> 0) Then
    82.                 m_DaysUsed = MaxDays
    83.                 m_Ended = True
    84.             Else
    85.                 'Return number of days used.
    86.                 m_DaysUsed = MaxDays - DateDiff("d", Date1, Date2) + 1
    87.                 'Check if in range.
    88.                 m_Ended = (DaysUsed <= 0) Or (DaysUsed > MaxDays)
    89.             End If
    90.         Catch ex As Exception
    91.             m_DaysUsed = MaxDays
    92.             m_Ended = True
    93.         End Try
    94.  
    95.         If HasEnded Then
    96.             m_DaysUsed = MaxDays
    97.             'Add date and trial end flag.
    98.             UpdateFile(Date2, 1)
    99.         End If
    100.  
    101.     End Sub
    102.  
    103.     Private Function UpdateFile(ByVal EndDate As DateTime, ByVal Flag As Integer) As Boolean
    104.         Dim sw As StreamWriter = Nothing
    105.         'File format is as follows flag,date
    106.         'flag 0 = still in trial
    107.         'flag 1 = trial has ended.
    108.  
    109.         Try
    110.             sw = New StreamWriter(Filename)
    111.             'Write date and trial flag.
    112.             sw.Write(XorString(Flag & EndDate.Date))
    113.             'Close file.
    114.             sw.Close()
    115.         Catch ex As Exception
    116.             Return False
    117.         End Try
    118.  
    119.         Return True
    120.     End Function
    121.  
    122.     Private Function XorString(ByVal source As String) As String
    123.         Dim sb As New System.Text.StringBuilder()
    124.  
    125.         Rnd(-1)
    126.         'Set Random seed.
    127.         Randomize(1830)
    128.  
    129.         For Each c As Char In source
    130.             'Encrypt bye
    131.             Dim b As Byte = Asc(c) Xor Int(Rnd() * 256) Mod 255
    132.             'Append char to stringbuilder.
    133.             sb.Append(Chr(b))
    134.         Next c
    135.         'Return string
    136.         Return sb.ToString()
    137.  
    138.     End Function
    139. End Class

    Example code put into a new form.

    vbnet Code:
    1. Option Explicit On
    2. Imports System.IO
    3.  
    4. Public Class frmmain
    5.  
    6.     Private MyTrial As New cTrial()
    7.  
    8.     Private Sub ShowMsg(ByVal msg As String)
    9.         MessageBox.Show(msg, "Trial-Demo",
    10.                         MessageBoxButtons.OK, MessageBoxIcon.Information)
    11.     End Sub
    12.  
    13.     Private Sub frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.         With MyTrial
    16.             'Set the number of trial days.
    17.             .MaxDays = 30
    18.             'Tril file, hide this file somewere were no one can find it.
    19.             .Filename = Path.Combine(Application.StartupPath, "trial.sys")
    20.  
    21.             'Check if trial has ended.
    22.             If .HasEnded Then
    23.                 ShowMsg("Your " & .MaxDays & " day trial has eneded.")
    24.             Else
    25.                 ShowMsg("You are on day " & .DaysUsed & " of " & .MaxDays & " days.")
    26.             End If
    27.         End With
    28.  
    29.     End Sub
    30.  
    31. End Class
    Last edited by BenJones; Sep 16th, 2012 at 11:32 AM.

  2. #2
    Frenzied Member
    Join Date
    Sep 02
    Location
    Columbus, Ohio
    Posts
    1,830

    Re: 30 Day Trial Example Update

    Looks like, without me testing, all someone has to do to keep using it is set back the clock on their computer. I had a trial version of Toad that I used for years that way

  3. #3
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 10
    Location
    Wales UK
    Posts
    540

    Re: 30 Day Trial Example Update

    Looks like, without me testing, all someone has to do to keep using it is set back the clock on their computer. I had a trial version of Toad that I used for years that way
    Yep it's basic tho the date is encrypted so they need to decrypt first plus you can hide the file somewere hard to find.
    I am going to think of something that if the date is set back, just need to think of something.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •