1

I have something like this in my code that checks for user's license:

// C# code:

#if DEBUG
    MakeLicenseValidForever();
#else
    CheckLicense();
#endif

Now, I need to know if these directives get saved in my released binary or not. If they do get saved, then a user can make #if DEBUG return true and bypass the checks. I need to know if #if DEBUG is safe.

PS1: Obviously I use release mode for distribution.

PS2: I asked my question here first, but since it's not getting any attention, I ask it here again!

Gudarzi
  • 486
  • 3
  • 7
  • 22
  • This is probably worth reading. https://stackoverflow.com/questions/2104099/c-sharp-if-then-directives-for-debug-vs-release – Kroepniek Oct 31 '22 at 08:21
  • @Kroepniek I've already read it and I couldn't get a definitive answer for my question! – Gudarzi Oct 31 '22 at 08:22
  • 1
    The chances that you will create unbreakable licensing checks in a client executable when the games industry *as a whole* struggled for decades is extremely slim. Best options are 1) Keep the code on your server, 2) Buy a product that has been hardened over years of development or 3) Accept that piracy *may* be an issue but chances are slim and customers won't be picking your product because it has the best licensing checks (i.e. opportunity costs suggest working on something more useful to your users) – Damien_The_Unbeliever Oct 31 '22 at 08:30
  • 1
    And also importantly, the whole code of the `MakeLicenseValidForever()` function shall also be put inside `#if DEBUG` condition. Because you cannot change compilation condition at runtime, but a skilled hacker can definitely call a function directly (well, if compilation is not aggressive enough to remove unused code). – Pierre Oct 31 '22 at 08:33
  • @Damien_The_Unbeliever thanks for the advice. it's a small app and i just want to prevent junior crackers from messing with it. – Gudarzi Oct 31 '22 at 08:37
  • @Pierre very very nice point. i will definitely do that. – Gudarzi Oct 31 '22 at 08:38

2 Answers2

2

No, preprocessor directives are processed at compile time, namely, the compiler won't compile the section into the resulting binary. It's more like code comments, which are discarded during compilation, you can even put invalid statement there and the compiler won't complain about it.

In general, however, your code would be compiled to intermediate language(IL), which isn't native machine code and be decompiled pretty easily. If you want protection better use some AOT compilation technology or obfuscator.

Sardelka
  • 354
  • 3
  • 9
1

C# is a language which is compiled into byte code, eg your statements are getting processed during compilation. Conditional statements, used by compiler, are accessable and compilable only if they are defined. In another words, compiler do not see any other statements and your code is safe

Lonli-Lokli
  • 3,357
  • 1
  • 24
  • 40