3
我有一個託管C++ DLL使用WINSOCK。在接收時,它通過PostMessage()向CWnd *發送自定義消息。這從非託管C++調用時很好。 *與C++類施工後使用此代碼註冊目標的CWnd:如何創建C#事件來處理來自PostMessage的MFC Windows消息()
// 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;
}
下面是帖子的消息的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
);
我現在使用從這個CUdpRetrySocket類的代碼C#Windows窗體應用程序。 問題:
從C#窗體類我如何獲得一個CWnd *與我的C註冊++ CUdpRetrySocket類
// 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);
如何在C#窗口中創建一個事件來捕獲這個自定義MFC風格的消息?
即使窗口最小化,我也需要C#應用程序來處理數據包。如果最小化,C#窗體窗口是否仍會收到這些消息?
有沒有更好的方法來做到這一點?