What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Interop.Call("user32.dll","GetActiveWindow") returns 0

Interop.Call("user32.dll","GetActiveWindow") returns 0
Author: Message:
V@no
Full Member
***

Avatar
sexy

Posts: 162
Joined: Mar 2004
O.P. Interop.Call("user32.dll","GetActiveWindow") returns 0
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.
12-22-2008 02:01 AM
Profile PM Find Quote Report
felipEx
Scripting Contest Winner
***


Posts: 378
Reputation: 24
35 / Male / Flag
Joined: Jun 2006
RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
You can achieve this by using the GetForegroundWindow function.


MSDN is your friend : >
12-22-2008 03:01 AM
Profile E-Mail PM Find Quote Report
V@no
Full Member
***

Avatar
sexy

Posts: 162
Joined: Mar 2004
O.P. RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
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?
12-22-2008 03:11 AM
Profile PM Find Quote Report
Pinecone
New Member
*

Avatar
Coffee Powered

Posts: 9
34 / Male / Flag
Joined: Dec 2008
RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
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;

This post was edited on 12-23-2008 at 06:52 PM by Pinecone.
12-22-2008 04:12 AM
Profile E-Mail PM Find Quote Report
V@no
Full Member
***

Avatar
sexy

Posts: 162
Joined: Mar 2004
O.P. RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
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]
12-22-2008 05:45 AM
Profile PM Find Quote Report
Pinecone
New Member
*

Avatar
Coffee Powered

Posts: 9
34 / Male / Flag
Joined: Dec 2008
RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
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.

This post was edited on 12-23-2008 at 06:52 PM by Pinecone.
12-22-2008 06:31 AM
Profile E-Mail PM Find Quote Report
V@no
Full Member
***

Avatar
sexy

Posts: 162
Joined: Mar 2004
O.P. RE: Interop.Call("user32.dll","GetActiveWindow") returns 0
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.
12-22-2008 06:42 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On