Extending C++ app with C#

Started by
1 comment, last by Black Knight 15 years, 5 months ago
My world editor is done with c++ I started it a very long time ago like 5+ years so writing it all again in c# is not an option for me.However I would like to code some parts in c# as it will speed things up considerably. Letme give an example of what I'm looking for.Imagine a button on a tab in the world editor called "Player Editor".This button will launch a Dialog Box which will allow the user to edit everything related to the player this dialog will not have any 3d viewports so it wont need direct 3d.The dialog will have listviews buttons comboboxes etc.So I was thinking of coding the "Player Editor" in C# and running it from a button inside the C++ World Editor. Can this be done with ShellExecute function? Or is there another way. Also how should I return or pass values back from the c# dialog? Let say there is a button in the c# dialog called "Show Player".When this is clicked the c# dialog should send a message to the main c++ app and the main c++ app will zoom to the player in the 3d viewport. I think i need to pass the HWND of the c++ app to the c# dialog at startup and then send WM_USER messages from the c# app to the c++ app.Is there a better way? Hope I made sense heh [smile]
Advertisement
There's no need to ShellExecute into a seperate process, or anything like that. You can directly mix native and managed C++ code by using the /CLR compiler switch. So how it would work is you would write your "Player Editor" as a C# class that inherits from System.Windows.Forms.Form, and also write any needed support classes in C#. Then you would write a little bit of "glue" code in managed C++ that would "bridge the gap" between your native C++ and the Form you made. You would also need to make managed wrappers for any C++ classes that your C# code needs to access...fortunately this isn't very hard thanks to the /CLR switch.

I would have a look through the section on Interopability in the MSDN documentation, it describes in detail the various ways in which mananged and native code can work together. In particular you'll want to look at the section on C++ Interop. There's also a section on making a managed wrapper for a native class.

Oh and one more thing...if you need to have a native window launch a modal Form, you'll need to provide the window handle in the form of a IWin32Window. It's very simple, this blog entry shows you how (just the bit on the WindowWrapper class).
Hmm sounds like alot of work.
What would be the benefit of doing this with CLR over just making it another process? Would it make passing information between C++ c# easier?
If I make it another application ill just write C# and dont mess with managed C++ and I have no prior knowledge of managed C++.

This topic is closed to new replies.

Advertisement