Thread: setwindowshookex returns null

  1. #16
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    A bit more info for you.

    After the CreateProcess call but before SetWindowsHookEx I now do GetGUIThreadInfo and pass the threadId as the first parameter and a pointer to a memset GUITHREADINFO structure (with the size set correctly) as the second paramerter. This returns false with the same error 87 "Invalid parameter". So it must be the threadId which is incorrect causing both GetGUIThreadInfo and SetWindowsHookEx to fail. The threadId I get from CreateProcess (see my first example). The process is still running when I call both GetGUIThreadInfo and SetWindowsHookEx. I also sleep 10 seconds after I do CreateProcess to give the process time to initialize before calling GetGUIThreadInfo (this is only temporary).

    I see the same behaviour for both myapp and google chrome.

    I believe this to be the cause of the problem

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... well, have fun with that.

    I even showed you working code from a software package I wrote and explained why yours doesn't work... but you just went wizzing right past it...

    I can't help you if you aren't listening to the advice you are given.
    Last edited by CommonTater; 04-07-2011 at 09:33 AM.

  3. #18
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Looks like you have to wait until the thread makes its first call into the user32 kernel functions, which is when a certain struct is initialized which SWHE and GGTI require. The easiest way to wait for this is WaitForInputIdle. If you remove it from the code below it'll likely give you 87 as an error, which is the invalid parameter error

    Code:
    #define UNICODE
    #define _UNICODE
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        PROCESS_INFORMATION pi = {0};
        STARTUPINFO si = {sizeof(si), 0};
        BOOL bRet = CreateProcess(
            L"C:\\Windows\\System32\\winver.exe",
            NULL,
            NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &si,
            &pi
        );
        if(bRet)
        {
            GUITHREADINFO gti = {sizeof(gti), 0};
            WaitForInputIdle(pi.hProcess, INFINITE);
            if(!GetGUIThreadInfo(pi.dwThreadId, &gti))
            {
                DWORD err = GetLastError();
                printf("GetGUIThreadInfo errored with %lu\n", err);
            }
            else
            {
                printf("Active window is %p\n", gti.hwndActive);
            }
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crashing program, help
    By xniinja in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2010, 03:57 PM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM