Link to home
Start Free TrialLog in
Avatar of devon-lad
devon-lad

asked on

Tracking down ::PostMessage error

I have an MFC based dialog application that uses CEvents, CSyncObjects and CMultiLocks to ensure that only one instance is running.  This works fine.

If the user starts a second instance of the application by double clicking on an associated file, then the app recognises another instance is already running and posts a message to the first instance to load the file.  The second instance then exits.

In the Debug version this works without problems.

In the Release version this works fine the first time an associated file is double clicked, but if I double click the same file again, the application fails with an exception in MFC42.dll.

I've determined that the line of code causing the error is the one that posts the message to the first instance of the application.  I've put a try/catch block round the line to see if I can discover anything more, but no exception is caught.  But if I comment the line out, no error.

As the first instance of the app also exits after the error - I'm inclined to think that the exception is occuring in the first instance as a result of the posted message.  But if I put a try/catch block around the body of the message handler, there are no exceptions caught.

The following is the code used to find the main window of the first instance (after it is determined that another instance is running) and post the message:

HWND hMain = ::FindWindow("#32770", "MyApp");
if (hMain)
{
     ::PostMessage(hMain, MY_MESSAGE, 0, 0);
}

Where should I be looking to track down the cause of this error?

SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may refer this too regarding same :

How to send a user-defined message with SendMessage, PostMessage or PostThreadMessage
http://www.codeproject.com/tips/gbTestSDI.asp <=====

-MAHESH
Avatar of devon-lad
devon-lad

ASKER

Hi Mahesh,

I was using WM_USER as a base.  I've changed it to WM_APP - never realised that's the one I should be using.

However, this hasn't fixed the problem.  Still exactly the same error.

I've commented out all the code in the message handler and this gets rid of the error.  Sorry, should have thought about doing that before.

So it appears it isn't a PostMessage problem after all.

That being the case, should probably close this question.

Thanks for the input - at least I learnt about WM_APP :o)

Forget all that!

I was a bit hasty responding.

Still get the error when the event handler code is commented out - but it's not so consistent at appearing the second time I double click the associated file - sometimes it's the 3rd or 4th time.

So the error is definitely occurring in the first instance of the application when it's processing the posted message.  But it is happening somewhere other than the event handler.

Any suggestions where to look next?
Could you post the MY_MESSAGE handler? I mean, the MFC wrapper for it.

I would also suggest that you use a small application with a button that posts MY_MESSAGE to ::FindWindow("#32770", "MyApp") - it will make debugging easier.
Handler declared in CMyDlg as

afx_msg void OnLoadUpdateFile();

Message map entry:

ON_MESSAGE(MY_MESSAGE, OnLoadUpdateFile)

Event handler:

void CMyDlg::OnLoadUpdateFile()
{
/*
           Handler code commented out but error still occurs
*/
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As said by alexcohn... ON_MESSAGE handler should be like in format LRESULT HandleFunction(WPARAM, LPARAM).....

Nothing else will work.

NOTE : Fortunately, VS7 makes this a compilation error, rather than letting it go through. In VS6, you are simply required to follow the specification.

-MAHESH
That solved it...I was obviously missing some fundamental knowledge there.

And I'm still using VS6...which didn't help the situation.