Best way to port MFC COM Server to Managed code - c#

I am working on an application suite comprising of multiple Automation servers written using MFC and this is legacy code. These apps inter communicate via COM interfaces and other events. Most of these apps provide multiple form views with various input controls to capture information. I was wondering what would be the best way to migrate these applications to managed code one app at a time. Any advice...
We work in native code, but porting the code base to managed world is my personal idea because I can see a lot of functionality that can be achieved in a better way if the code is written as managed code.
Thanks,
Byte

You could write new parts of server using C++/CLI. It looks like a less painful option.

Related

Two way communication between MATLAB and C#

I have a third party MATLAB toolbox, which performs some calculation over set of data. I am designing a Windows Form based application in C# to integrate it with MATLAB Toolbox. Following are the primary requirements-
The Windows Form i.e C# application can be invoked from MATLAB
If uses performs any operation in the Windows Form, the Form should be able to call the method and pass the parameters to MATLAB Toolbox. The Form should be able to get the data returned by the MATLAB as well.
Also If user modify the data/parameters in MATLAB then it should be reflected in Windows Form.
From Google Search, I got plenty of results to call C# application from MATLAB by using COM approach. I am looking for two way communication between MATLAB and C#. Please note that the communication should be fastest as possible.
I've tried to do something related to this, and you can see the solution I arrived at in this question. Ultimately, you're gonna have to use COM if you plan to have any meaningful interaction between the two programs. I've looked into UIAutomation but unfortunately Matlab exposes nothing you can use.
To address your questions individually:
Should be pretty simple using the shell escape to call your C# app. Your app should support command line arguments, or use some kind of interop to pass anything to a running instance of your app (see 3).
This is also doable. See the linked question for how to interop with a running instance of Matlab from C#.
This is going to be the hard part. Your going to have to write your own COM server in your app and create a COM client in Matlab to interop with your app. This won't be automatic though since your user will have to type some kind of command to initiate the interop from Matlab. The only way I can think of making this automatic would be to have your app poll Matlab's workspace using COM in fixed time intervals, and figure out if anything has changed (neither simple nor efficient).
The conclusion? This is generally way too much work to be bothered with and you're much better off just writing a Matlab GUI. If you need any functionality from the .NET framework you can simply use Matlab's .NET interface.

Looking for a cross-platform, end-to-end example of C++ to WPF/Cocoa interoperability

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.
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.

Porting C++ code to Silverlight

I have C++ application which has UI developed using MFC, does some networking using sockets (using boost libraries) and some image processing. I want to move this application into Silvelight framework (I can use 4.0 if required) so that it can be used over the internet easily. Here I want to move all parts (UI + networking etc) in to C# but keep the image processing code in unmanaged C++ only. I don't know .NET framework yet, but whatever I have read so far suggests that it is not possible to call the unmanaged code from a silvelight web application. Is my understanding correct? Can something be done to achieve what I am trying to do? Also, if somebody has some suggestions on how to go about porting the code?
Silverlight 4 supports COM when running in trusted mode.
So, tecnically you could have Silverlight call your c++ library using COM.
The main problem I see is on deployment and I don't think it's a good idea.
Also, remember that Silverlight can run on Macs but COM is Windows only.
What you could do is to have the image processing happening on the server, but then you
can run into scalability issues.
Transfering large amounts of data between client and server can become an issue.
UI response should probably be closely evaluated too.
Regarding porting the code, well, you are most certain looking at a complete rewrite.
I'd suggest using WPF instead of Silverlight if possible. With WPF you'd be able to reuse your C++ as is because desktop .NET / unmanaged interop is very good (P/Invoke or CLI/C++).
Otherwise you are stuck porting your C++ to managed code, which could prove to be laborious and the resulting code inefficient.

Visual Studio 2008 - Sharing Data in C++/C# Multi Project Solution

I have a solution with 2 projects:
a c++ main project
a c# project (display simulator)
Today these 2 apps share data using a loopback TCP client/server connection, but that's not very optimal (timing issues..).
I was wondering if there was a way to access the c# data from the c++ project directly and vice versa? (It seems to be possible with 2 c# projects..)
If it's not possible, what's the best way to implement this with shared memory?
thanks!
Michael
EDIT: thanks for the answers. The 2 projects used to be independant solutions and are both executables - I'm actually trying to merge the 2 into 1 solution / executable.
For info: The c++ app is a PC version of an embedded app - the c# app is a lcd/HMI simulator.
Converting the C++ project to a C++/CLI project might be the easiest way to go. Note however that some code doesn't play well with C++/CLI (we've had problems using libraries that use boost::thread in a managed executable).
You can use COM Interop or Platform Invoke to access native code in C#.
If that's not what you're asking for, please elaborate.
Named Pipes?
For interprocess communication via named pipes you can check out the .NET 3.5 feature
http://msdn.microsoft.com/en-us/library/system.io.pipes.aspx for the C# side. From the C++ side, I assume you know the equivalent :).
There are two ways I know of to get direct access to memory between c++ and c# without the overhead of marshaling/demarshaling and com. If it is required to keep the c++ part of your code native then the only way I know to achieve this is to host the clr from your c++ application. A relatively complicated undertaking. Google "hosting the common language runtime". Then you can load your c# code as an assembly, call into the c# code and provide a common shared memory style interface. Although you will have to write all of the shared memory support yourself as I have found no direct support for shared memory in c#/.net.
The other way is to compile your c++ code with common language runtime support. This is by far easier and will allow you all the power and glory of c++ while allowing access to clr based data types, and the ability to call back and forth between your c++ and c# code. See "Language Features for Targeting the CLR" in your VS2008 documentation. pin_ptr will become your close friend and ally in this process.

What is the best way to pass information from a C# app to a C++ app while both are running?

What would be the best way to pass information from a windows forms C# app to a MFC C++ app while they are running? I don't need to send much, just a small string.
Thanks,
Jeff
Going down the list of IPC options:
Memory mapped files. Easy in C++, tough in C# without pointers, awkward handshaking
WM_COPYDATA. Easy in both, tricky to find the window handle you'll need
Clipboard. Easy in both, very awkward handshaking
COM. Out-of-proc is a beast, forget about it
Mailslots. Not suitable for one-to-one communication
Pipes. Easy in .NET 3.5, do-able in C++ but a bit tricky to get right
Sockets. Easy in both, hard to pass up.
Use named pipes, see this posting.
If the C++ app has a main window, take a look at using the SendMessage function (via P/Invoke) in the C# app to send a WM_COPYDATA message to the C++ app.
http://msdn.microsoft.com/en-us/library/ms649011%28VS.85%29.aspx
MailSlot api is small and simple but requires P/Invoke to use from c# and can go outside your local machine so needs care.
see my answer here
Named pipes may well be more robust but this is an alternate.

Categories

Resources