Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HWND Creation Time

Tags:

I am new to this community, while working with 1 of my automation script I am encountering an issue, I wanted to get HWND’s, creation time.

I am having a set of HWND in an array which I have retrieved from FindWindowEx, I want to find in array which HWND is created last depending upon system time

I do not have enough knowledge of window hooks, but I read about some CBTproc which has some event called “CBT_CREATEWND” which can return HWND at the time window is about to create, I am very much unsure about how to work with window hooks But if I will get HWND I can pick up system time and compare with HWND of my array.

Anyone can please put some light on the same, also ask me for more elaboration if I am not clear.

Thanks, Manish Bansal

like image 938
Manish Bansal Avatar asked Jun 19 '09 18:06

Manish Bansal


1 Answers

Windows doesn't store this information in a way that is accessible via the API, so you have to gather it yourself.

If you can modify the code that creates the HWND, you can just store the current time while handling WM_CREATE or WM_NCCREATE.

I'd avoid window hooks if possible - they inject your DLL into every process that is creating windows. A critical bug in your DLL will cause every app on your desktop to die a horrible death.

If you have to go to with windows hook, you inject the hook using SetWindowsHookEx like this:

HHOOK myHook = SetWindowsHookEx(WH_CBT, MyHookFunction, myHookDll, 0);

Your hook proc will then look like this:

LRESULT CALLBACK MyHookFunction(int nCode, WPARAM wParam, LPARAM lParam)
{
   if (nCode == HCBT_CREATEWND)
   {
        // wParam is new window.
   }
   else if (nCode == HCBT_DESTROYWND)
   {
        // wParam is window being destroyed
   }

   return CallNextHookEx(myHook, nCode, wParam, lParam);
}

The hook proc needs to be present in a DLL, so it can be loaded by other processes. Since your hook will be running in different processes, you'll need to marshal the information back to your original process. You could do this via a custom window message, for example.

like image 71
Michael Avatar answered Oct 11 '22 16:10

Michael