Handles invalid when no focus?

This is a discussion on Handles invalid when no focus? within the Windows Programming forums, part of the Platform Specific Boards category; I am messing with a Hook program that intercepts keypresses, throughout the system, passes it onto a "server", which then ...

  1. #1
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Handles invalid when no focus?

    I am messing with a Hook program that intercepts keypresses, throughout the system, passes it onto a "server", which then dished the information out to a client on my LAN (Basically, everykey I press on 1 computer is shown on another computer)

    The Dll that implements my hook procedure relays the info back to the server using a windows pipe, and the server sits with a dedicated thread using ReadFile() to take my information and send it over the network.

    It all works very nice, except that I cannot get the "server" to do anything when its out of focus......I have checked my hook procedure and that is working fine, but as soon as the "server" does not have focus, the handle to the pipe is made invalid (It actually becomes NULL while the server loses focus). As soon as I restore focus its back again and working fine.......

    I have tried just about everything I can think about; Moving the ReadFile to its own thread, using DuplicateHandle() to try get a better handle, using SendMessage(), Posting Messages directly to the thread with PostThreadMessage()........and so on.....but no luck.......

    Its 04:21AM, I have had enough and I have no hope of further ideas tonight (there may be a simple solution...but I wont get it now)..

    Oh BTW, I am using MSVC++6 and Windows 2000Pro

    Thanks in advance....

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I'm not quite sure I understand your question/problem, but you might want to try using SetFocus when you lose the focus from the server. Do you always want the focus on the server? If so, then handle the WM_KILLFOCUS (I'm not sure if this is the message. The message that is sent to the wndproc when the control/window loses focus) and then use SetFocus to keep it.

    Is this what you want?
    Last edited by Garfield; 03-10-2002 at 08:21 AM.
    1978 Silver Anniversary Corvette

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    No, I want the program to continue processing when it loses focus, but it seems that all handle created in that process are assigned NULL during the time the focus is lost....

    I wrote the app in MFC, and so I am trying to rewrite it at the moment just using the SDK.......maybe that will help...I dont know though...

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Nope...tried rewriting it in the SDK but still I get an error....

    As soon as the window loses focus, the handle that was passed to the dll for the pipe is set to 0.......If I try use it, GetLastError() returns ERROR_INVALID_HANDLE.

    But as soon as the main window gets focus, the handle is restored....

    This is driving me nuts......

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So, what you want to do is when, say, your program is open but then somebody "goes" to another program therefore setting focus to that other program and losing your program? And, your problem is that you can't "do" anything when the program has no focus? I can't see why it would set each of the handles to NULL. Sorry.
    1978 Silver Anniversary Corvette

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Doh......fixed it...

    I spend all damn morning chasing the wrong problem.....

    If anyones interested then I worked out that the handles were not being invalidted at all........when my hook was attached to another process, the handle to my pipe became invalid for that process's address space....I couldnt use the DuplicateHandle() trick cuz I could not know the process handle for that perticular process and my "server" at the same time....

    I settled for a mailslot in the end and now it works ok......

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Way over my head , I haven't gotten into socket programming...yet. It's on my list of to-do's
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21