PC Review


Reply
Thread Tools Rate Thread

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

 
 
__Sarge
Guest
Posts: n/a
 
      28th Apr 2004
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Activator.CreateInstance for COM object - locks thread when inside Windows Service??? google@techretary.com Microsoft Dot NET 1 24th May 2007 08:31 PM
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on Joe Microsoft C# .NET 4 12th Mar 2007 10:59 AM
When does a message processing thread get created for for Windows Form me Microsoft Dot NET Framework Forms 1 20th Feb 2005 08:01 AM
Controls created on one thread cannot be parented to a control on a different thread gregbacchus@hotmail.com Microsoft Dot NET Framework Forms 8 19th Oct 2004 04:23 PM
Controls Created on One Thread Cannot Be Parented to a Control on a Different Thread William Ryan Microsoft ADO .NET 0 8th Sep 2003 11:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.