Results 1 to 26 of 26

Thread: Trial Period code - VB.NET

  1. #1

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Trial Period code - VB.NET

    Originally wrote this up as a reply to a thread where the OP had asked for a way to check to see if the user had used their app for X days... The problem with most approaches is that they use date comparisons. That means the user can get around it by moving the system date back.

    Here's something I threw together... it's nothing complicated, still has a loop hole, but for as basic as it is, not too bad. Rather than just storing the install or first-run date, I grab the current date, hash it, and check to see if the hash exists in the settings collection. If not, and we still have "slots" open... then it's considered to be a new date of usage, and the hash gets added to the collection. Once all of the slots have been used, the trial period is over and the function returns false.

    Written in VB2008... should work in 2005, and will work in 2010 (although it's only tested in vb2008)

    First the setup - From Project properties, go to the settings and create a user scoped setting "UsageDates" Set the type to "Specialized.StringCollection" and leave the default value blank.

    Then add these two functions somewhere where your start up form or sub can get to them (like in the form itself, or the module where the Main sub is... where ever)
    vb Code:
    1. Private Function CheckDate(ByVal dateToCheck As Date) As Boolean
    2.         'In reality, CheckDate would get the date (current date) itself and not have it passed in
    3.         Dim retValue As Boolean = False 'Fail safe, default to false
    4.         Dim usageDatesLeft As Int16 = 3 ' set it to 4 just for testing
    5.         'Dim usageDatesLeft As Int16 = 30 ' set this to the number of days of application access
    6.  
    7.         'Hash the date
    8.         Dim hashedDate As String = HashDate(dateToCheck)
    9.         'Check to see if the hash value exists in the UsageDates
    10.  
    11.         'Initialize the container if necessary
    12.         If My.Settings.UsageDates Is Nothing Then
    13.             My.Settings.UsageDates = New System.Collections.Specialized.StringCollection
    14.         End If
    15.  
    16.         If My.Settings.UsageDates.Contains(hashedDate) Then
    17.             'then we are ok...  it's already been checked
    18.             retValue = True
    19.             usageDatesLeft -= My.Settings.UsageDates.Count
    20.  
    21.             'sanity check... if the system date is backed up to a previous date in the list, but not the last date
    22.             If usageDatesLeft <= 0 AndAlso My.Settings.UsageDates.IndexOf(hashedDate) <> My.Settings.UsageDates.Count - 1 Then
    23.                 retValue = False
    24.             End If
    25.         Else
    26.             If My.Settings.UsageDates.Count < usageDatesLeft Then
    27.                 My.Settings.UsageDates.Add(hashedDate)
    28.             End If
    29.             usageDatesLeft -= My.Settings.UsageDates.Count
    30.  
    31.  
    32.             'If not, and the remining count has "slots" open, add it
    33.             If usageDatesLeft > 0 Then
    34.                 retValue = True
    35.             Else
    36.                 'If not and tree are no more slots, tell user, exit app
    37.                 retValue = False
    38.             End If
    39.  
    40.         End If
    41.         'Display to the user how many days are remianing:
    42.         MessageBox.Show(String.Format("You have {0} day(s) remaining.", usageDatesLeft))
    43.  
    44.         Return retValue
    45.     End Function
    46.  
    47.     Private Function HashDate(ByVal dateToHash As Date) As String
    48.         'Get a hash object
    49.         Dim hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
    50.         'Take date, make it a Long date and hash it
    51.         Dim data As Byte() = hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(dateToHash.ToLongDateString()))
    52.         ' Create a new Stringbuilder to collect the bytes
    53.         ' and create a string.
    54.         Dim sBuilder As New System.Text.StringBuilder()
    55.  
    56.         ' Loop through each byte of the hashed data
    57.         ' and format each one as a hexadecimal string.
    58.         Dim idx As Integer
    59.         For idx = 0 To data.Length - 1
    60.             sBuilder.Append(data(idx).ToString("x2"))
    61.         Next idx
    62.  
    63.         Return sBuilder.ToString
    64.  
    65.     End Function

    To use it, is fairly simple... NOTE: the messageboxes in the CheckDate function were for testing... you could change CheckDate to return the number of days left, or leave it as is...
    Here's how I tested it... add a button to a form (strictly for testing... in reality your main sub or the form_load event - or the from constructor - would call checkDate) and add this to the click event:
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim aCount As Integer = 0
    3.         Dim loopIt As Boolean = True
    4.         'My.Settings.Reset() 'This is here for design time support... otherwise you won't get your app to run agin
    5.  
    6.         Do While loopIt
    7.             MessageBox.Show(String.Format("Checking Date: {0}.", Date.Now.AddDays(aCount)))
    8.             loopIt = CheckDate(Date.Now.AddDays(aCount))
    9.             If Not loopIt Then
    10.                 MessageBox.Show("Trial Period Ended! Application closing!")
    11.                 Me.Close()
    12.             Else
    13.                 MessageBox.Show("You can keep using the app")
    14.             End If
    15.             aCount += 1
    16.         Loop
    17.  
    18.     End Sub

    Like I said, it's not perfect, I'll provide minimal support for it, in that I'll help you get it going if you want it, but other than that, you're on your own. Ff some one wants to take it and run with it and do something worthwhile with it, or even beef it up some... go right ahead... just let me know, as it would be interesting to see what could be done with it. Or better yet, add the modifications to this thread.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  2. #2
    Lively Member
    Join Date
    Jun 2010
    Posts
    95

    Re: Trial Period code - VB.NET

    Thankyou! This is beautiful!

  3. #3
    New Member
    Join Date
    Dec 2010
    Location
    China
    Posts
    4

    Re: Trial Period code - VB.NET

    it Useful for me,thank you so much.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    50

    Re: Trial Period code - VB.NET

    Thanks!

  5. #5
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Re: Trial Period code - VB.NET

    I have a question, when the trial finished the user can uninstall and reinstall the app again?

    Also the form never show my app, only show the MessageBox when I make click on the button the MessageBox appear again and again.
    Last edited by romanos8; Jul 16th, 2011 at 10:27 PM.

  6. #6

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    THat's because the code in the button click is just an example of how to use it,... it's up to you to incorporate it properly into your app.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Trial Period code - VB.NET

    What if the settings file is deleted or modified?
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    Quote Originally Posted by post1
    Like I said, it's not perfect
    Odds are, you probably have other issues besides the trial period starting over...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    21

    Re: Trial Period code - VB.NET

    it's perfect for my needs, i was able to modify it just a little to get it to do what i wanted.

    Great Work!!

    Thanks.

  10. #10
    Junior Member ApDev's Avatar
    Join Date
    Nov 2011
    Location
    Madrid
    Posts
    23

    Re: Trial Period code - VB.NET

    thanks! I'll try later or when I have time!

  11. #11
    New Member
    Join Date
    Jan 2012
    Posts
    11

    Re: Trial Period code - VB.NET

    I test in Smart Device Win CE 5 some syntax dont work any help?

  12. #12

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    The code was written in VS2008 (might have been 2005, I forget)... and works against FW2.0 ... how ever it was neither tested, nor guaranteed, against CF...

    That said, since you didn't specify the errors, I can only guess as to what the problem is. So, I'll guess that the problem is that you've got some eels in your hover craft.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    New Member
    Join Date
    Jan 2012
    Posts
    11

    Re: Trial Period code - VB.NET

    this is the error techgnome sorry for not putting it...
    Smart Device WinCE5

    System.Collections.Specialized.StringCollection

  14. #14
    Member
    Join Date
    Feb 2012
    Posts
    48

    Re: Trial Period code - VB.NET

    question very nob question after a search of this forum.

    Where does it store this string data?

    Thanks
    Last edited by Norseman; Apr 16th, 2012 at 01:10 PM.

  15. #15

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    as suggested by the name "My.Settings.UsageDates" ... in the settings file. (user level config file).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    Member
    Join Date
    Feb 2012
    Posts
    48

    Re: Trial Period code - VB.NET

    thanks.

  17. #17
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Re: Trial Period code - VB.NET

    Hi

    I have used your code and it worked. However, now even when I remove the code still the application checks for the usage dates. I want to undo it. Can you help? Thanks!

  18. #18
    Addicted Member Miklogak's Avatar
    Join Date
    Jan 2013
    Posts
    203

    Re: Trial Period code - VB.NET

    This Project is pretty awesome. Thanks for the hard work.

    The sad part is that I was using it for some of my project, and later my users reported that they could bypass the Trial time by deleting the files inside Windows Appdata and Temprorary Folder[/B]. True enough thats what happened. But again thank you. It is very usefull for basic project thoug!
    Last edited by Miklogak; Jan 20th, 2015 at 06:48 PM.

  19. #19

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    I did say that it was basic... it does have a loop hole... that being it... it was never meant to be be bullet proof. It will stop some people, but it won't stop those who know how to look for the config file and manipulate it. For the purposes of the thread from which I wrote it for, it was adequate.
    Quote Originally Posted by me
    Like I said, it's not perfect
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20
    Addicted Member t3cho's Avatar
    Join Date
    Mar 2014
    Posts
    234

    Re: Trial Period code - VB.NET

    I see this thread is from 2010th . But seems as a really good method.

    The main problem with this is reinstallation ? When user reinstall aplication he can use 15 days once again ? I'm I right ?

    Or is there any better method.
    Anel

  21. #21
    Addicted Member Miklogak's Avatar
    Join Date
    Jan 2013
    Posts
    203

    Re: Trial Period code - VB.NET

    @T3cho, I dont think its the Re-Installation. I have tested many times now after my users reported an issue, but the re-installation doesnt change anything. But as soon as use my Cleaning Tool I get to re-use the software again. Just like my users reported. I believe its because when deleting the files in APP DATA and TEMP folder then you can bypass it.

    I am working on something different using the same method because its so simple. Hope I get to find a solution. If I do I will post it below this topic.
    Last edited by Miklogak; Jan 20th, 2015 at 07:01 PM.

    A huge thanks to all the Great Developers and Helpers on vBForums for helping me and many others! Special thanks to Dunfiddlin, Paul, TechnoGome, , JayInThe813, ident for helping me with my projects througout the years. Incl. those i forgot to mention!

  22. #22

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    re-installation won't change it... as Miklogak noted, it's all just in a config file in the app data folder (or more accurately it's in the user's app data folder)... so clearing that out would re-start it. If your users are savy enough to know how to go looking for it, and where to look for it, then you likely need something more. This code was written for a very basic need of a poster. It was half-way decent enough that I posted it here. Would I use it myself? not likely. I'd probably prefer something a bit more robust. If someone wants to take it and make it more tamper proof, by all means go ahead.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Trial Period code - VB.NET

    Just a suggestion, but putting it in the current user's appdata folder will cause it to only expire on that user's profile, if another user logs in and uses the app, then they will get a different expire date. Maybe that's what you want, but if not, then I would suggest putting the config file (and encrypting it of some kind) in the Public User AppData folder so it'll expire at the same time for all users on the machine.
    Be sure to have the installer for your app create the config file, otherwise they'll get extra days to use it if the file is created on first run.

  24. #24
    Lively Member kobusjhg's Avatar
    Join Date
    Jul 2023
    Location
    Pretoria, South Africa
    Posts
    124

    Re: Trial Period code - VB.NET

    Quote Originally Posted by techgnome View Post
    Originally wrote this up as a reply to a thread where the OP had asked for a way to check to see if the user had used their app for X days... The problem with most approaches is that they use date comparisons. That means the user can get around it by moving the system date back.

    Here's something I threw together... it's nothing complicated, still has a loop hole, but for as basic as it is, not too bad. Rather than just storing the install or first-run date, I grab the current date, hash it, and check to see if the hash exists in the settings collection. If not, and we still have "slots" open... then it's considered to be a new date of usage, and the hash gets added to the collection. Once all of the slots have been used, the trial period is over and the function returns false.

    Written in VB2008... should work in 2005, and will work in 2010 (although it's only tested in vb2008)

    First the setup - From Project properties, go to the settings and create a user scoped setting "UsageDates" Set the type to "Specialized.StringCollection" and leave the default value blank.

    Then add these two functions somewhere where your start up form or sub can get to them (like in the form itself, or the module where the Main sub is... where ever)
    vb Code:
    1. Private Function CheckDate(ByVal dateToCheck As Date) As Boolean
    2.         'In reality, CheckDate would get the date (current date) itself and not have it passed in
    3.         Dim retValue As Boolean = False 'Fail safe, default to false
    4.         Dim usageDatesLeft As Int16 = 3 ' set it to 4 just for testing
    5.         'Dim usageDatesLeft As Int16 = 30 ' set this to the number of days of application access
    6.  
    7.         'Hash the date
    8.         Dim hashedDate As String = HashDate(dateToCheck)
    9.         'Check to see if the hash value exists in the UsageDates
    10.  
    11.         'Initialize the container if necessary
    12.         If My.Settings.UsageDates Is Nothing Then
    13.             My.Settings.UsageDates = New System.Collections.Specialized.StringCollection
    14.         End If
    15.  
    16.         If My.Settings.UsageDates.Contains(hashedDate) Then
    17.             'then we are ok...  it's already been checked
    18.             retValue = True
    19.             usageDatesLeft -= My.Settings.UsageDates.Count
    20.  
    21.             'sanity check... if the system date is backed up to a previous date in the list, but not the last date
    22.             If usageDatesLeft <= 0 AndAlso My.Settings.UsageDates.IndexOf(hashedDate) <> My.Settings.UsageDates.Count - 1 Then
    23.                 retValue = False
    24.             End If
    25.         Else
    26.             If My.Settings.UsageDates.Count < usageDatesLeft Then
    27.                 My.Settings.UsageDates.Add(hashedDate)
    28.             End If
    29.             usageDatesLeft -= My.Settings.UsageDates.Count
    30.  
    31.  
    32.             'If not, and the remining count has "slots" open, add it
    33.             If usageDatesLeft > 0 Then
    34.                 retValue = True
    35.             Else
    36.                 'If not and tree are no more slots, tell user, exit app
    37.                 retValue = False
    38.             End If
    39.  
    40.         End If
    41.         'Display to the user how many days are remianing:
    42.         MessageBox.Show(String.Format("You have {0} day(s) remaining.", usageDatesLeft))
    43.  
    44.         Return retValue
    45.     End Function
    46.  
    47.     Private Function HashDate(ByVal dateToHash As Date) As String
    48.         'Get a hash object
    49.         Dim hasher As System.Security.Cryptography.MD5 = System.Security.Cryptography.MD5.Create()
    50.         'Take date, make it a Long date and hash it
    51.         Dim data As Byte() = hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(dateToHash.ToLongDateString()))
    52.         ' Create a new Stringbuilder to collect the bytes
    53.         ' and create a string.
    54.         Dim sBuilder As New System.Text.StringBuilder()
    55.  
    56.         ' Loop through each byte of the hashed data
    57.         ' and format each one as a hexadecimal string.
    58.         Dim idx As Integer
    59.         For idx = 0 To data.Length - 1
    60.             sBuilder.Append(data(idx).ToString("x2"))
    61.         Next idx
    62.  
    63.         Return sBuilder.ToString
    64.  
    65.     End Function

    To use it, is fairly simple... NOTE: the messageboxes in the CheckDate function were for testing... you could change CheckDate to return the number of days left, or leave it as is...
    Here's how I tested it... add a button to a form (strictly for testing... in reality your main sub or the form_load event - or the from constructor - would call checkDate) and add this to the click event:
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim aCount As Integer = 0
    3.         Dim loopIt As Boolean = True
    4.         'My.Settings.Reset() 'This is here for design time support... otherwise you won't get your app to run agin
    5.  
    6.         Do While loopIt
    7.             MessageBox.Show(String.Format("Checking Date: {0}.", Date.Now.AddDays(aCount)))
    8.             loopIt = CheckDate(Date.Now.AddDays(aCount))
    9.             If Not loopIt Then
    10.                 MessageBox.Show("Trial Period Ended! Application closing!")
    11.                 Me.Close()
    12.             Else
    13.                 MessageBox.Show("You can keep using the app")
    14.             End If
    15.             aCount += 1
    16.         Loop
    17.  
    18.     End Sub

    Like I said, it's not perfect, I'll provide minimal support for it, in that I'll help you get it going if you want it, but other than that, you're on your own. Ff some one wants to take it and run with it and do something worthwhile with it, or even beef it up some... go right ahead... just let me know, as it would be interesting to see what could be done with it. Or better yet, add the modifications to this thread.

    -tg
    techgnome, I realise that you have written this code more than 10 yrs ago. I don't suppose it will work with 2022?
    I have tried to change the settings, but it only have 2 default settings on user scope which is Application and User. Looks like it is uneditable.
    Last edited by kobusjhg; Sep 23rd, 2023 at 02:56 AM.

  25. #25

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Trial Period code - VB.NET

    Right... there are only two types of settings... Application and User ... those are the only two scopes. For that code to work, it needs to be a User setting. Application settings can'r be changed once set (well they can but not so easily). But in this case it should be User anyways because that's whats being tracked.

    As for will it work in 2022? Shrug. Maybe. It should work if using .NET Framework ... if using Core (or what's now just .NET) ... maybe? I don't have a god way to test. All I can say is try it. Its small code, easy to drop in and test and see if it works.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  26. #26
    Lively Member kobusjhg's Avatar
    Join Date
    Jul 2023
    Location
    Pretoria, South Africa
    Posts
    124

    Re: Trial Period code - VB.NET

    Quote Originally Posted by techgnome View Post
    Right... there are only two types of settings... Application and User ... those are the only two scopes. For that code to work, it needs to be a User setting. Application settings can'r be changed once set (well they can but not so easily). But in this case it should be User anyways because that's whats being tracked.

    As for will it work in 2022? Shrug. Maybe. It should work if using .NET Framework ... if using Core (or what's now just .NET) ... maybe? I don't have a god way to test. All I can say is try it. Its small code, easy to drop in and test and see if it works.


    -tg
    Thank You for the info

Posting Permissions

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



Click Here to Expand Forum to Full Width