Shoutbox

Interop.Call("user32.dll","GetActiveWindow") returns 0 - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Interop.Call("user32.dll","GetActiveWindow") returns 0 (/showthread.php?tid=87893)

Interop.Call("user32.dll","GetActiveWindow") returns 0 by V@no on 12-22-2008 at 02:01 AM

Hello!
I'm trying get a currently active window handle. I found this on the forum:

code:
var handle = Interop.Call("user32.dll","GetActiveWindow")
However if active window not related to WLM or MPL, it always returns 0

Any other way get handle of the active window?

Thank you.
RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by felipEx on 12-22-2008 at 03:01 AM

You can achieve this by using the GetForegroundWindow function.


MSDN is your friend : >


RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by V@no on 12-22-2008 at 03:11 AM

Thank you. That is exactly what I needed.


There is one issue though. It returns HWND...how do I get PID from HWND or vise versa?

P.S.
I'm very new to this and not sure how to use classes...
I found this:
http://msdn.microsoft.com/en-us/library/system.di...etprocessbyid.aspx

It seems to be what I needed, bu how do I use it within MPL scripts?
RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by Pinecone on 12-22-2008 at 04:12 AM

Untested... I think will work as is. GetWindowThreadProcessId was the function you were looking for. http://msdn.microsoft.com/en-us/library/ms633522.aspx

code:
var D = Interop.Allocate(4);
var hWnd = Interop.Call('User32.dll', 'GetForegroundWindow');
var hThread = Interop.Call('User32.dll', 'GetWindowThreadProcessId', hWnd, D.DataPtr);
var ProcessId = D.ReadDWORD(0);
D.Size = 0;

RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by V@no on 12-22-2008 at 05:45 AM

Thank you very much!
I've tried before the GetWindowThreadProcessId, but it returned different number then expected, but with your code it's working great.

P.S.
You've misspelled: GetForegroundWindow[/code]


RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by Pinecone on 12-22-2008 at 06:31 AM

No problem. Thanks for pointing out the spelling error (corrected now).

Your previous attempts will have returned the wrong number if you were trying to use the return value of GetWindowThreadProcessId as it returns a handle to the thread that created the window, not the process id.


RE: Interop.Call("user32.dll","GetActiveWindow") returns 0 by V@no on 12-22-2008 at 06:42 AM

Yes, you are right.
I've checked with Process Explorer and the number in my previous attempts was one of the threads ID (under "Threads" tab).
Thank you for the explanation.