Microsoft recently released tools and documentation for its new Phone 7 platform, which to the dismay of those who have a big C++ codebase (like me) doesn't support native development anymore. Although I've found speculation about this decision being reversed, I doubt it. So I was thinking how viable would be to make this codebase available to Phone 7 by adapting it to compile under C++/CLI. Of course the user interface parts couldn't be ported, but I'm not sure about the rest. Anyone had a similar experience? I'm not talking about code that does heavy low-level stuff - but there's a quite frequent use of templates and smart pointers.
-
6What?!? No native c++?– KonradMar 16, 2010 at 17:20
-
Right, you have to use either Silverlight or XNA, and both are based on .net framework. theregister.co.uk/2010/03/16/microsoft_windows_phone_7_details– Fabio CeconelloMar 16, 2010 at 18:40
-
1According to this Ars Technica "op-ed", maybe there's some hope that MS will revert that decision. We'll see... arstechnica.com/microsoft/news/2010/03/…– Fabio CeconelloMar 29, 2010 at 16:48
-
See stackoverflow.com/questions/4539876/…– Seva AlekseyevJun 24, 2011 at 18:05
-
1It appears Microsoft is finally opening up the possibility of native C/C++ support in the near future: wpdev.uservoice.com/forums/110705-app-platform/suggestions/…– Fabio CeconelloDec 29, 2011 at 11:14
6 Answers
c++/cli can theoretically be used with WPF/Silverlight using the trick of replacing the C# generated from the XAML with a macro definition that can be used inside the main class in a code behind file. I worked out this technique but haven't had the motivation to take it beyond theory - I'm quite happy mixing languages.
As far as using c++/cli in a pure safe mode for your logic code, this may still not be possible but I'd love to hear how someone goes trying it now. Whilst researching it for Silverlight back in 2008 I found this daunting silverlight forum comment:
I just gave Silverlight&C++ it a try by compiling the MSIL from my C++ project into a Silverlight-compatible DLL. The good news: it works, and you can call this code from a Silverlight project. The bad news: The C++ compiler apparently uses MSIL instructions that Silverlight disallows.
So, if you try this, even with the simplest of programs, you'll almost immediately get the exception "Operation could destabilize the runtime." To me, this makes it seem less likely that we'll see Silverlight for C++ soon, as the compiler will need to behave quite a bit differently.
You can generate verifiable managed code in C++/CLI using the /clr:safe option. The problem is that most of your normal c++ code will not compile with that option.
-
1Could you elaborate a little more? What kinds of constructs, is it viable to make workarounds via conditional compilation, etc. Mar 16, 2010 at 15:24
C# is currently the only supported language for WinPhone7.
I fully expect that MS will add support for VB and C++/CLI in the future too, but don't expect to open up the native-code kimono anytime soon.
Native code just has too many issues to overcome, specifically around security, reliability, etc. Managed code is FAR easier to statically verify and FAR easier to control while running.
If you're upset about porting C++ code to C#, just be glad MS didn't force you to have to move to Objective-C ;)
From our own experience, the proces of porting well-written C++ to C# actually takes a lot less effort than one might at first expect. Sure, there's a learning curve, but you have that with any port. We actually got so much benefit from porting our core app and data engines to C# that we re-tooled our entire team to code in C# and port our C# back to C++ where necessary rather than the other way around! So far, we've only ported two modules back to C++ and call our C# code from our native code via interop instead.
Again, remember, WinPhone is a brand new platform using best of breed, highly-productive, next-generation development tools and platforms. It is not your father's WinMo.
If support for C++ is something you find to be crucially important, then make sure MS know - (respectfully and professionally) state your position in the MSDN forums and at developer events near you.
Update1: 2012-12-17:
While native C++ still isn't officially supported for Windows Phone 7, Windows Phone 8 now supports native C++ code so you can more easily port your existing C++ codebase(s) to Windows Phone 8 (as well as Windows 8 and Windows desktop apps).
While there isn't 100% compatibility between the Windows8/Phone8 platforms and API's right now, I expect the two platforms to become increasingly integrated over the next couple of releases.
This is especially true now that one of the key barriers to closer cooperation between Windows and other groups at Microsoft recently left the company ;)
Update2: 4/15/2014:
As per the recent announcements at //BUILD/ 2014, you can now start building "universal" apps in C++ & XAML, C#/VB & XAML or JavaScript & HTML that will run on Windows 8.1, Windows Phone 8.1 and Xbox One! For more details on building Windows Phone 8.1 Universal Apps, read this article.
-
3What do you mean with well-written C++ ? There's a lot of ways to do C++. And correctness depends on what you are using it for. So I guess, "well-written C++" is a bit subjective..." On the other side, Objective-C++ does fully support C++, and there seem to be a lot of devs relying on it for performance intensive ( or big-library based apps )– GolesNov 17, 2010 at 21:14
-
3What I mean by "well written C++" is that the code is maintainable, follows sound engineering practices, doesn't leak, doesn't block threads, doesn't depend on machine-specific aspects like byte ordering or machine word length, etc. Jan 11, 2011 at 1:59
-
Apple does not force you to use Objective-C, it's just their GUI-language. It's perfectly fine to use C++ for iOS development or even mix both languages with Objective-C++. But MS forces me to port all my code to a new, well but similar language, which is also an island (MS only). So I have still a lot of work for porting and testing, and maintaing two code-bases when changes are done. This is not nice by MS. Dec 23, 2012 at 10:49
-
1This argument is now moot. You can now write Windows Phone 8 apps in C++ or C#/.NET. Oh, and in case you've not seen it yet, Mono supports C# on iOS/OSX/Linux/Android/etc. so it's far from a Microsoft-only platform. What non-Apple platforms wholly support Objective-C? Mar 12, 2013 at 4:55
-
1@asveikau: While you can compile Objective-C for non-iOS/OSX platforms, there's practically no support for Obejective-C outside of the Apple ecosystem & platforms. Jul 25, 2013 at 22:40
The whole development idea is built on Silverlight. I think you can add your managed dll written in C++ without any problem to this Silverlight project, but it could not use native code.
I am planning to install the tools on my machine tonight and will try this out.
-
OK, I was guessing about that. But how much work it would be to make a typical modern C++ codebase (templates, smart pointers) compile under C++/CLI? Notice that it's not my idea to lose the ability to keep compiling native (to those platforms that support it, like WM6 and iPhone), but be able to mantain only one set of sources. Mar 16, 2010 at 15:13
-
1I would say that is closer to a complete rewrite than to a simple port.– gyuriscMar 16, 2010 at 18:23
It is fine if MS decides to leave the path and create something new, that is MS' decision. So let's face the facts. Silverlight is no success yet. MS lost significant share due to Apple, Android and RIM. Application developers simply have to evaluate the business case for their own applications and decide if they trust in a share gain of Windows 7 phone or not. For the company that I run, we decided not to support any more MS Windows phone 7, not because of this or the other technical reason, but just because that we don't believe in the return of our investment for the port. We start supporting Apple, Symbian, Andoid and MeeGo in the future if we see a market success of this new platform. All support C/C++ and enable us to reuse our proven application cores. So why worry at all. Personal technology preferences should not be gating. If personal preferences worry, then I would kick MS out for their to me ugly looking UI.
Thomas
-
Of the platforms you chose to adopt, MeeGo and Symbian are likely to crumble before your very eyes. At the end of the day, Managed C++ compiles down to IL which is packaged into assemblies and executed by the CLR. If you're careful, you may well be able to get your code to port pretty cleanly to Managed C++ and thus run unhindered on WP7. If it doesn't work, however, you're on your own. Things to avoid include direct memory manipulation, pointer arithmetic, etc., but then you're avoiding such things anyhow, right? Jul 12, 2010 at 19:31
It is on the horizon finally!
So a survey sent to windows phone developers about their future development preferences and XNA isn't mentioned once in the Survey (A survey sent to windows phone developers - did I mention that)
They do however ask:
How would you prefer to use C++ in your mobile apps/games?
- Develop apps/games that are C++ from top to bottom (UI, business logic, and platform APIs)
- Use C++ for business logic and then write platform abstraction layer
- Use C++ for business logic use 3rd party runtime engines
- I don’t want to use C++