Sign in to follow this  
Followers 0
FinalVersion

Can't Set Hwnd Back to 0

3 posts in this topic

Whenever "Shell.Detach" is typed in the input box at the bottom, and is executed, it's suppose to set $hWnd back to 0, but it doesn't. I'd like to know how to fix this.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$frmMain = GUICreate("Shell Controller", 483, 307, -1, -1)
$cbWins = GUICtrlCreateCombo("", 5, 5, 385, 25)
$btnAttach = GUICtrlCreateButton("Attach", 400, 7, 75, 20, $WS_GROUP)
$txtShell = GUICtrlCreateInput("", 5, 280, 377, 21)
$btnExecute = GUICtrlCreateButton("Execute", 400, 280, 75, 20, $WS_GROUP)
$edtShell = GUICtrlCreateEdit("", 5, 32, 470, 241)
GUISetState(@SW_SHOW)

$aWinList = WinList()
For $i = 1 To $aWinList[0][0]
    If Not $aWinList[$i][0] = "" Then
        GUICtrlSetData($cbWins, $aWinList[$i][0])
    EndIf
Next

Local $hWnd = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If Not $hWnd = 0 Then
                _ShellAdd($edtShell, "You must detach from attached window!")
            Else
                Exit
            EndIf
        Case $btnAttach
            If $hWnd = 0 Then
                $title = GUICtrlRead($cbWins)
                $hWnd = WinGetHandle($title)

                GUICtrlSetState($cbWins, $GUI_DISABLE)
                GUICtrlSetState($btnAttach, $GUI_DISABLE)

                _ShellAdd($edtShell, "Attaching to " & $title & "...")
            EndIf
        Case $btnExecute
            _ParseCommand($txtShell, $edtShell, $hWnd)
    EndSwitch
WEnd

Func _ShellAdd($theShell, $theMessage)
    $Read = GUICtrlRead($theShell)

    If Not $Read = "" Then
        GUICtrlSetData($theShell, $Read & $theMessage & @CRLF)
    Else
        GUICtrlSetData($theShell, $theMessage & @CRLF)
    EndIf
EndFunc ;==>_ShellAdd

Func _ParseCommand($theInput, $theShell, $hWnd)
    $command = GUICtrlRead($theInput)
    GUICtrlSetData($theInput, "")

    If $command = "Shell.Exit" Then
        If $hWnd <> 0 Then
            _ShellAdd($theShell, "You must detach from all windows!")
        Else
            Exit
        EndIf
    ElseIf $command = "Shell.Detach" Then
        If $hWnd = 0 Then
            _ShellAdd($theShell, "You haven't attached to a window...")
        Else
            $hWnd = 0
            _ShellAdd($theShell, "Detaching from window...")
            GUICtrlSetState($cbWins, $GUI_ENABLE)
            GUICtrlSetState($btnAttach, $GUI_ENABLE)
        EndIf
    ElseIf $command = "Shell.Hwnd" Then
        _ShellAdd($theShell, "Current Hwnd -> " & $hWnd)
    EndIf
EndFunc ;==>_ParseCommand

Sorry if code is confusing, it makes sense to me.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$hWnd is local and passed to your function. The function changes it's value for internal use, but never passes this information back.

Solutions:

1. Make $hWnd global and don't pass it to the function as a parameter.

2. Make _ParseCommand() Return $hWnd

3. Make the $hWnd parameter Byref. (requires the least changes in the code)

Edited by Tvern

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