Click here to Skip to main content
Click here to Skip to main content

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

, 28 Feb 2002
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

Purpose

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. 

Disclaimer

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's InitInstance().

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

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
{
    CExpire Protector("{00000000-0000-0000-8888-00AA006D2EA4}",
        "InprocServer32",6,TYPEDAYS);
    if(Protector.HasExpired())
    {	
        MessageBox(0,
            "This program has expired. Please buy the registered version.",
            "Fatal Error",0);
    }
    else
    {
        char s[128];
        sprintf(s,"you have %d days left",Protector.GetDaysLeft());
        MessageBox(0,s,"",0);
        StartExecution();
    }
    return 0;
}

MFC example

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

    AfxEnableControlContainer();	
	
#ifdef _AFXDLL
    Enable3dControls();	
#else
    Enable3dControlsStatic();	
#endif

    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.

License

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

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalManikandan1028-Jun-14 4:11 
QuestionRegistry key deletion PinmemberDjibril9-Nov-11 7:04 
GeneralSome comments would have been very useful... PinmemberAdy_MFC6-Feb-07 10:08 
Questionit does not work on Windows Vista PinmemberYurat20-Dec-06 4: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,
Yuriy

AnswerRe: it does not work on Windows Vista PinmemberTayrone Augusto2-May-07 11:24 
GeneralRe: it does not work on Windows Vista Pinmembercujobr15-Jul-09 2:45 
Generalgood, but not really usable PinmemberT1TAN16-Dec-05 14:33 
GeneralRe: good, but not really usable PinstaffNishant Sivakumar21-Jul-06 8:49 
Generaldo you have something like this in C# PinsussAnonymous20-Oct-05 4:38 
GeneralRe: do you have something like this in C# PinstaffNishant Sivakumar21-Jul-06 8:49 
GeneralRe: do you have something like this in C# PinmemberPrasadrn26-Nov-07 3:26 
GeneralRe: do you have something like this in C# PinmemberPrasadrn26-Nov-07 3:31 
QuestionRe: do you have something like this in C# PinmemberProgramm3r22-Jul-09 20:17 
GeneralI'd be happy with the security of using the registry, but not this Pinmemberpierznj10-Nov-09 13:36 
GeneralI found one problem PinmemberJiten D. Gandhi31-Oct-04 20:07 
GeneralRe: I found one problem PinsussAnonymous21-Feb-05 1:51 
GeneralRe: I found one problem PinmemberDavidCrow16-Aug-05 2:50 
GeneralRe: I found one problem PinmemberGaJurong16-Oct-06 21:30 
GeneralRe: I found one problem PinmemberDjibril8-Nov-11 8:47 
GeneralRe: I found one problem PinmemberDjibril9-Nov-11 7:05 
GeneralRe: I found one problem PinmemberDjibril29-Nov-11 5:29 
QuestionGood work, Activex Control? Pinmemberhajer29-Apr-04 22:59 
GeneralNice one Pinmemberdeepakbsmurthy26-Feb-04 22:56 
Questionhow secure is this? Pinmemberbrian scott31-Dec-03 11:05 
AnswerRe: how secure is this? PinmemberMrLeeGriffiths9-Aug-06 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 1 Mar 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid