PC Review Forums Newsgroups Windows 2000 Microsoft Windows 2000 Developer SetWindowsHookEx() fails inside a thread of Windows service created using AfxBeginThread()

Reply

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

 
Thread Tools Rate Thread
Old 28-04-2004, 03:21 PM   #1
__Sarge
Guest
 
Posts: n/a
Default SetWindowsHookEx() fails inside a thread of Windows service created using AfxBeginThread()


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)
  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off