5

First, some background:

I've decided to start a new project designed from the ground up to run on multiple platforms (Windows, iOS, OSX, Linux, Android). Since my background is mostly C++ I intend to write the core functionality using C++11. That being said, on each platform I'll need to write a platform-specific UI that can inter-operate with the C++ core.

The first platform I'm targeting is Windows (the second is iOS). I will have data stored in a SQLite database as well as user-supplied data that will be entered using a WPF DataGrid (inserting rows, manipulating existing data, etc.). This is my first time working with WPF (though I've used Windows Forms) and my first time working with C++ in a managed environment. I plan to make this an MVC-style architecture so in my mind SQLite is the Model, WPF is the View, and the C++ code is the Controller.

My question is this:

Are there any examples out there illustrating how to grab data from a C++ interface and display it using C# and WPF without destroying/mangling the C++ code itself (since it must work on multiple platforms)? I've read a tiny bit about P/Invoke (tedious, but works) and mixed assemblies (works, but will mangle my C++?) but Google hasn't been helpful when it comes to concrete examples (especially those involving populating WPF controls with data obtained via a C++ DLL).

Thanks!

EDIT: While searching for approaches to this problem I came across CXXI. I'm not very familiar with it but it seems like it may be a simple solution to my problem. Any thoughts? My grasp of all these concepts is limited.

Rotsiser Mho
  • 551
  • 2
  • 5
  • 19
  • 1
    I don't have any WPF experience, but I can tell you that what you're doing is going to be somewhat difficult. When you get to the OS X app (and I'm sure iOS is the same), you will have to write quite a bit of Objective-C code. I think it largely depends on the nature of the app you're making, but you're basically going to have to re-write much of it for each platform. –  Jul 03 '12 at 02:37
  • 1
    Thanks for your input Hassan. I know I've got a lot of work ahead of me but my goal is to code the UI natively for each platform. I feel that delivers the best user experience as well as the best learning experience :) – Rotsiser Mho Jul 03 '12 at 02:54
  • 1
    You're right, it does deliver the best user experience. And if the hardest part of the UI is the table, then I'd say you're in good shape. I hope someone actually answers now... –  Jul 03 '12 at 02:58
  • I think it is. In any case I'm confident I can use existing widgets on most platforms. Feel free to upvote if you're interested in the answer too ;-) – Rotsiser Mho Jul 03 '12 at 03:01
  • I'm still playing around with things. I'll post back when I have something useful. :) – Rotsiser Mho Dec 07 '12 at 20:59

1 Answers1

3

Having worked with both XAML and WinForms, I don't think you'll find WPF to be feel much more "native" than your WinForms experience. In both cases, everything is abstracted by .Net. But if you want to learn WPF, then go for it.

P\invoke works just fine as does COM. As for other ways of getting data from C++ to other languages, consider approaching this as an IPC problem rather than a language one. Run one process as the child of the other and look at sockets and libraries such as 0mq/clrzmq (see this for 0mq on iPhone--this for Android) and Thrift to get C++ talking to the other process.

With sockets/0mq, you can use xml/json/Protobuf/Protobuf-net to serialize and deserialize objects from one language to the other. With Protobuf, you'll only need to create one set of json-esque data objects and use the langauge-specific tools for generating code. That'll take care of your data objects across platforms and languages, allowing you focus on the fun stuff. Serialization will work nicely with sql, too. All of these technologies are easy to implement and each has a score of language implementations, making it simple to wire things up to your next GUI.

In regards to examples, it all depends on how you want to move your data between the runtimes.

Community
  • 1
  • 1
0x1mason
  • 757
  • 8
  • 20
  • Are you suggesting that WinForms may be easier? I just assumed the WPF DataGrid would be easiest to work with. I really like the idea of using IPC but for this project I think it's overly complex and overkill. Thanks for that though. Very interesting. At this point I'd still like to find an example without relying on external code (at least at runtime). CXXI looks interesting. – Rotsiser Mho Jul 03 '12 at 19:21
  • 1
    Winforms is much easier than WPF. Honestly, I think you'd be best off with Mono for your desktop GUIs, but it's your call. – 0x1mason Jul 04 '12 at 17:28
  • 1
    If it were me, I'd go with Mono or some other cross-platform GUI abstraction layer, but that's mostly because I don't enjoy UI work ;). That said, I appreciate what you're trying to do by learning how things work across platforms. Maybe you need to clarify what you mean by "native"--are you most interested in trying different technologies or are you most interested in getting down to the nuts and bolts on each OS? – 0x1mason Jul 04 '12 at 17:45
  • I only just looked at CXXI--that looks great. I haven't worked with Mono in a while, it's really taken off. – 0x1mason Jul 04 '12 at 17:47
  • I still haven't decided which route I will take for the project in question, but I will say that this approach worked really well for me on a different project. Protobuf solved the serialization problem neatly and there are auto-generators for both C++ and C# as you mentioned. – Rotsiser Mho Jul 21 '13 at 23:18