Programming note: PeekMessage

This posting is of  historical interest. It exists  to help other programmers who may come across a similar problem.

When we ran Cardbox under Wine for the very first time, we tried using File > Open > Server to connect to an Internet-based database. This is simpler than the “hidden server” technology that is used when you open a database on your own computer, so it seemed likelier to work.

Once the database was selected from the list, and OK was pressed, the “Opening database…” message was displayed, together with a progress bar. The opening process went on… and on and on… and on and on and on. It never ended until the user cancelled it.

To cut a long story short, this turned out to have nothing to do with Internet communications at all. For complicated design reasons, Cardbox needed to check for all messages except certain specific ones. This meant that instead of GetMessage it used a collection of PeekMessage calls:

if (PeekMessage(lpMsg,hWnd,0,WM_KEYFIRST-1,wRemoveMsg)
    ||  PeekMessage(lpMsg,hWnd,WM_KEYLAST+1,WM_MOUSEFIRST-1,wRemoveMsg)
    || PeekMessage(lpMsg,hWnd,WM_MOUSELAST+1,0,wRemoveMsg)
    )  return TRUE;

This works under every version of Windows from 3.1 to Vista, but not under Wine. The reason is that although the Windows documentation says that PeekMessage(...,0,0,...) retrieves every possible message (so that the second 0 effectively means “infinity”) it does not actually say that 0 in the second position means “infinity” always. Wine, being a clean re-implementation of the Windows API according to the published documentation, treats 0 in the second position as 0. Replacing the second 0 with UINT_MAX gave a program that continued to work normally under Windows but now also worked under Wine:

if (PeekMessage(lpMsg,hWnd,0,WM_KEYFIRST-1,wRemoveMsg)
    || PeekMessage(lpMsg,hWnd,WM_KEYLAST+1,WM_MOUSEFIRST-1,wRemoveMsg)
    || PeekMessage(lpMsg,hWnd,WM_MOUSELAST+1,UINT_MAX,wRemoveMsg)
    )  return TRUE;

This was the only change that needed to be made before Cardbox for Windows could use Internet databases when run under Wine.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: