#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
;~ Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
#region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 700, 500, 200, 20)
Global $btnCommand = GUICtrlCreateButton("Command", 30, 50, 75, 25, $WS_GROUP)
GUISetIcon("D:\102.ico")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
;~ GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
;~ GUISetOnEvent($btnCommand, "SENDCommand")
GUIRegisterMsg(0xF, "WM_PAINT")
Global $pid = Run("cmd.exe /T:F0 /k TITLE PLM")
ProcessWait($pid)
; get the handle of the cmd window as i cannot be certain that there will be only one instance of the cmd running with the same window title or class
Global $cmdHandle = _ProcessGetHWnd($pid, 2)
;~ _ArrayDisplay($cmdHandle)
Global $hWndChild = $cmdHandle[1][1]
DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $hWndChild, "hwnd", $Form1)
DllCall("user32.dll", "long", "SetwindowLong", "hwnd", $hWndChild, "int", -20, "long", 0x80000000 + 0x40000000 + 0x40000)
GUISetStyle(BitOR($WS_POPUP, $WS_BORDER), '', $hWndChild)
WinSetState($hWndChild, '', @SW_SHOW)
WinMove($hWndChild, '', 140, 60, 498, 348)
;~ Global $handle = _gethwnd($pid)
;~ ConsoleWrite("Window Handle for " & $pid & " is " & $hWndChild & @CRLF)
;~ ControlSend($hWndChild,"","","Hello")
; inifinite event loop
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
CLOSEClicked()
Case $btnCommand
SENDCommand()
EndSwitch
; sleep for 100 milliseconds (to not hog the cpu)
Sleep(100)
; end of event loop
WEnd
Func CLOSEClicked()
; take care of things to do when exiting
ProcessClose($pid)
Exit
EndFunc ;==>CLOSEClicked
Func SENDCommand()
ConsoleWrite("Window Handle for " & $pid & " is " & $hWndChild & @CRLF)
ControlSend($hWndChild, "", "", "DIR{ENTER}")
EndFunc ;==>SENDCommand
Func WM_PAINT($hWnd, $Msg, $wParam, $lParam)
Sleep(100)
DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", 0, "int", 0)
EndFunc ;==>WM_PAINT
Func _gethwnd($proc)
$Windowlist = WinList()
Local $winhandle = 0
For $i = 1 To $Windowlist[0][0]
If WinGetProcess($Windowlist[$i][1]) = $proc Then
$winhandle = $Windowlist[$i][1]
EndIf
Next
ConsoleWrite("Window Handle for " & $proc & " is " & $winhandle & @CRLF)
Sleep(500)
Return $winhandle
EndFunc ;==>_gethwnd
;===============================================================================
;
; Function Name: _ProcessGetHWnd
; Description: Returns the HWND(s) owned by the specified process (PID only !).
;
; Parameter(s): $iPid - the owner-PID.
; $iOption - Optional : return/search methods :
; 0 - returns the HWND for the first non-titleless window.
; 1 - returns the HWND for the first found window (default).
; 2 - returns all HWNDs for all matches.
;
; $sTitle - Optional : the title to match (see notes).
; $iTimeout - Optional : timeout in msec (see notes)
;
; Return Value(s): On Success - returns the HWND (see below for method 2).
; $array[0][0] - number of HWNDs
; $array[x][0] - title
; $array[x][1] - HWND
;
; On Failure - returns 0 and sets @error to 1.
;
; Note(s): When a title is specified it will then only return the HWND to the titles
; matching that specific string. If no title is specified it will return as
; described by the option used.
;
; When using a timeout it's possible to use WinWaitDelay (Opt) to specify how
; often it should wait before attempting another time to get the HWND.
;
;
; Author(s): Helge
;
;===============================================================================
Func _ProcessGetHWnd($iPid, $iOption = 1, $sTitle = "", $iTimeout = 2000)
Local $aReturn[1][1] = [[0]], $aWin, $hTimer = TimerInit()
While 1
; Get list of windows
$aWin = WinList($sTitle)
; Searches thru all windows
For $i = 1 To $aWin[0][0]
; Found a window owned by the given PID
If $iPid = WinGetProcess($aWin[$i][1]) Then
; Option 0 or 1 used
If $iOption = 1 OR ($iOption = 0 And $aWin[$i][0] <> "") Then
Return $aWin[$i][1]
; Option 2 is used
ElseIf $iOption = 2 Then
ReDim $aReturn[UBound($aReturn) + 1][2]
$aReturn[0][0] += 1
$aReturn[$aReturn[0][0]][0] = $aWin[$i][0]
$aReturn[$aReturn[0][0]][1] = $aWin[$i][1]
EndIf
EndIf
Next
; If option 2 is used and there was matches then the list is returned
If $iOption = 2 And $aReturn[0][0] > 0 Then Return $aReturn
; If timed out then give up
If TimerDiff($hTimer) > $iTimeout Then ExitLoop
; Waits before new attempt
Sleep(Opt("WinWaitDelay"))
WEnd
; No matches
SetError(1)
Return 0
EndFunc ;==>_ProcessGetHWnd