Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I hook an application’s main thread message queue processi

10 views
Skip to first unread message

Richard

unread,
Aug 11, 2009, 7:22:02 PM8/11/09
to
have a Windows CE application that is targeted at several (well two)
different windows CE devices. The application is developed in Visual studio
2005 using C++ & MFC (no .NET). One of the devices signals system shutdown to
running apps. by posting a custom message to the apps. main thread message
queue using the PostThreadMessage() API call, at present the application
intercepts and acts on this message from within an overridden
CWinApp::PreTranslateMessage() method.

We have tried to shift all device/platform specific code in the application
out to a hardware abstraction library, and I'd like to shift the shutdown
message processing out to the library too.

On Desktop Windows I could use SetWindowsHookEx(WH_CALLWNDPROC ...) to hook
the app. main thread processing, however on Windows CE/Mobile
SetWindowsHookEx(CALLWNDPROC ...) is not supported. Hooking/"subclassing" the
app. main window message processing with a SetWindowLong() call aint going to
help me either as the message in question is not directed at any particular
window.

Anyone got any other ideas??

Thanks

Richard.

Paul G. Tobey [eMVP]

unread,
Aug 11, 2009, 7:32:23 PM8/11/09
to
Is intercepting the message handling of the main window really the way to do
this? I suppose that you could subclass the window, just as you would with
a control that you wanted to slightly adjust (SetWindowLong, etc.) I don't
see any value in using hooking, given that you just want to see a message
that is otherwise ignored by the main window...

Paul T.

"Richard" <Ric...@discussions.microsoft.com> wrote in message
news:A30DA103-1ACF-4B1D...@microsoft.com...

Richard

unread,
Aug 12, 2009, 12:51:01 AM8/12/09
to
I'm no expert on windows messaging but as I noted in the initial post, I
don't think that subclassing the apps. "main" window (whatever we mean by
"main"...)
is going to help, as the message in question is being posted to the apps.
main thread (the thread indicated in the PROCESS_INFORMATION struct
populated by CreateProcess()) rather than to any particular window.

My aim here is to wrap all the platform specific code in a hardware/platform
abstraction library and have everything "just work" by linking in the
library, rather than having to polute the main application code with platform
specific message handling.

thanks

Richard.

Paul G. Tobey [eMVP]

unread,
Aug 12, 2009, 11:50:24 AM8/12/09
to
Well, I guess, at the cost of slowing down all of your message processing,
you could try a journaling hook. Here's a little code that uses it. Note
that it's completely undocumented...

-----

// JournalHook.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

// Need some of the definitions from the SetWindowsHookEx area.
#include <pwinuser.h>

// Declare a couple of completely undocumented functions implemented in
// coredll.
HHOOK WINAPI QASetWindowsJournalHook(
int nFilterType,
HOOKPROC pfnFilterProc,
EVENTMSG *pfnEventMsg
);


BOOL WINAPI QAUnhookWindowsJournalHook(
int nFilterType
);

// Declare this global so that it stays around while the hook is connected.
EVENTMSG evtMsg;
HHOOK localHook = NULL;
DWORD msgCount = 0;

HANDLE hf = NULL;

LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam)
{
msgCount += 1;

DEBUGMSG( 1, ( TEXT( "JournalRecordProc: code %d, wParam 0x%x, lParam
0x%x\r\n" ),
code, wParam, lParam ) );
DEBUGMSG( 1, ( TEXT( "JournalRecordProc: localHook = %d, msgCount =
%d\r\n" ),
localHook, msgCount ) );

// PGT: It looks to me from reading GWES that lParam here is actually
// going to be a pointer to the eventmsg structure.
EVENTMSG *e = (EVENTMSG*)lParam;
DWORD xlen;
char s[ 512 ];
sprintf( s, "message %d, wParam 0x%x, lParam 0x%x\r\n",
e->message, e->paramH, e->paramL, e->time );

WriteFile( hf, s, strlen( s ), &xlen, NULL );

return CallNextHookEx( localHook, code, wParam, lParam );
}

int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
ZeroMemory(&evtMsg, sizeof(EVENTMSG));

// Create log file.
hf = CreateFile( _T( "\\msglog.txt" ),
GENERIC_READ | GENERIC_WRITE, 0,
NULL, CREATE_ALWAYS, 0, NULL );

// Connect the hook.
localHook = QASetWindowsJournalHook(WH_JOURNALRECORD,
JournalRecordProc, &evtMsg);

while ( msgCount < 50 )
{
Sleep( 100 );
}

// Disconnect the hook.
QAUnhookWindowsJournalHook(WH_JOURNALRECORD);

CloseHandle( hf );

return 0;
}


-----

Paul T.

"Richard" <Ric...@discussions.microsoft.com> wrote in message

news:32006D66-5913-42BB...@microsoft.com...

Richard

unread,
Aug 12, 2009, 5:08:01 PM8/12/09
to
Thanks Paul, I'll have a look at it.

Rob

unread,
Aug 30, 2009, 7:36:01 AM8/30/09
to
What is 'shutdown' in your case ?
Is it also unplugging the power supply ?
I have an image that supports (HW-)power-failure-detection.
if the power is unplugged, an interrupt is trigerred, and I have a few ms
left to do some stuff before the capacitor in the power part of the device is
drained....
Windows messaging is probably too slow for that (queue, N messages in front
of yours).

Just a thought....

Kind regards,
Rob.
www.robtso.nl

0 new messages