UI Thread dummy window !?!?!?
-
Hi to all !!! I'm currently trying to code a Winsock server application. I'm using the main process thread to listen to incoming connection. When a connection is detected (i'm using WSAAsyncSelect) i spawn a new UI thread that will process that connection. I do not want a window to appear but i'm creating a dummy window because I need the message map... But the problem i'm having right now is that the messages for the WSAAsyncSelect goes to the dummy window and not to the UI thread message map... Here is how I create the dummy window... //------------------------------------------------- bool CConnection::InitDummyWnd(void) { m_pDummyDlg = new CDummyDlg; if (!m_pDummyDlg->Create(IDD_DUMMY, NULL)) return false; m_pMainWnd = (CWnd *)m_pDummyDlg; m_pMainWnd->SubclassWindow(m_pDummyDlg->GetSafeHwnd()); m_pMainWnd->ShowWindow(SW_SHOW); return true; } bool CConnection::PrepareWSAAsync(void) { if (WSAAsyncSelect(m_sock.getSocket(), m_pMainWnd->GetSafeHwnd(), WSA_THREADSOCKETEVENT, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR) { WORD err = WSAGetLastError(); return false; } return true; } //------------------------------------------------------------ How can I make sure that the thread message map gets all the winsock message instead of the dummy window message map ??? Thanks for any advice ! Luc B.
-
Hi to all !!! I'm currently trying to code a Winsock server application. I'm using the main process thread to listen to incoming connection. When a connection is detected (i'm using WSAAsyncSelect) i spawn a new UI thread that will process that connection. I do not want a window to appear but i'm creating a dummy window because I need the message map... But the problem i'm having right now is that the messages for the WSAAsyncSelect goes to the dummy window and not to the UI thread message map... Here is how I create the dummy window... //------------------------------------------------- bool CConnection::InitDummyWnd(void) { m_pDummyDlg = new CDummyDlg; if (!m_pDummyDlg->Create(IDD_DUMMY, NULL)) return false; m_pMainWnd = (CWnd *)m_pDummyDlg; m_pMainWnd->SubclassWindow(m_pDummyDlg->GetSafeHwnd()); m_pMainWnd->ShowWindow(SW_SHOW); return true; } bool CConnection::PrepareWSAAsync(void) { if (WSAAsyncSelect(m_sock.getSocket(), m_pMainWnd->GetSafeHwnd(), WSA_THREADSOCKETEVENT, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR) { WORD err = WSAGetLastError(); return false; } return true; } //------------------------------------------------------------ How can I make sure that the thread message map gets all the winsock message instead of the dummy window message map ??? Thanks for any advice ! Luc B.
It is the call
WSAAsyncSelect(m_sock.getSocket(), m_pMainWnd->GetSafeHwnd(),...)
that is telling Winsock to send notifications to your dummy window. The problem is thatWSAAsyncSelect
needs a validHWND
to send the notifications to. One workaround for this is havingPreTranslateMessage
overriden so that it intercepts messages intended to the dummy window from Winsock, like this:BOOL CConection::PreTranslateMessage(MSG *pMsg)
{
if(pMsg->message==WSA_THREADSOCKETEVENT){
pMsg->hwnd=0; // divert this message to the thread
}
return 0;
}Haven't checked it out myself, so it'd be great if you tell us back whether this works. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
It is the call
WSAAsyncSelect(m_sock.getSocket(), m_pMainWnd->GetSafeHwnd(),...)
that is telling Winsock to send notifications to your dummy window. The problem is thatWSAAsyncSelect
needs a validHWND
to send the notifications to. One workaround for this is havingPreTranslateMessage
overriden so that it intercepts messages intended to the dummy window from Winsock, like this:BOOL CConection::PreTranslateMessage(MSG *pMsg)
{
if(pMsg->message==WSA_THREADSOCKETEVENT){
pMsg->hwnd=0; // divert this message to the thread
}
return 0;
}Haven't checked it out myself, so it'd be great if you tell us back whether this works. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Nope doesn't work. My CConnection::PreTranslateMessage never gets called !?!?!
-
Nope doesn't work. My CConnection::PreTranslateMessage never gets called !?!?!
Hmmm... Do you mean
CConnection::PreTranslateMessage
never ever gets called (for this or for any other message) and yet the Winsock messages make their way to the dummy window message map? At least some standard messages delivered to the dummy window upon creation should go acrossPreTranslateMessage
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo