I have a very weird issue with a program that receive a windows
message ( a WM_USER one) and do some job. Doing this job takes
several seconds and then it waits for the next message.
If a new message comes while doing the job, it is retained in the
queue until the job ends because there is only the main thread. All is
fine until here.

But to do some parts of this job I call two functions:
DMProcessConfigXML and SndSetSound in different pieces of code. Well,
if there is a message in the queue, it enters while one of these
functions are being processed. As there cannot be two copies of the
main thread because global variables, etc, the program crashes. If no
other message arrives while doing the job, all works fine. Also,
commenting out these two lines the crashes dissapear.
I've introduced loging to a file to see when the functions enter,
message comes, etc and I'm sure that all is how I am telling it, even
if it sounds weird.


The message queue is the usual one:
WinMain:

#define EXTERNAL_COMMAND WM_USER+100 //As a example
MSG msg;
while (GetMessage (&msg, NULL, 0, 0)) //Despachamos los mensajes.
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}


And the WinProc also is very straightforward:

LRESULT CALLBACK WndProc (HWND hwnd, UINT uimessage, WPARAM wParam,
LPARAM lParam)
{
TCHAR szParam[64];

switch (uimessage)
{
case EXTERNAL_COMMAND:
wsprintf(szParam,_T("%d"),(int)wParam);
ProcessParameter(szParam);
return 0;

case WM_CLOSE:
DestroyWindow (g_hWnd);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hwnd, uimessage, wParam, lParam) ;
}

void ProcessParameter(TCHAR szParam[64])
{
................
................
................
................
TCHAR szFavoriteXml[1024]={0};
lstrcpy(szFavoriteXml,
L"<wap-provisioningdoc> "
L" <characteristic type=\"Sync\"> "
L" <characteristic type=\"Settings\"> "
L" <parm-query name=\"PeakFrequency\" /> "
L" </characteristic> "
L" </characteristic>"
L"</wap-provisioningdoc>");

LPWSTR wszOutput = NULL;
HRESULT hr = E_FAIL;
// Process the XML.
hr = DMProcessConfigXML(szFavoriteXml, CFGFLAG_PROCESS,
&wszOutput); //Fallo 1
if (hr == E_FAIL)
return false;

delete [] wszOutput;
................
................
................
SNDFILEINFO sndfile = {0};// Initialize an empty SNDFILEINFO
structure

sndfile.sstType = SND_SOUNDTYPE_ON;
SndSetSound(SND_EVENT_ALL, &sndfile, true);
................
................
................
return;
}

Any idea will be wellcomed. Thanks!

Re: DMProcessConfigXML and SndSetSound makes the Windows message queue to work asynchronously by beemer

beemer
Wed Oct 10 03:37:09 PDT 2007

I've solved the issue running these pieces of code asinchronously in a
different thread, but it would be great if someone could give an
explanation!