How are portions of a program disabled in demo versions?

If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif: int save_document(char* doc_name) { #if DEMO_VERSION printf(“Sorry! You can’t save documents using the DEMO version of this program!n”); return(0); #endif ... }

Showing Answers 1 - 3 of 3 Answers

kbjarnason

  • Jul 2nd, 2010
 

The example given has a flaw.  It essentially works like this:

void SaveFile( char *fname, void *data )
{
#if DEMO
  puts( "Nope, sorry, no can do" );
   return;
#endif

   ... remaining normal save code goes here.
}


Here's the kicker: a trivial hack turns the puts/return bit into a series of NOPs, which do nothing at all, followed by... yes, you got it, the normal save code.  Voila, the demo code now saves.

What you should do is this:

void SaveFile( char *fname, void *data )
{
#ifdef DEMO
   puts( "No save for you!" );
   return;
#else
   ... normal save code
#endif
}


This way, if DEMO is defined at compile time, the normal save code is skipped entirely.  They can hack until they're blue in the face, but they can't enable the code if it simply isn't in the executable.

Simply recompiling _without_ DEMO defined turns off the demo message and re-enables the actual save code.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions