How to define and use precompiled variable in delphi directives

1.1k views Asked by At

I want to define a precompile string variable and use it in {$include} directive in delphi, for example:

{$define FILE_NAME "lockfile"}
{$include FILE_NAME'.txt.1'}
{$include FILE_NAME'.txt.2'}
...

For security reasons (this is part of our licensing system), we don't want to use normal strings and file reading functions. Is there any capability for this purpose in Delphi?

2

There are 2 answers

0
David Heffernan On BEST ANSWER

The $INCLUDE directive does not support indirection on the file name. So, the following code:

const
  someconst = 'foo';

{$INCLUDE someconst}

leads to the following error:

F1026 File not found: 'someconst.pas'

If you must use an include file, you must apply the indirection by some other means. One way could be to use the fact that the compiler will search for the included file by looking on the search path. So, if you place each client specific include file in a different directory, then you can add the client specific directory to the search path as part of your build process.

FWIW, I find it hard to believe that this will make your program more immune to hacking. I think that a more likely outcome is that your program will be just as susceptible to hacking, but that it will become much more difficult and error prone for you to build and distribute the program.

2
LDS On

You requirement may be better satisfied by the proper use of a VCS system. You need "branches" for every customer where customer-specific files contains customer-specific data. This will avoid to litter your code with complex directive to manage each customer - file names stays the same, just their content is different in each branch. Adding a new customer just requires to create a new branch and update files there.

Then you just need get each branch and compile it for each customer to get the final executable(s) with customer specific data built in.