Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (37 votes)
28 Feb 2002Ms-PL3 min read 246.8K   2.2K   104   72
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)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: Good Job Pin
Nish Nishant18-Oct-02 1:32
sitebuilderNish Nishant18-Oct-02 1:32 
GeneralIdea (add version/build param) Pin
CzarTJ6-Aug-02 8:49
CzarTJ6-Aug-02 8:49 
GeneralRe: Idea (add version/build param) Pin
Nish Nishant6-Aug-02 17:54
sitebuilderNish Nishant6-Aug-02 17:54 
GeneralAnother Suggestions Pin
Mauricio Ritter11-Mar-02 1:53
Mauricio Ritter11-Mar-02 1:53 
GeneralRe: Another Suggestions - Set Hidden File Time Pin
Codin' Carlos18-Oct-02 6:22
Codin' Carlos18-Oct-02 6:22 
GeneralRe: Another Suggestions - Set Hidden File Time Pin
over-cast30-Dec-03 18:14
sussover-cast30-Dec-03 18:14 
GeneralExcellent Pin
5-Mar-02 10:20
suss5-Mar-02 10:20 
GeneralArticle Updated [Mar 02 2002] Pin
Nish Nishant1-Mar-02 18:02
sitebuilderNish Nishant1-Mar-02 18:02 
I have updated the article.

Two new functions have been added that allow you to show the user the number of runs/days left before the program expires.

Thanks to Mauricio Ritter for this nice suggestion.

I have also removed a bug out by limax where I use sizeof instead of strlen on a const char*. In this particular case due to a strange situation [where I add 300] there was no error, but it was a very dangerous thing to do

Thanks for all your feedback, guys.
Nish

It's seven o'clock
On the dot
I'm in my drop top
Cruisin' the streets - Oh yeah
I got a real pretty, pretty little thing that's waiting for me
GeneralSuggestion Pin
Mauricio Ritter27-Feb-02 2:35
Mauricio Ritter27-Feb-02 2:35 
GeneralRe: Suggestion Pin
Nish Nishant27-Feb-02 3:49
sitebuilderNish Nishant27-Feb-02 3:49 
GeneralRe: Suggestion Pin
Nish Nishant1-Mar-02 18:07
sitebuilderNish Nishant1-Mar-02 18:07 
GeneralRe: Suggestion Pin
Mauricio Ritter2-Mar-02 11:30
Mauricio Ritter2-Mar-02 11:30 
GeneralBad style (in other cases it could be bug) Pin
limax26-Feb-02 22:16
limax26-Feb-02 22:16 
GeneralRe: Bad style (in other cases it could be bug) Pin
Nish Nishant1-Mar-02 18:10
sitebuilderNish Nishant1-Mar-02 18:10 
Generalanother cp solution Pin
26-Feb-02 21:58
suss26-Feb-02 21:58 
GeneralRe: another cp solution Pin
Juan Carlos Cobas14-Jun-03 5:17
Juan Carlos Cobas14-Jun-03 5:17 
GeneralRe: another cp solution Pin
Tibor Blazko15-Jun-03 22:00
Tibor Blazko15-Jun-03 22:00 
GeneralRe: another cp solution Pin
Juan Carlos Cobas15-Jun-03 22:15
Juan Carlos Cobas15-Jun-03 22:15 
QuestionBad Form? Pin
William Bartholomew26-Feb-02 11:14
William Bartholomew26-Feb-02 11:14 
AnswerRe: Bad Form? Pin
Maximilien26-Feb-02 12:15
Maximilien26-Feb-02 12:15 
GeneralRe: Bad Form? Pin
Nish Nishant26-Feb-02 12:33
sitebuilderNish Nishant26-Feb-02 12:33 
GeneralRe: Bad Form? Pin
Uwe Keim26-Feb-02 13:35
sitebuilderUwe Keim26-Feb-02 13:35 
GeneralRe: Bad Form? Pin
Nish Nishant26-Feb-02 14:14
sitebuilderNish Nishant26-Feb-02 14:14 
GeneralRe: Bad Form? Pin
Neville Franks26-Feb-02 23:49
Neville Franks26-Feb-02 23:49 
GeneralRe: Bad Form? Pin
Bruce Duncan27-Feb-02 8:24
Bruce Duncan27-Feb-02 8:24 

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.