Communication between C++ DLL and a C# GUI

This is a discussion on Communication between C++ DLL and a C# GUI within the C++ Programming forums, part of the General Programming Boards category; So here is the scenario: Another member on the team is responsible for a GUI that is being programmed in ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    Communication between C++ DLL and a C# GUI

    So here is the scenario:

    Another member on the team is responsible for a GUI that is being programmed in C#. This GUI runs a DLL that has been coded in C++/CLI and retrieves information from the DLL.

    All that is working fine. However, now there is a portion of networking code in the C++ DLL that continuously listens for new messages. When a message is received, the C# GUI needs to be notified in order to gather the information that was taken in via the TCP connection inside of the DLL.

    Any pointers as to how I can communicate back up to the GUI within the c++ dll? The C# GUI has an instance of the C++ DLL, so I will need to travel the message backwards. If this was both c++, i would simply use a function pointer. However, my novice understanding of C# tells me that will not be possible. My goal is to trigger an event in some manner so that the C# GUI will know to grab the info.

    Note: I know this is a bad system design, however, this is how it has to be done. If I had my way, the networking portion would have been done in C# on the gui end. So please, don't go after the architecture here, it is not something I can control.


    Thank you



    Edit: This is probably more of a "Windows" topic, my apologies if I used the wrong forum.
    Last edited by JeremyCAFE; 11-28-2007 at 02:44 PM. Reason: Clearification

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Why not fire an event then. Expose the event in C++ and have the C# have an event listener.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    You can use pointers in C# in unsafe code sections. This may be your easiest solution although not highly recommended since both C# and C++/CLI are 'safe'. The way I handled my problem was creating an ATL COM object that wrapped the calls to the C++ DLL. The functions then would return their values on the stack and I had no issues.

    However your problem is different. You actually need to have the DLL call the C# module instead of the other way around. I know you can do pointers in C# using System.Interop.Marshal but I'm not sure about function pointers. At the low level there is no difference because they both point to something but in the end you want to jump to the address it points to. C++ understand this call but C# does not. C# methods are not called via this call so I'm not sure how you are going to call from C++ to C# since C++ will be using this call.

    The main thing in your favor is that your C++ DLL is CLI compliant and mine was unmanaged. Perhaps the internet holds a solution. I would google the heck outta that as I'm sure you have already.

    I've never tried to pass a method in C# via the ref keyword and I doubt seriously it would work or compile. I'm not sure how you would 'register' your C# method with C++ DLL. How would it know who to call? The event suggestion sounds much neater but I'm not sure how to get that working either. Your common ground between the two is the .NET framework so that is where I would turn for help.
    Last edited by VirtualAce; 11-28-2007 at 05:08 PM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by prog-bman View Post
    Why not fire an event then. Expose the event in C++ and have the C# have an event listener.
    Like i said, if there is a way of doing that I would love to know how

    My understanding of events are simply passing a pointer to a function that will act as a callback function when a specific action has occurred. I do not know how to pass that function pointer to the dll in order to trigger it when the action has triggered the event.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm rusty on C# and have basically no C++/CLI knowledge, but something like this should work.

    DLL Code:
    Code:
    public delegate void ConnectionCallback();
    
    public ref class ClassWithNetworkStuff
    {
    private:
      ConnectionCallback ^m_IncomingConnection;
    
      void gotConnection()
      {
        m_IncomingConnection->Invoke();
      }
    public:
      ClassWithNetworkStuff() { m_IncomingConnection = gcnew ConnectionCallback(); }
    
      property ConnectionCallback ^IncomingConnection
      {
        ConnectionCallback ^get() { return m_IncomingConnection; }
      }
    };
    C# app:
    Code:
    ClassWithNetworkStuff cwns = new ClassWithNetworkStuff();
    cwns.IncomingConnection += new ConnectionCallback(this, someMethodOrOther);
    cwns.createListener();
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Thank you, I will give this a shot.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21