June 29th, 2008
If you’re shipping a professional app with multiple users, you might have a need at times to ship customized levels of the apps according to the needs of the different user groups. Ex., you might need to ship a cut-down version of your application which may be sold for a lesser price, or given away as a demo. Kind of an ‘Express’, or ‘Lite’ version.
One way to do this is to copy the entire project to a different location, then remove those selected features and re-compile the app. This way however whenever you upgrade the application, or make modifications, you’ll have to do that at two places. Not a very effective technique then.
Another way is to set up regular constants and flags that you can use in your code to determine whether a certain feature should be activated. While this removes the upgrade problem in the technique above, it makes your application vulnerable. There’s a very high potential that a smart hacker will be able to debug your app, change a simple flag and get a full version instead of the trial. It’s been done too many times.
That’s why it’s better to use Compiler constants which remove the shortcomings of the above two techniques.
What is a compiler constant/directive?
In a program, most of the code you write targets the user. Be it your business logic, or interface validation, ultimately you’re talking to the user, but when you write compiler directives, or make use of compiler constants, you’re talking to the compiler.
Using compiler constants/symbols, you can set up variables that the compiler can access at compile time, and using compiler directives you can take action according to the state of these variables. You can ask the compiler to compile something, or not to compile something, or compile in a certain manner.
Here’s how you can do this
Set up a compiler constant in your code that you will use to flag whether you want to compile the lite version, or the full version. A good place to put this is the code file of the main window of your application, or your main class file. Here’s what the variable could look like in C#: -
#define trial;
Then in your code file you can use the #if, #else, #elif and #endif to separate the code that should be compiled. Remember, this is c# code, but the technique is universal and you can apply it in almost any language, including VB, C++, Delphi and Java.
#if trial
MessageBox.Show("You need to buy the full version to access this feature.");
#else
DoSomething();
#endif
The code is self explanatory. If the compiler variable is set, you can show the user something entirely different. Maybe a limited feature, a nagscreen, or a message requesting them to purchase your software. This is totally secure, because you will be compiling a different trial version, and since your registered code is not going to be there, it will be very hard for someone to debug your application to remove the nag-screen.
When you need to compile the full version, just comment out the constant declaration like this: -
//#define trial;
Now when you compile your app, the compiler will not be able to find the trial variable and will thus compile the version which has full functionality.
So now you know how you can use this great approach to make separate versions of your software based on compiler constants. This can be a very powerful technique if put to good use. There’s only one limitation, you can’t have global compiler constants (at least not in C# and VB.Net). You’ll have to declare the variable in every file anew.
If you liked this article, or want to know something, drop me a line.
Posted in C#, Programming, VB.Net | No Comments »