First of all, you will need to declare the required variables. For these you will need the following:
2 Date type variables - I named them dteStartDate and dteLastStart. dteStartDate is when the trial program is first installed and ran, dteLastStart is the date the program was last ran.
2 Boolean type variables - blnEnabled and blnFirstTime. blnEnabled keeps track of if the trial is still active or should be disabled. blnFirstTime will come into effect into part 2 of the tutorial (Adding a registry key to stop the trial being ran more than once), but I need more study before I can do that part.
1 Long type variable - lngTimeLeft. Keeps track of how many days remain in the trial.
1 Integer type variable - intTime. Keeps an eye out for people making changes to the system clock to extend the trial.
At this point, you should also set intTime to '1', blnEnabled to 'True', and make sure dteStartDate is assigned the date the program is first ran. Then add a statement telling your application to save it's settings when the program is closed.
Dim intTime As Integer = 1 Dim dteLastStart, dteStartDate As Date Dim blnFirstTime, blnEnabled As Boolean Dim lngTimeLeft As Long blnEnabled = True If dteStartDate = Nothing Then dteStartDate = Now End If My.Application.SaveMySettingsOnExit = True
Our next step is to add a few lines of code designed to stop people changing the system clock to extend the trial. Eventually, this will be coupled with a registry entry to disallow multiple installs of the demo, but as I have already mentioned, I need to look into that a bit further before I put anything on here.
If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
'First clock change
If intTime = 1 Then
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
intTime = 2
ElseIf intTime = 2 Then
'Second clock change
blnEnabled = False
MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
End If
'disables app
If blnEnabled = False Then
If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
So, first of all, check the number of days between the date the application was last ran, and today's date according to the system clock. If today's date is before the date the application was last ran, then the program will issue a warning, basically saying 'We know you changed your clock to an earlier date. Because this is the first time you've done this, we'll be nice and give you a warning. Don't do it again". Then it changes the intTime variable to 2 in case they try this trick a second time. This code can easily be changed to give more or fewer warnings before disabling the trial. All you will need to do is move the line 'blnEnabled = False' to another If statement, and remember to keep adding 1 to intTime for every warning you wish to give the user.
For this example, we only give the 1 warning, so if the user tries changing the system clock again, it will disable the application and givbe a message basically saying 'We warned you". Then it offers a second messagebox telling the user it has been disabled, then closes any open forms.
Finally, the last piece of code we need is that which disables the application at the end of the trial. This part can be modified to change the length of the trial to whatever you see fit. Just change the '14' (all 3 of them in this piece of code) to however many days you want the user to trial your application.
If DateDiff(DateInterval.Day, dteStartDate, Now) > 14 Then
blnEnabled = False
If blnEnabled = False Then
If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
'Close all open forms
For Each form As Form In My.Application.OpenForms
form.Close()
Next
End If
End If
End If
dteLastStart = Now
If blnFirstTime = True Then
blnFirstTime = False
End If
'Saves variable settings
My.Settings.Save()
lngTimeLeft = 14 - (DateDiff(DateInterval.Day, dteStartDate, Now))
MsgBox("This is a 14-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")
The very end records the date the program is ran, saves the settings, and gives a messagebox telling the user how many days are left.
If you have any questions or comments about this, please post them here.
Happy coding,
Bort





MultiQuote









|