Combining C# and C++

Posted on 2011-05-05
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
    LVL 7

    Expert Comment

    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

    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 43

    Expert Comment

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

    Author Comment

    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 43

    Accepted Solution

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

    Thanks to the group for your assistance with this.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now