I am preparing a Delphi module, which sets a hook in a thread to record a macro:
FHandleRec := SetWindowsHookEx(WH_JOURNALRECORD, FRecordProc, HInstance, 0);
FHandlePlay := SetWindowsHookEx(WH_JOURNALPLAYBACK, FPlayProc, HInstance, 0);
That works fine on WinXP, but on Vista/Windows 7 fails with ERROR_ACCESS_DENIED
.
I have found in Google (this) referring (that). The quote:
A lower privilege process cannot: … Use Journal hooks to monitor a higher privilege process.
Tried without success:
- Run application as administrator. Probably the thread is started with lower privileges than the main thread (though I am not 100% sure)
- Impersonating the thread with administrator security context doesn’t help either.
The code sample:
if LogonUser(PWideChar(sAdminUser), PWideChar(sDomain), PWideChar(sPwd),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken) then
begin
if not ImpersonateLoggedOnUser(hToken) then
raise Exception.Create('Error impersonating the user');
end;
FHandleRec := SetWindowsHookEx(WH_JOURNALRECORD, FRecordProc, HInstance, 0);
LogonUser
and ImpersonateLoggedOnUser
execute without errors.
Other possibilities to try:
- Turn UAC OFF permanently. This helps, but I cannot force the module users to do that.
- A module customer signs an application and put it in a trusted location. Not tried that, but that radically complicates the module usage for the users.
- Put the module into some signed application and distribute EXE. That will break some core functionality.
Could you please show the code that is setting the hook under Visa / Windows 7 or suggest the working solution ?