Sign in to follow this  
Followers 0
Zohar

[Closed] why doesn't this DllCall Get the Last Active Window like it should?

12 posts in this topic

#1 ·  Posted (edited)

Hello

This code should get a Handle to the last active window, and then MsgBox the last active window's Title.

Yet it returns a different result.. of some different window..

Anyone knows what I need to change in this code in order to make it work?

CODE

#Include <Constants.au3>

Local $hWnd =DllCall("user32.dll","hwnd","GetNextWindow","hwnd",WinGetHandle("[ACTIVE]"),"int",$GW_HWNDNEXT)

Local $Title =WinGetTitle($hWnd)

MsgBox(0,"",$Title)

this API function is from here:

GetNextWindow Function

http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx

Edited by Zohar

Share this post


Link to post
Share on other sites



Hello

This code should get a Handle to the last active window, and then MsgBox the last active window's Title.

Yet it returns a different result.. of some different window..

Anyone knows what I need to change in this code in order to make it work?

CODE

#Include <Constants.au3>

Local $hWnd =DllCall("user32.dll","hwnd","GetNextWindow","hwnd",WinGetHandle("[ACTIVE]"),"int",$GW_HWNDNEXT)

Local $Title =WinGetTitle($hWnd)

MsgBox(0,"",$Title)

this API function is from here:

GetNextWindow Function

http://msdn.microsoft.com/en-us/library/ms633509(VS.85).aspx

The value of @error is 3 for me after making the dllcall so there is no such function. If you use GetWindow instead then you might have more luck. Also you get an array returned from dllcall if it succeeds.

#Include <Constants.au3>

Local $hWnd =DllCall("user32.dll","hwnd","GetWindow","hwnd",WinGetHandle("[ACTIVE]"),"int",$GW_HWNDLAST);$GW_HWNDNEXT)
ConsoleWrite(@error & @CRLF)
Local $Title =WinGetTitle($hWnd[0])
$wp = WinGetPos($hwnd[0])
ConsoleWrite($wp[0] & ', ' & $wp[1] & ', ' & $wp[2] & ', ' & $wp[3] & @CRLF)
MsgBox(0,"",$Title)

But it might be easier to use WinList which gives the windows in their Z order and just select the visible ones because I suspect that GetWindow will not care if the window is visible or not so you might not get the window of the previously active application in the way that Alt TAB gives for example.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

hi martin..

yeah I did eventually use WinList() to get the previous window

CODE
Func GetPreviousWindow()

Local $WinList =WinList()

For $i = 1 to $WinList[0][0]

If ($WinList[$i][0]<>"") AND BitAnd(WinGetState($WinList[$i][1]),2) Then ExitLoop

Next

Return $WinList[$i][1]

EndFunc

The code is very short so I am happy,

tho I wonder why my API call did not work :)

That function does exist.. as MSDN shows:)

but ok

thank you !

Share this post


Link to post
Share on other sites

It's not in my user32.dll either (XP SP3)


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

..

That function does exist.. as MSDN shows:)

but ok

thank you !

How much do you want to bet?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Minimum operating systems Windows 95, Windows NT 3.1

I would assume that means it's a much older function.

Share this post


Link to post
Share on other sites

It's not in my user32.dll either (XP SP3)

It's because GetNextWindow is just an alias for GetWindow(), from WinUser.h:

#define GetNextWindow(hWnd, wCmd) GetWindow(hWnd, wCmd)

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Yes, I see. Like that AVI function that was killing you.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

How much do you want to bet?

hehe =]

It's because GetNextWindow is just an alias for GetWindow(), from WinUser.h:

#define GetNextWindow(hWnd, wCmd) GetWindow(hWnd, wCmd)
damn, you're good :]

btw I must say,

that even after changing the Function Name to GetWindow:

Local $hWnd     =DllCall("user32.dll","hwnd","GetWindow","hwnd",WinGetHandle("[ACTIVE]"),"int",$GW_HWNDNEXT)
Local $Title    =WinGetTitle($hWnd)
MsgBox(0,"",$Title)

It still shows the title of Current Window instead of the previous one.

(I did solve the problem with WinList, as said, tho it makes me wonder, why a simple API call does not succeed =( )

Edited by Zohar

Share this post


Link to post
Share on other sites

Is your $GW_HWNDNEXT defined correctly?

Share this post


Link to post
Share on other sites

I have "#Include <Constants.au3>" at the top of the file..

so it means yes..

Share this post


Link to post
Share on other sites

Why don't you use Winlist() for this task? As far as I know it returns hWnd in order of last activity.

$var = WinList()

For $i = 1 to $var[0][0]
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) AND $var[$i][1] <> WinGetHandle("[ACTIVE]") Then
    MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
    ExitLoop
  EndIf
Next

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
  Else
    Return 0
  EndIf
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0