views:

660

answers:

9

I learned windows programming using Visual C++, and the Win32 API. Nowadays, it seems most apps are being developed in .NET using C#. I understand that most of the time there isn't much performance difference between native code and managed code. So I'm wondering, if I were to start writing a new desktop app today, is there any reason (other than the fact that I'm more familiar with C++), that I might want to write it in non-managed C++ instead of .NET? Are there still some advantages to using C++ and native code? Or has that method been more-or-less replaced with .NET on the Windows platform?

Of course I know that people who are writing low-level device drivers and similar programs wouldn't do it in .NET. I'm asking with reference to typical client-facing apps that don't make direct hardware calls.

+2  A: 

Memory footprint. But unless you're developing for a severely handicapped machine memory-wise, it really shouldn't be an issue for most applications.

Nicholas Mancuso
.NET tends to "hog" memory and not release it back to the OS unless the latter's available memory falls below a threshold. 'Tis OK if your there will be few instances of your app running concurrently, using most of the resources for it. In other situations, it's a disaster.
Joe Pineda
+10  A: 
  • Performance (certain situations, such as graphics)
  • Memory footprint (as Mancuso said)
  • Use of existing libraries
  • No need for a runtime
  • Finer control

To list a few.

However, you may also want to look at the question from the opposite angle to fairly evaluate which language to use.

Additionally, you could use C++/CLI to incorporate both native and .net code.

JTA
+8  A: 

If your application needs to be able to run without an installation (i.e. if you can't or shouldn't do something like install the .NET framework), you can't count on .NET being on a windows machine (pre-Vista). Lots of utility applications can fall in this category.

Daniel LeCheminant
+1  A: 

Given your parameters, no, I see no reason.

Andrew Cowenhoven
+12  A: 

IMO the most important one for small downloadable applications is that native code does not need the .NET runtime. While broadband becomes more and more common not nearly everybody has it yet.

Some people may be disappointed to see that your 2 MB application actually requires another 20MB of framework download and a bothersome installation process to run. If they are not sure whether or not they really need your application in the first place, they might just delete it before even giving it a try and turn to a competing product.

Adrian Grigore
I wish I could up-vote this more times. When I want to try a new app and it asks me to install a runtime I don't have already installed, I immediately try something else.
Joe Pineda
How often do you install a new version of .NET runtime or the JRE? I think that I installed .NET 3.5SP1 back in August 2008.
olli
Olli: You are a developer, which makes you the elite of computer userrs. Unless your customers are also developers (and especially if they are unexperienced users who barely use their computer), about half of them will probably not have the runtime installed.
Adrian Grigore
One could bootstrap the framework - but that turns your 2mb app into a 20mb app. The upside is that the user isn't aware that they're installing the framework.
SnOrfus
As I user I would want the installer to download and install the framework for me if necessary so that the original download is not bloated with the framework. This way I can download the application faster if I already have the framework. Still, it's far from perfect. A native application is definitely better here.
Adrian Grigore
+3  A: 

If you can afford the dependency on the stack, go for .NET Modern, elegant, powerful and as a result much quicker to develop for.

But realize that you chain your app to it - to the language and the framework, if you forsee a future where you may want to escape this, then better think twice.

Win32 is old and clunky, but it works on virtually any Windows version without extra dependencies, and your code can be in plain, portable, C/C++.

nixps
+2  A: 

+1 for not having to require a .NET package/install on the target machine(s). This is still a big issue.

When all machines have mono or NET it won't be such a big deal.

Tim
A: 

Two things that I can think of.

  1. Protection of intellectual property. It's infinitely harder for someone to reverse engineer an Unmanaged C++ app. Managed .Net or Java apps can be easily de-compiled this is not the case with Unmanaged C++.

  2. Speed. C++ is closer to hardware and has a smaller memory footprint as the other comment mentioned. This is why most video games continue to be written in C++ and inline assembly.

James
Comparing apples to bananas. C++ is a multi-platform language, which can be used to create code targeting the .NET environment. This app would be as easily decompiled as a c#/CLR or Java/JVM one. And then, if I recall it correctly, GCC can compile java to native code, so this point is moot.
Joe Pineda
Actually it's not moot at all, it all depends on which compiler you use. You're point is true if the code is compiled to managed code. But if you compile to unmanged code then my point still stands.
James
To protect your intellectual property you can use obfuscation techniques in managed assemblies. That's not fool proof, but native code can be disassembled, too. Thus, I don't see an advantage for the native world here.
olli
It's hardly a protection the obfuscation techniques for managed code are weak by comparison. Have you ever tried to disassemble native code back into working C++? Trust me, it is infinitely harder to do than to reverse engineer a managed app where there is any free tools that do this now.
James
I wish I could edit my comment. I meant to say there are many free tools that do this now. So there definitely is an advantage if you want to write something that has stronger code protection.
James
Are you really developing something so new that you even need to worry about obfuscation? Why not allow people to extend your apps for their needs by inheriting from your Assembly (DLL or EXE)?
Matthew Whited
+9  A: 

I would recommend to write every desktop application in managed code. .NET/C# is a great platform to do so.

My reasons:

  1. Performance penalty is negligible. Google for benchmarks if you don't take my word. What matters more is the code itself. You can write O(n^m) algorithms in C++ or .NET/C#. JIT engines are very mature these days.
  2. Unmanaged C++ has major drawbacks when it comes to unit testing, mocking and refactoring. It's very cumbersome and inflexible. Reflection allows managed code to make such things very convenient.
  3. Deployment is a small issue. However, creating a setup which checks for the necessary .NET preconditions and installs them automatically is a no-brainer.
  4. Compilation is quicker, no linker! It even happens in the background when you edit the code.
  5. .NET library support is way better and cleaner than STL, MFC and boost.
  6. No header files and macros. They are just error prone.
  7. Security! Good bye buffer overflows, bad pointers, uninitialized variables...
  8. Exceptions. Clear exception hierarchy in .NET. C++ exceptions are messed up.
olli
Nothing could be farther from the truth. The performance penalty of running is a managed environment is huge. You have little or no control over how memory is managed this is a disaster for apps that need Real Time performance like video games. Managed solutions are usually best for business apps.
James
And you are stuck with a windows only solution. Too bad for all those Mac users...
Granted Unmanaged C++ does not make sense for every situation. But it does still have its advantages in certain scenarios per the intention of the original question. You would think from all these comments that Unmanaged C++ has no place anymore (not true).
James
There are alternatives to .Net that are cross platform and still managed (e.g. Mono, Java).
Mark
@James, granted C++ has its place for video codecs, video games and real time scenarios. But definitely not for desktop apps, web apps, business apps and so forth.
olli
@wdu, writing a native C/C++ app that is portable is black art. It requires a lot of effort. Achieving the same with mono/.NET and Java is easier.
olli
This answer has a number of good points, but I accepted James Atkinson's answer because it more directly answers the question. What I really wanted to know was "what place does unmanaged C++ still have a .NET world".
Joshua Carmody
I agree with James. The memory overhead for .net apps is massive. Look at Qt (now with LGPL) for a good unmanaged cross-platform solution.
Nick
Again, I cannot disagree more on that point. Yes, there is an overhead due to GC, but on modern PCs with ~4GB of RAM it's just pointless to talk about memory issues in *standard* desktop apps. Memory handling in unmanaged code is error prone and costs time and thus money.
olli
You guys that are saving managed code can't handle games really need to check out XNA. True 3D games in Java suck but XNA is amazing and easy to work with.
Matthew Whited