Jump to content

Send command to child cmd window


Recommended Posts

Trying to create a GUI with a cmd window inside it, and send commands to the cmd window with preprogramed buttons.

I can get the commands to work if I don't use the GUI and just code the commands, but when I use the GUI script and have the cmd window inside it, no commands get sent. It appears the cmd window is not active and I haven't been able to make it active after trying a few different methods.

I know this is a touchy subject and even with controlsendplus(), results can vary. I hoping someone can catch something I'm missng or point me in another direction.

I left in some of the commented out lines so you can see some of what I've tried.

Thanks for any help or suggestions!

Credit goes to Holger for the original base of this script.

#include <GUIConstants.au3>
#include <ControlSendPlus.au3>

Opt("SendKeyDelay", 40)

Global $BtnListDisk

$hGUI = GUICreate("DispartGUI - 1.00", 850, 600, -1, -1, BitOr($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_CLIPCHILDREN))

$PID    = Run(@comspec & " /c Diskpart", "", @SW_HIDE)  ;   Run(@ComSpec & " /c " & 'command', "", @SW_HIDE)

$hWnd   = 0
$stPID  = DllStructCreate("int")

$BtnListDisk = GUICtrlCreateButton("List Disk", 25, 65, 100, 25)
GUICtrlSetTip(-1, "Click here to set retreive disk details")
GUICtrlSetResizing ($BtnListDisk,$GUI_DOCKHEIGHT+$GUI_DOCKVCENTER+$GUI_DOCKWIDTH)


Do
    $WinList = WinList()
    For $i = 1 To $WinList[0][0]
        If $WinList[$i][0] <> "" Then
            DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $WinList[$i][1], "ptr", DllStructGetPtr($stPID))
            If DllStructGetData($stPID, 1) = $PID Then
                $hWnd = $WinList[$i][1]
                ExitLoop
            EndIf
        EndIf
    Next
    Sleep(100)
Until $hWnd <> 0

$stPID = 0

If $hWnd <> 0 Then
    $nExStyle = DllCall("user32.dll", "int", "GetWindowLong", "hwnd", $hWnd, "int", -20)
    $nExStyle = $nExStyle[0]
    DllCall("user32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", -20, "int", BitOr($nExStyle, $WS_EX_MDICHILD))
    DllCall("user32.dll", "int", "SetParent", "hwnd", $hWnd, "hwnd", $hGUI) 
    WinSetState($hWnd, "", @SW_SHOW)
    WinMove($hWnd, "", 245, 25, 600, 400)
EndIf

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then ExitLoop
    
    Select
        Case $msg = $btnListDisk
        ;ControlFocus("Untitled - Notepad", "", "Edit1")
        ;$a = ControlGetFocus("C:\WINDOWS\system32\cmd.exe", "")
        ;MsgBox(0, "test", $a)
        ;ControlFocus("C:\WINDOWS\system32\cmd.exe", "", "Edit1")
        ;WinActivate("C:\WINDOWS\system32\cmd.exe", "")
            WinWaitActive("classname=ConsoleWindowClass", "")
            WinActivate("classname=ConsoleWindowClass")
            ControlsendPlus("classname=ConsoleWindowClass", "", "", "List Disk{ENTER}", 0)
;MsgBox(0, "test", "test")
    
    EndSelect
            
    
WEnd
Edited by stev379
Link to comment
Share on other sites

Trying to create a GUI with a cmd window inside it, and send commands to the cmd window with preprogramed buttons.

I can get the commands to work if I don't use the GUI and just code the commands, but when I use the GUI script and have the cmd window inside it, no commands get sent. It appears the cmd window is not active and I haven't been able to make it active after trying a few different methods.

I know this is a touchy subject and even with controlsendplus(), results can vary. I hoping someone can catch something I'm missng or point me in another direction.

I left in some of the commented out lines so you can see some of what I've tried.

Thanks for any help or suggestions!

Credit goes to Holger for the original base of this script.

#include <GUIConstants.au3>
#include <ControlSendPlus.au3>

Opt("SendKeyDelay", 40)

Global $BtnListDisk

$hGUI = GUICreate("DispartGUI - 1.00", 850, 600, -1, -1, BitOr($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_CLIPCHILDREN))

$PID    = Run(@comspec & " /c Diskpart", "", @SW_HIDE) ;    Run(@ComSpec & " /c " & 'command', "", @SW_HIDE)

$hWnd   = 0
$stPID  = DllStructCreate("int")

$BtnListDisk = GUICtrlCreateButton("List Disk", 25, 65, 100, 25)
GUICtrlSetTip(-1, "Click here to set retreive disk details")
GUICtrlSetResizing ($BtnListDisk,$GUI_DOCKHEIGHT+$GUI_DOCKVCENTER+$GUI_DOCKWIDTH)
Do
    $WinList = WinList()
    For $i = 1 To $WinList[0][0]
        If $WinList[$i][0] <> "" Then
            DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $WinList[$i][1], "ptr", DllStructGetPtr($stPID))
            If DllStructGetData($stPID, 1) = $PID Then
                $hWnd = $WinList[$i][1]
                ExitLoop
            EndIf
        EndIf
    Next
    Sleep(100)
Until $hWnd <> 0

$stPID = 0

If $hWnd <> 0 Then
    $nExStyle = DllCall("user32.dll", "int", "GetWindowLong", "hwnd", $hWnd, "int", -20)
    $nExStyle = $nExStyle[0]
    DllCall("user32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", -20, "int", BitOr($nExStyle, $WS_EX_MDICHILD))
    DllCall("user32.dll", "int", "SetParent", "hwnd", $hWnd, "hwnd", $hGUI) 
    WinSetState($hWnd, "", @SW_SHOW)
    WinMove($hWnd, "", 245, 25, 600, 400)
EndIf

GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then ExitLoop
    
    Select
        Case $msg = $btnListDisk
    ;ControlFocus("Untitled - Notepad", "", "Edit1")
    ;$a = ControlGetFocus("C:\WINDOWS\system32\cmd.exe", "")
    ;MsgBox(0, "test", $a)
    ;ControlFocus("C:\WINDOWS\system32\cmd.exe", "", "Edit1")
    ;WinActivate("C:\WINDOWS\system32\cmd.exe", "")
            WinWaitActive("classname=ConsoleWindowClass", "")
            WinActivate("classname=ConsoleWindowClass")
            ControlsendPlus("classname=ConsoleWindowClass", "", "", "List Disk{ENTER}", 0)
;MsgBox(0, "test", "test")
    
    EndSelect
            
    
WEnd
it seems like it would be easier to fake it. meaning have a black multi line edit box, and a single line edit box stuck to the bottom of it, that you control the behavior of. you can have the lines written to it as if it were a console and processed backend with @comspec. you could even make it so that if a command was typed in and enter pressed, it would process that as a command...
Link to comment
Share on other sites

it seems like it would be easier to fake it. meaning have a black multi line edit box, and a single line edit box stuck to the bottom of it, that you control the behavior of. you can have the lines written to it as if it were a console and processed backend with @comspec. you could even make it so that if a command was typed in and enter pressed, it would process that as a command...

Cool suggestion. I'll give that whirl.

Thanks!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...