dlg.DoModal(); // tadaaa! this is the line everyone's been waiting for!!!
I loved the tadaa in the source comments and it's because at that moment the penny should drop that the template has just been sitting in memory and wont do anything until you execute those two lines. It's much kinder on windows especially in a threaded app and less chance of making mistakes.
So when you get the message from your ethernet port all you have to do is execute those two lines and dialog flies out of memory and up on screen. You can actaully do everything else before that any earlier time.
Not sure how MFC does this but there is an lParam value that is part of the indirect and usually you pass a struct with your data you want loaded in the dialog handler in as well. It saves having to carry a global with your data.In your case I would imagine it would be a pointer to your payload data recieved. Again saves copy or moving any data.
It stuns me dlg.InitModalIndirect doesn't take a second parameter which is what I was expecting and where you would pass in your data pointer. So bit of mystery to me how you do that under MFC. Oh I wonder does dlg.InitModalIndirect have overrides and used as a shim on the indirect call to cater for that. In which case there will be a form of that you can pass a second parameter on.
Yes any dialog or window can be created from templates. The window one usually catches even seasoned programmers out. If you look at the template call CreateDialogIndirectParam the DLGPROC is optional. What you do is pass in the window handler and use setwindowlong to put it onto the modeless dialog which just happens to have all the style flags of a window. Once it has the handler attached you can just insert it into any APP window or even the desktop because it's a normal window and so you fake CreateDialogIndirectParam returning a fail. Did you notice it's up to you to call DestroyWindow and that is why they don't do it automatically. Sometimes it's useful to lie to the API
Yes its a template you can launch multiple copies as it only gets read and all global alloc memory is thread safe. It can't write or move anything as you usually only allocate just enough space to hold the resource. I assume that all still holds true for MFC. Officially the GlobalAlloc etc is now "old" but they have not got around to giving us replacement template system.
Most windows programmers are familiar with LoadResource and loading bitmap/icons/menus etc because they are going to draw them on screen alot but using the system for dialogs and windows usually escapes them because they don't need the response of a window like in your example.
The heap corruption indicates that you have an out of bound memory write access (e.g. writing to an array beyond it's size). Running out of memory indicates that you are allocating a lot of memory but not freeing it.
For modeless dialogs follow these adviceses:
Create a variable and initialise it (assigning NULL or using new)
When the dialog should be created the first time check if the variable is NULL and call new if so
Create the dialog
When finished with the dialog, call DestroyWindow()
If the allocated memory is not needed anymore, delete it and assign NULL to the variable
When the variable goes out of scope, delete the memory (e.g. in the destructor of the class holding the variable)
I suggest to check your code for the above (each new must have a corresponding delete, and each CDialog::Create must have a corresponding CDialog::DestroyWindow).
Because the memory corruption is a severe error, I suggest to find the source and fix it first. Use a debug build. Then you don't need the GFlags. They are for debugging executable files where you don't have the sources.
If you know the code portions (or have a guess) where the heap corruption occurs, you can insert some AfxCheckMemory[^] calls to check the C++ heap.
it is after do the create of the dialog and populate the control including allocating via new a Richedit and streaming data to it that something goes wrong all storage is allocated via new
A common error for such cases is forgetting to append a NULL char to the received data before passing them as string and/or forgetting to allocate the additional memory for the NULL char.
Get a resource explorer (search the web for one) to find out which resources are contained in the DLL. If you refer to the DLL for the Solitaire game shipped with older Windows versions it will contain only bitmaps.