SetWindowsHookEx() fails inside a thread of Windows service created using AfxBeginThread()

S

__Sarge

I've written a piece of code that works perfectly fine inside an
executable on Win 2k platform, but does not work when invoked from a
multi threaded Windows Service.

The task is to track change in value of a status bar and items of a
list control on a 3rd party executable. The window for this target
application is first searched using EnumWindows() and than the
following code is executed

g_hinstDll = LoadLibrary("hookDLL.dll");

if (g_hinstDll == NULL) {

return;
}

// Set WH_CALLWNDPROCRET hook on the 3rd party application thread

g_hhook = ::SetWindowsHookEx(WH_CALLWNDPROCRET,
(HOOKPROC)CallWndProcRet, g_hinstDll, m_target_threadid);

if (g_hhook == NULL) {
TRACE("Can't create hook! Error Code = %d\n",GetLastError());
return;
}

This code works perfectly when compiled as an executable. But if we
run this code in a thread invoked using AfxBeginThread(), inside a
Windows Service, it fails with the error code ERROR_ACCESS_DENIED
after the SetWindowsHookEx call.

Q - Can somebody give some pointers on what could be the reason for
the error message?

Additional Information –
• The service has the option ‘Allow service to interact with the
desktop' option selected, to facilitate search of the target
application window thread.
• Currently logged on user is part of the Administrators group.
• Inside this particular thread in the service, i'm able to open the
target process using these calls
OpenProcess( PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION
|PROCESS_VM_WRITE, FALSE, m_target_processid)
Or
OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, m_target_processid)
• Call for starting the thread is
AfxBeginThread(RUNTIME_CLASS(CHookTarget),THREAD_PRIORITY_NORMAL, 0,
CREATE_SUSPENDED, NULL)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top