Todd Todd - 23 days ago 7
C++ Question

Windows message loop and server loop

I made a window using WinAPI and it needs to enter message loop:

while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}


Now how do I create a server and start receiving clients, if my main thread is processing UI messages?
I tried putting all server code in
ThreadProc
and call
CreateThread
from
WinMain
but it just crashes with:

Exception thrown at 0x00000000 in test.exe: 0xC0000005:
Access violation executing location 0x00000000.


Any help would be appreciated.

Answer

This is a very confusing question as it asks one question in the title, but the content addresses a different problem (threads crashing).

To address the primary question: Microsoft did add a way to handle socket communications in a GUI thread friendly way: WSAAsyncSelect. This will send socket events as messages to your applications message queue - typically an invisible window is created to handle the messages.