title: Compiling C# to Native?
body: <p>I think I'm somewhat confused about compiling .NET byte-code to native code, or maybe I'm confused about the end result.  So please bear with me as I try to sort through what I think I understand so you can help me figure out what I'm missing.</p>
  
  <p><strong>What I'd like to do is compile my application written in C# down to regular native code</strong> like I'd get if I had written it in C.  My reasoning has nothing to do with performance, but rather with some degree of protection.  I understand that my end-goal is not impossible (or even really that difficult) to circumvent, but I just feel like reversing x86 assembly is more difficult than reversing what Reflector gives me.</p>
  
  <p>Right now if I throw my C# application into Reflector, I basically get my source-code back.  Typically when I throw my unmanaged C/C++ applications into IDAPro and use the HexRays decompiler, I don't quite get the same degree of decompilation back and I have to resort to wading through x86 disassembly to understand the logic flow.  It's my understanding that such great decompilation comes from Reflector due to the application being in MSIL instead of the more terse native code that HexRays tries to decompile. </p>
  
  <p>I have no concerns about the client machine still needing the .NET runtimes, I'm not trying to circumvent any of that.  I would like to run normal software obfuscation programs like <code>upx</code> on my program, and doing it as a .NET binary fails.</p>
  
  <p>It was my understanding from <a href="https://stackoverflow.com/questions/45702/is-there-some-way-to-compile-a-net-application-to-native-code">this</a> related question that <code>ngen</code> does what I want. I've tried using <code>ngen</code>.  But after copying the output file from the <code>C:\Windows\assemblies\...\applicationName.ni.exe</code> directory to somewhere I can double-click, and trying to run it produces an error about it not being "a valid Win32 application".  Further, when I toss the <code>applicationName.ni.exe</code> into Reflector, I get the same output as I did from just the <code>applicationName.exe</code>.  Since <code>applicationName.ni.exe</code> is supposed to be native code, I expected Reflector to error out, but it didn't.  If this the way I'm supposed to do this, why did Reflector still give me such a great decompilation?</p>
  
  <p>So, just to summarize my main question again: How can I compile my .NET program into a native binary that Reflector won't so easily decompile?  Or what's some best practices for protecting a product written in a .NET language from newbie reverse-engineers?</p>
  
  <p>If I need a different tool, I'd prefer something free and not something like <a href="http://www.codewall.net" rel="noreferrer">Codewall</a>.</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE:</strong> I understand that what I'm looking for might limit some of the features of the language like Reflection, but I think I'm fine with that.  None of my code does any explicit <code>Assembly.Load</code> calls or anything of the sort.  But couldn't those just be replaced with <code>GetProcAddress/LoadLibrary</code> calls anyway?</p>