9/21/2006

09-21-06 - 1

Windows programming tip : you should basically never use SendMessage() , you should use SendMessageTimeout() instead. If you SendMessage() to an app that is locked up or for some reason not processing messages, your app will become locked up, which is lame and dumb.

In fact you should probably just drop this in any place you use SendMessage() :

LRESULT SafeSendMessage(   
	HWND hWnd,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam)
{
	DWORD dwResult;
	if ( SendMessageTimeout(hWnd,Msg,wParam,lParam,SMTO_NORMAL,5000,&dwResult) )
	{
		return dwResult;
	}
	else
	{
		// timed out !
		// (not sure what a good error value is here, return values for SendMessage() seem very nonstandardized)
		return (-1) ;
	}
}

edit : I guess (0) is a better failure return because some important messages use TRUE/FALSE for success/failure. Stupid non-standardized message return values, this definitely is sucky. If your app is exception-safe, then a throw there is probably better because no return value works.

No comments:

old rants