Sign in to follow this  

rename variables in release mode (like C++ #define)

This topic is 2078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using VB.NET 2008.
I'd like to change variable names from debug to release mode.


I know I can't use a #define preprocessor directive (like in C++)
[CODE]
#if DEBUG
# define MyVarName myDebugVarName
#else
# define MyVarName myReleaseVarName
#endif
[/CODE]
and I wonder if there's something I can do to have this kind of result.

I know I can write something like:
[CODE]
#If DEBUG Then
Dim myDebugVarName As Integer = 0
#Else
Dim myReleaseVarName As Integer = 0
#End If
[/CODE]
but this is very ugly since now every instruction (or block of instructions) dealing with that var has to be written 2 times, one for debug mode and one for release mode. E.g.:

[CODE]
#If DEBUG Then
myDebugVarName+=1
#Else
myReleaseVarName+=1
#End If
[/CODE]
or
[CODE]
#If DEBUG Then
if myDebugVarName > 2 Then
DoSomething(myDebugVarName)
Else
DoSomethingElse(myDebugVarName)
End If
#Else
If myReleaseVarName > 2 Then
DoSomething(myReleaseVarName)

Else
DoSomethingElse(myReleaseVarName)
End If
#End If
[/CODE]
The code becomes soon unreadable and it is easy to forget keep the couple of version in sync (violation of DRY principle). [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

Renaming variables in MSIL might be an option, but I've not found an easy way to do that. For example I imagine having a table with debug names and release names for each var (maybe a Dictionary) and call a translation of all the names in Release version (so I write only myDebugVarName in my source and in MSIL it changes in myReleaseVarName).
I think I'll apply your suggested methods to change names to some classes and methods.

I hope you see what I mean.
Thank you in advance for any help. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
You need an obfuscator.

[url="http://www.google.com/search?q=obfuscator+for+.net&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a"]Here's what Google says about it.[/url]

Share this post


Link to post
Share on other sites
I don't really see the purpose of doing that. Variable names do not appear in compiled executables, they are strictly for programmer use. As for interpreted languages, you are probably looking for an obfuscator like Alnite suggests (not that I condone the use of those tools).

But if you're desperate, there's always Replace All [img]http://public.gamedev.net//public/style_emoticons/default/mellow.png[/img]

Share this post


Link to post
Share on other sites
Thank you for your replies. :)
I'm already using an obfuscator (since the exe in .Net is just all the source code without comments) but a lot of classes dealing with WCF services cannot be renamed without compromising communication between clients and server (in fact obfuscator leaves them unchanged). That's why I'd like to change theese names manually. So I can have the class CCustomerInfoService in debug mode and something named C0945 in release mode (the same for model classes, all the methos and members).

I know obfuscators are not a (great) protection, but since it's not an open source project I don't want other to see so easily how I wrote my program, neither I want to give them too many insights about the class diagram I implemented. :P

"Replace All" as Bacterius suggests maybe is not a bad idea (or am I desperate? ;-) :D ): I'm thinking about a String.Replace done on my source code by a little utility as a pre-compilation step. That's will keep in sync release and debug version of source code (so I have no change in how I write programs), and names are changed automagically before compiling release version. If I forgot to rename something it will become a compiler error in release mode, so it should be easy to fix since I'll find a debug name not replaced with its counterpart in release mode.

I think to try this way. :)
Thank you again, your comments have been useful :)

Share this post


Link to post
Share on other sites
Unless you're writing something that is extremely popular, nobody is even going to bother to try to decompile your program. Most likely, you are wasting your time.

Share this post


Link to post
Share on other sites
[quote]since it's not an open source project[/quote]

It's written in .Net.

It's open source. .Net provides reflection, which, unlike C or C++, preserves all information by design. Renaming doesn't do a single thing.

Obfuscation in C or C++ isn't better because it generates native code, but because it's lossy. Both languages allow and encourage structural transformations, eliminating information needed to reconstruct the source. .Net (or Java, Python, etc) projects do not allow that.


Look at Minecraft. It's obfuscated, but people have been modding it since day one, working with original source directly.

Share this post


Link to post
Share on other sites
@wack: I hope it will be extremely popular [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] About decompilation I agree with you. I'll try to spend the least time possible doing renaming :P

@antheus:You're right. I regret a little to have used this kind of tecnology to develop my program. I think I'll rewrite it using VC++ to have native code, but at present I need to complete the first release as soon as possible and I'd like to discourage reading of my exe/source code by casual observers. :P

Thanks for sharing your thoughs. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
[quote name='Fil' timestamp='1333722922' post='4928790']
I regret a little to have used this kind of tecnology to develop my program.
[/quote]
Why? Looking at Antheus's example of Minecraft again, it's pretty clear that the technology choice hasn't hindered the success of the final product. If .NET is allowing you to quickly and easily create a correctly working program, then worrying about the final product being decompiled is probably a poor reason to change to C++.

Concentrate your efforts on creating a good product. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
[quote]Why? Looking at Antheus's example of Minecraft again, it's pretty clear that the technology choice hasn't hindered the success of the final product. If .NET is allowing you to quickly and easily create a correctly working program, then worrying about the final product being decompiled is probably a poor reason to change to C++.

Concentrate your efforts on creating a good product.[/quote]
+1.

Besides, unless your product is extremely specific then you don't need obfuscation. Obfuscation is for protecting proprietary corporate algorithms and (in general) preventing people from blatantly refactoring your entire code and taking credit for it. And if your product becomes immensely successful such as Minecraft (taking the same example again), then you probably don't need obfuscation anymore unless you really don't want to show people how you did your game (why wouldn't you want to share?) because everybody knows who the legitimate author is (now as to why he keeps obfuscating it, I guess he has his own reasons. I guess it keeps the amount of clones in check, which after all isn't a bad thing. But I digress).

And like Antheus outlines, obfuscation is not 100% effective. It will certainly prevent upwards of 99% of the people trying to read your code to do so, but the 1% or so who really want to read it, will find a way eventually (and you just need one person able to do it and willing to share his method for your obfuscation to become useless).

Share this post


Link to post
Share on other sites
@jbadams: Thank you: your words are very encouraging :P
@Bacterius: I haven't proprietary algorithms but it's a business software with (I think) its unique selling points... so I hate the idea my various classes can be reused by others in their programs (that's the source of my little regret). I know my (unique) features can be redone from scratch by competition, I just don't want to help them too much, something like "hey, that's the source code!". :(

With respect to Minecraft example, I'd say that because it's based on a community its source code is important only to mod developers: I imagined a clone of the game would be easy to spot for everyone... but I understand from Bacterius' reply that my reasonings are wrong :P

Anyway you're right: obfuscation is not so effective... even a friend of mine told me not to spend too much time in obfuscation since it's far more easy to understand a program analysing the data base schema instead of trying to unobfuscate or (in case of native code) to decompile an exe.

Thanks for your suggestions :)

Share this post


Link to post
Share on other sites

This topic is 2078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this