Link to home
Start Free TrialLog in
Avatar of ewong_111
ewong_111

asked on

Unable to set a hook to a process created in winlogon

Hi,

I am having a problem not being able to hook into a process created by CreateProcess:

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    si.lpDesktop = "Winsta0\\Winlogon";
    ZeroMemory( &pi, sizeof(pi) );

    if(!CreateProcess( NULL,
        TEXT("Notepad.exe"),
        NULL,
        NULL,
        TRUE,
        0,
        NULL,
        NULL,
        &si,
        &pi )
    )
    return false;

    if(!g_hHook_Keyboard)
    {
      g_lpHookProc_Keyboard = (HOOKPROC) HookProcKeyboard;
      b_Success_Keyboard = (NULL != (g_hHook_Keyboard = ::SetWindowsHookEx (WH_KEYBOARD, g_lpHookProc_Keyboard, (HINSTANCE)pi.hProcess, NULL)));
    }

It returns "false" for "b_Success_Keyboard"

Any help will be appreciated..
Thanks a lot.
Avatar of jkr
jkr
Flag of Germany image

You need to change your process'/threads window station and desktop using the appropriate APIs ('SetProcessWindowStation()'/'SetThreadDesktop()') to the one the process that you want to hook is using, otherwise it won't work.
Avatar of ewong_111
ewong_111

ASKER

jkr..

I have inserted the follow code before the "if(!g_hHook_Keyboard)"

      HWINSTA h_Winsta = OpenWindowStation((LPCTSTR)"Winsta0", TRUE, GENERIC_ALL);
      if (!SetProcessWindowStation(h_Winsta)) AfxMessageBox("SetProcessWindowStation Failed");

      HDESK h_Desk = OpenDesktop((LPCTSTR)"Winlogon", 0, TRUE, GENERIC_ALL);
      if (!SetThreadDesktop(h_Desk)) AfxMessageBox("SetThreadDesktop Failed");

but unfortunately it still returns "false" for "b_Success_Keyboard"

      if (b_Success_Keyboard) AfxMessageBox("Success on Keyboard Hook");
      else AfxMessageBox("Failed on Keyboard Hook");

but now the AfxMessageBox is showing up in the "Winlogon" desktop now.
Any more ideas?
Thanks a lot.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so, what kind of hook will be allowed there?
and is it true for Screen-saver also?
Thanks again..
Actually, what is it that you have to hook the logon desktop for? To know the purpose might help to find a suggestion around that :o)
jkr,

Thanks a lot for your help, but you have already answered this question in another question I posted.
https://www.experts-exchange.com/questions/21174306/Keep-track-of-users-logging-on-and-off.html
I guess you just have too many questions each day to answer that you didn't notice it right?
Thanks again for your motivation for helping..

:D
Well, I thought these were two completely separate issues :o)