30959

How to create C# Event to handle MFC Windows message from PostMessage()

Question:

I have a managed C++ DLL using WINSOCK. On receive it sends a custom message to a CWnd * via PostMessage(). This works fine when called from unmanaged C++. The target CWnd * is registered with the C++ class after construction using this code:

// Registers a window (CWnd *) to receive a message when a valid // incoming data packet is received on this UdpRetrySocket. void CUdpRetrySocket::RegOnReceive(CWnd *i_pOnReceiveWnd, UINT i_RecvMsgId = WM_USER_RECV_DATA_AVAIL) { m_pOnReceiveWnd = i_pOnReceiveWnd; m_RecvMsgId = i_RecvMsgId; }

Here's the code that Posts the message to CWnd *

// If there is a pending incoming packet and there is a window // registered for receive notification then post a message to it. if (m_bInPktPending && m_pOnReceiveWnd != NULL) m_pOnReceiveWnd->PostMessage(m_RecvMsgId, (WPARAM)m_RecvSocket.LocalSockAddrIn().Port(), (LPARAM)this );

I'm now using this CUdpRetrySocket class from a C# Windows Forms application. Questions:

<ol><li>

From C# Forms class how do I obtain a CWnd * to register with my C++ CUdpRetrySocket class

<a href="http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/bdfb2783-4111-4ebf-b9ed-218200c09d03" rel="nofollow">FOUND ANSWER #1 HERE</a>

</li> </ol><blockquote> // C++ Register Window Method void RegOnReceive(System::IntPtr i_Hwnd) { m_pOnReceiveWnd = CWnd::FromHandle((HWND)i_Hwnd.ToPointer()); } // C# Caller of Register Window Method class MyForm : Form { . . . cmdDev.RegOnReceive(Handle); </blockquote> <ol><li>

How do I create an event in my C# window to capture this custom MFC style message?

</li> <li>

I need the C# app to process packets even when window is minimized. Will the C# Forms Window still receive these messages if it is minimized?

</li> <li>

Is there a better way to do this?

</li> </ol>

Answer1:

Found answer to how to create event handler <a href="http://forums.codeguru.com/archive/index.php/t-341896.html" rel="nofollow">HERE</a><br /> You simply override the Form.WndProc() virtual method, test for the specific custom message ID and pass all others on to the base class for processing.

protected override void WndProc(ref System.Windows.Forms.Message message) { if (message.Msg == MY_CUSTOM_WINDOW_MSG_ID) { // PROCESS EVENT HERE } base.WndProc(ref message); }

Testing shows that the answer to the last question is YES. The message is sent and processed even when Form is minimized.

Recommend

  • Prevent Firefox Web Notifications from automatically Closing
  • Cordova/Phonegap File Transfer plugin
  • Detect Removable Media Ejection Request in Windows Service
  • How to handle push notification in firebase
  • How to Pass information Back in iOS when reversing a Gesture Recognizer using Swift?
  • Deselect table view row when returning to app
  • Alert when TFS team query results change
  • Why this orion subscription don't works as I want?
  • How to create email notification function in Odoo?
  • Need to display user's choices realtime in magento
  • Local Notifications for Corona SDK (Android)
  • Google Play Games not allowing user to join turn-based match
  • Android: Why I am not receiving the BOOT_COMPLETED intent?
  • MPI message received in different communicator
  • SVG background image in IE9
  • NSScrollView scroll downward when resizing?
  • Broadcast receiver / service in Android vs. iPhone
  • Samsung phones and Notification.FLAG_INSISTENT bug?
  • Creating a genericly typed Action at runtime
  • Android camera preview freezes when switching cameras?
  • Hive command line Select query time taken incorrect if its not map reduce job in the background
  • How to link a work load item with a commited change?
  • Android Notification FLAG_AUTO_CANCEL not working
  • How to capture or listen to browser notifications?
  • using a UILocalNotification to play a sound that's not in the app bundle
  • Title cutoff in tabbed page in Xamarin.Forms
  • Trouble downloading file from browser on Android
  • Reload UITableView from another tab
  • What are zone turns?
  • jQuery: replace() or wrap() http://name.tld/request_url?parameter with …?
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • PushKit for VOIP iOS apps
  • Why cepheus don't send int without quotes to orion?
  • Webgrid not refreshing after delete MVC
  • Free memory of cv::Mat loaded using FileStorage API
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize