Click here to Skip to main content
11,934,380 members (41,532 online)
Click here to Skip to main content
Add your own
alternative version


101 bookmarked

CExpire - A C++ class that implements time and run based restrictions

, 28 Feb 2002 Ms-PL
Rate this:
Please Sign up or sign in to vote.
Shareware authors can use this class to limit the number of runs or the number of days that a program will function


This class is something I wrote to kill time. But by the time I finished it, I thought it might be pretty useful for shareware developers. It can be used to implement limited-number-of-runs and limited-time-period-only programs. Means if you want to allow only 25 runs, then this class will do that for you. Or if you want to restrict execution to 15 days from first run, this program will do that too. By the way, even if the user changes his system date back to the allowed-time-period after it has expired, he/she won't be able to run it. 


This is by no means a fool-proof protection scheme, but it serves its purpose at the intermediate level. And usually crackers manage to actually patch the executable to disable these checks or they manage to write key generators for your protected programs. But this class will save some time for you in the sense, it actually does what you would otherwise have to code on your own.

Protection Mechanism

It uses a combination of registry and file based protection mechanisms. You should be smart enough to pick the right innocent sounding registry keys and filenames to prevent the casual user from simply deleting a few odd keys and break the protection.

How to use the class.

You must instantiate an object of the class as early as possible. You may declare it as a global object if you would like to. In an SDK program, you might want to instantiate the class on the first line in your WinMain() and in an MFC program you might want to put it in your CWinApp derived class'sInitInstance() .

There are two ways in which you can use the class. In the first way you specify a specific number of times that the program can be run. In the second you set a number of days so that the program is active for that many days from the time of first execution. I shall demonstrate both methods below.

The constructor takes 4 parameters.

CExpire(const char*  ProgName,const char* KeyName, UINT Num, UINT ExpireType);

ProgName - This is a unique value that will be used to create the unique registry key for protection. It is a good idea to choose a GUID for this key. It will be placed in a location confusing enough for most of the average users of your program.

KeyName - This is again used as part of the unique registry key as well as in the generation of the special file. You can use a unique, but natural sounding name like MouseDrv98 or something that would not actually stand out.

Num - This is either the number of days from the date of execution or the total number of runs allowed. This value's interpretation depends upon the ExpireType parameter.

ExpireType - This can be one of two values as shown below

TYPERUNS - This implies run-count based protection
TYPEDAYS - This implies date-interval based protection

There are only three public functions that you'll need to call.

bool HasExpired();

It returns true if your program has expired and false if the user is still within his allowed limits.

UINT GetDaysLeft();

This returns the number of days left before the program expires. Use this if you have chosen ExpireType as TYPEDAYS

UINT GetRunsLeft();

This returns the number of runs left before the program expires. Use this if you have chosen ExpireType as TYPERUNS

Example Usage

SDK example

    CExpire Protector("{00000000-0000-0000-8888-00AA006D2EA4}",
            "This program has expired. Please buy the registered version.",
            "Fatal Error",0);
        char s[128];
        sprintf(s,"you have %d days left",Protector.GetDaysLeft());
    return 0;

MFC example

BOOL CTest_deleteApp::InitInstance()
    CExpire expire("JPEG-File-Viewer","FileStore",30,TYPERUNS);
        AfxMessageBox("This program will not run anymore...");
        return FALSE;
    char s[128];
    sprintf(s,"you have %d runs left",expire.GetRunsLeft());

#ifdef _AFXDLL

    CTest_deleteDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;

Important Warning

DON'T choose a value for the KeyName parameter that might already define a file in the Windows System directory. It will be over-written. The best thing to do is to choose a filename and then add some numbers to the end like 32_32 or something natural sounding.

Thank you.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
kore3d18-Jan-15 23:13
memberkore3d18-Jan-15 23:13 
GeneralRe: My vote of 1 Pin
Nish Sivakumar19-Jan-15 5:32
sitebuilderNish Sivakumar19-Jan-15 5:32 
GeneralMy vote of 5 Pin
Manikandan1028-Jun-14 5:11
professionalManikandan1028-Jun-14 5:11 
QuestionRegistry key deletion Pin
Djibril9-Nov-11 8:04
memberDjibril9-Nov-11 8:04 
GeneralSome comments would have been very useful... Pin
Ady_MFC6-Feb-07 11:08
memberAdy_MFC6-Feb-07 11:08 
Questionit does not work on Windows Vista Pin
Yurat20-Dec-06 5:38
memberYurat20-Dec-06 5:38 
it does not work on Windows Vista. Under Windows Vista it is not possible to read Registry HKEY_CLASSES_ROOT. So the class always returns 0 days and continues working. Although if a user does not have admin rights, it does not work as well.

Best regards,

AnswerRe: it does not work on Windows Vista Pin
Tayrone Augusto2-May-07 12:24
memberTayrone Augusto2-May-07 12:24 
GeneralRe: it does not work on Windows Vista Pin
cujobr15-Jul-09 3:45
membercujobr15-Jul-09 3:45 
Generalgood, but not really usable Pin
T1TAN16-Dec-05 15:33
memberT1TAN16-Dec-05 15:33 
GeneralRe: good, but not really usable Pin
Nishant Sivakumar21-Jul-06 9:49
staffNishant Sivakumar21-Jul-06 9:49 
Generaldo you have something like this in C# Pin
Anonymous20-Oct-05 5:38
sussAnonymous20-Oct-05 5:38 
GeneralRe: do you have something like this in C# Pin
Nishant Sivakumar21-Jul-06 9:49
staffNishant Sivakumar21-Jul-06 9:49 
GeneralRe: do you have something like this in C# Pin
Prasadrn26-Nov-07 4:26
memberPrasadrn26-Nov-07 4:26 
GeneralRe: do you have something like this in C# Pin
Prasadrn26-Nov-07 4:31
memberPrasadrn26-Nov-07 4:31 
QuestionRe: do you have something like this in C# Pin
Programm3r22-Jul-09 21:17
memberProgramm3r22-Jul-09 21:17 
GeneralI'd be happy with the security of using the registry, but not this Pin
pierznj10-Nov-09 14:36
memberpierznj10-Nov-09 14:36 
GeneralI found one problem Pin
Jiten D. Gandhi31-Oct-04 21:07
memberJiten D. Gandhi31-Oct-04 21:07 
GeneralRe: I found one problem Pin
Anonymous21-Feb-05 2:51
sussAnonymous21-Feb-05 2:51 
GeneralRe: I found one problem Pin
DavidCrow16-Aug-05 3:50
memberDavidCrow16-Aug-05 3:50 
GeneralRe: I found one problem Pin
GaJurong16-Oct-06 22:30
memberGaJurong16-Oct-06 22:30 
GeneralRe: I found one problem Pin
Djibril8-Nov-11 9:47
memberDjibril8-Nov-11 9:47 
GeneralRe: I found one problem Pin
Djibril9-Nov-11 8:05
memberDjibril9-Nov-11 8:05 
GeneralRe: I found one problem Pin
Djibril29-Nov-11 6:29
memberDjibril29-Nov-11 6:29 
QuestionGood work, Activex Control? Pin
hajer29-Apr-04 23:59
memberhajer29-Apr-04 23:59 
GeneralNice one Pin
deepakbsmurthy26-Feb-04 23:56
memberdeepakbsmurthy26-Feb-04 23:56 
Questionhow secure is this? Pin
brian scott31-Dec-03 12:05
memberbrian scott31-Dec-03 12:05 
AnswerRe: how secure is this? Pin
MrLeeGriffiths9-Aug-06 6:18
memberMrLeeGriffiths9-Aug-06 6:18 
GeneralGood job but ... Pin
Juan Carlos Cobas29-Jul-03 13:23
memberJuan Carlos Cobas29-Jul-03 13:23 
GeneralRe: Good job but ... Pin
Nishant S29-Jul-03 16:23
editorNishant S29-Jul-03 16:23 
GeneralRe: Good job but ... Pin
Juan Carlos Cobas30-Jul-03 5:48
memberJuan Carlos Cobas30-Jul-03 5:48 
GeneralRe: Good job but ... Pin
Nishant S30-Jul-03 7:48
editorNishant S30-Jul-03 7:48 
GeneralRe: Good job but ... Pin
Juan Carlos Cobas30-Jul-03 7:04
memberJuan Carlos Cobas30-Jul-03 7:04 
GeneralRe: Good job but ... Pin
Nishant S30-Jul-03 7:45
editorNishant S30-Jul-03 7:45 
GeneralRe: Good job but ... Pin
Juan Carlos Cobas30-Jul-03 8:58
memberJuan Carlos Cobas30-Jul-03 8:58 
GeneralRe: Good job but ... Pin
fischer_man_s2-Jul-04 13:53
sussfischer_man_s2-Jul-04 13:53 
GeneralBug in GetDayCount( ) Pin
Codin' Carlos17-Apr-03 18:07
memberCodin' Carlos17-Apr-03 18:07 
GeneralRe: Bug in GetDayCount( ) Pin
EnCoder12-Oct-03 15:54
memberEnCoder12-Oct-03 15:54 
QuestionFound a memory leak bug? Pin
Codin' Carlos19-Oct-02 8:21
memberCodin' Carlos19-Oct-02 8:21 
AnswerRe: Found a memory leak bug? Pin
Nishant S19-Oct-02 9:14
editorNishant S19-Oct-02 9:14 
GeneralGood Job Pin
Old Timer18-Oct-02 2:25
memberOld Timer18-Oct-02 2:25 
GeneralRe: Good Job Pin
Nishant S18-Oct-02 2:32
editorNishant S18-Oct-02 2:32 
GeneralIdea (add version/build param) Pin
CzarTJ6-Aug-02 9:49
memberCzarTJ6-Aug-02 9:49 
GeneralRe: Idea (add version/build param) Pin
Nishant S6-Aug-02 18:54
editorNishant S6-Aug-02 18:54 
GeneralAnother Suggestions Pin
Mauricio Ritter11-Mar-02 2:53
memberMauricio Ritter11-Mar-02 2:53 
GeneralRe: Another Suggestions - Set Hidden File Time Pin
Codin' Carlos18-Oct-02 7:22
memberCodin' Carlos18-Oct-02 7:22 
GeneralRe: Another Suggestions - Set Hidden File Time Pin
over-cast30-Dec-03 19:14
sussover-cast30-Dec-03 19:14 
GeneralExcellent Pin
Dan Petitt5-Mar-02 11:20
memberDan Petitt5-Mar-02 11:20 
GeneralArticle Updated [Mar 02 2002] Pin
Nish [BusterBoy]1-Mar-02 19:02
memberNish [BusterBoy]1-Mar-02 19:02 
GeneralSuggestion Pin
Mauricio Ritter27-Feb-02 3:35
memberMauricio Ritter27-Feb-02 3:35 
GeneralRe: Suggestion Pin
Nish [BusterBoy]27-Feb-02 4:49
memberNish [BusterBoy]27-Feb-02 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 1 Mar 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid