Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Combining C# and C++

Posted on 2011-05-05
Medium Priority
Last Modified: 2013-12-16
I have a C++ DLL (MFC) that I would like to implement in a CSharp Managed application. C# would implement the GUI only.

Communication between the GUI and the class library beside parameterized arguments I would want to communicate via the Message Queue from the MFC threads to the C# GUI.
Any suggestions on how this might be implemented?
Question by:TimPeer
  • 3
  • 2

Expert Comment

ID: 35702836
Hmmm...the very first thing that comes to mind is that, assuming you can even load the DLL into the managed app's process space, is that the C++ DLL is going to be creating new threads outside the Managed executable, and that's a potentially serious problem for stability, to say nothing of memory management issues.

You might consider writing some sort of managed-code interface to broker between the two...otherwise this sounds like potential mineshaft of maintenance issues unless I'm just overlooking something obvious. If there are any COM-exposed objects in that DLL, you could at least leverage a COM-interop layer..


Author Comment

ID: 35703148
Thanks Dave,

I am leaning toward Visual C++ (Managed) and rewriting the DLL to use Managed C++. The DLL leverages the ODBC API and hasn't been a problem with memory management, but I know this approach will require extraordinary changes in code to implement the equivalent functionality in the MFC class. This has yet to be determined though.

Can you speak on the use of the message pump by and between the C++ and DLL (both managed)? I would also welcome any examples you can forward.

LVL 45

Expert Comment

ID: 35704158
A message queue (and message pump) typically require a window.  Is the functionality in the MFC dll you want to use something like a modal dialog?

You may not need to rewrite the MFC dll as managed code.  You could code a bridge dll (C++.net) which has both managed and unmanaged components.  The C# 'talks' to the managed C++ which in turn instructs the unmanaged MFC dll what to do.
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.


Author Comment

ID: 35704167
Thanks Andy,

My goal in using a message queue is to communicate status between worker threads and a GUI thread. The "GUI" main thread may not have a UI as it might be a service.

Bridging the DLL seems an interesting option. How would I configure this DLL? I considered using Sockets to communicate status instead of the message queue... but the message would permit communication from the GUI to abort cleanly.

LVL 45

Accepted Solution

AndyAinscow earned 2000 total points
ID: 35717912
>>My goal in using a message queue is to communicate status between worker threads and a GUI thread.

Talking about the C++ side.

My understanding of a worker thread is that it does not have a message pump, so sending message to it is not going to be trivial.

To communicate with a worker thread one has, in the thread, a loop that checks some sort of flag.  It could be a simple variable but it really is preferable to use a more robust object such as an event.

A GUI thread does have a message pump, so if the worker thread has the handle of a window in the GUI then it can post messages back to the GUI.

You might wish to look at:
which is an article here at EE adressing interthread communications.

>>The "GUI" main thread may not have a UI as it might be a service.
With that I suspect you couldn't use messages, I don't know if a socket is suitable in that case.

Author Closing Comment

ID: 35896453
Thanks to the group for your assistance with this.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question