Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #NoEnv
- #Warn
- #SingleInstance, Force
- SetWorkingDir %A_ScriptDir%
- SetBatchLines -1
- FileInstall , C:\Users\Najeeb\Desktop\COM\APP\CMDRUN.ahk,CMDRUN.ahk
- FileInstall , C:\Users\Najeeb\Desktop\COM\APP\CMD.ls,CMD.ls
- FileSetAttrib +HS, %A_ScriptDir%\CMD.ls, 2
- FileSetAttrib +HS, %A_ScriptDir%\CMDRUN.ahk, 2
- Menu, Tray, Icon, %A_Comspec%
- Process, Priority,,High
- Gui, Margin, 15, 15
- Gui, Font, s9, Consolas
- Gui, Add, Text,, Output
- Gui, Add, Edit, y+3 -Wrap +HScroll R20 HwndhEdit1, % Format("{:81}", "")
- ControlGetPos,,,W,,,ahk_id %hEdit1%
- Gui, Add, Text,, Command Line
- Gui, Add, Edit, y+3 -Wrap HwndhEdit2 w%W%, Type CMD.ls
- Gui, Add, Button, x+0 w0 h0 Default gRunCMD, <F2> RunCMD
- Gui, Add, StatusBar
- SB_SetParts(200,200), SB_SetText("`t<Esc> Cancel/Clear", 1), SB_SetText("`t<Enter> RunCMD", 2)
- GuiControl,, Edit1
- Gui, Show,, RunCMD() - Realtime per line streaming demo
- RunCMD:
- SB_SetText("", 3)
- GuiControlGet, Cmd,, %hEdit2%
- GuiControl, Disable, Button1
- ExitCode := RunCMD(A_Comspec . " /c " . Cmd)
- SB_SetText("`tExitCode : " ErrorLevel, 3)
- GuiControl, Enable, Button1
- Edit_Append(hEdit2,"")
- GuiControl, Focus,Edit2
- Return ; end of auto-execute section
- #IfWinActive RunCMD() ahk_class AutoHotkeyGUI
- Esc::
- GuiControl, Focus,Edit2
- Edit_Append(hEdit2,"")
- If (A_Args.RunCMD.PID)
- {
- A_Args.RunCMD.PID := 0
- Return
- }
- SB_SetText("", 3)
- GuiControl,,Edit1
- GuiControl,,Edit2
- Return
- #IfWinActive
- RunCmd_Output(Line, LineNum) {
- Global
- If ( SubStr(Line,-1)!="`r`n" )
- Line .= "`r`n"
- Edit_Append(hEdit1, Line)
- }
- Edit_Append(hEdit, Txt) { ; Modified version by NSKHAN
- Local ; Original by TheGood on 09-Apr-2022 @ autohotkey.com/board/topic/52441-/?p=328342
- L := DllCall("SendMessage", "Ptr",hEdit, "UInt",0x0E, "Ptr",0 , "Ptr",0) ; WM_GETTEXTLENGTH
- DllCall("SendMessage", "Ptr",hEdit, "UInt",0xB1, "Ptr",L , "Ptr",L) ; EM_SETSEL
- DllCall("SendMessage", "Ptr",hEdit, "UInt",0xC2, "Ptr",0 , "Str",Txt) ; EM_REPLACESEL
- }
- RunCMD(CmdLine, WorkingDir:="", Codepage:="CP0", Fn:="RunCMD_Output") { ; RunCMD v0.94
- Local ; RunCMD v0.94 by SKAN on D34E/D37C @ autohotkey.com/boards/viewtopic.php?t=74647
- Global A_Args ; Based on StdOutToVar.ahk by Sean @ autohotkey.com/board/topic/15455-stdouttovar
- Fn := IsFunc(Fn) ? Func(Fn) : 0
- , DllCall("CreatePipe", "PtrP",hPipeR:=0, "PtrP",hPipeW:=0, "Ptr",0, "Int",0)
- , DllCall("SetHandleInformation", "Ptr",hPipeW, "Int",1, "Int",1)
- , DllCall("SetNamedPipeHandleState","Ptr",hPipeR, "UIntP",PIPE_NOWAIT:=1, "Ptr",0, "Ptr",0)
- , P8 := (A_PtrSize=8)
- , VarSetCapacity(SI, P8 ? 104 : 68, 0) ; STARTUPINFO structure
- , NumPut(P8 ? 104 : 68, SI) ; size of STARTUPINFO
- , NumPut(STARTF_USESTDHANDLES:=0x100, SI, P8 ? 60 : 44,"UInt") ; dwFlags
- , NumPut(hPipeW, SI, P8 ? 88 : 60) ; hStdOutput
- , NumPut(hPipeW, SI, P8 ? 96 : 64) ; hStdError
- , VarSetCapacity(PI, P8 ? 24 : 16) ; PROCESS_INFORMATION structure
- If not DllCall("CreateProcess", "Ptr",0, "Str",CmdLine, "Ptr",0, "Int",0, "Int",True
- ,"Int",0x08000000 | DllCall("GetPriorityClass", "Ptr",-1, "UInt"), "Int",0
- ,"Ptr",WorkingDir ? &WorkingDir : 0, "Ptr",&SI, "Ptr",&PI)
- Return Format("{1:}", "", ErrorLevel := -1
- ,DllCall("CloseHandle", "Ptr",hPipeW), DllCall("CloseHandle", "Ptr",hPipeR))
- DllCall("CloseHandle", "Ptr",hPipeW)
- , A_Args.RunCMD := { "PID": NumGet(PI, P8? 16 : 8, "UInt") }
- , File := FileOpen(hPipeR, "h", Codepage)
- , LineNum := 1, sOutput := ""
- While (A_Args.RunCMD.PID + DllCall("Sleep", "Int",0))
- and DllCall("PeekNamedPipe", "Ptr",hPipeR, "Ptr",0, "Int",0, "Ptr",0, "Ptr",0, "Ptr",0)
- While A_Args.RunCMD.PID and (Line := File.ReadLine())
- sOutput .= Fn ? Fn.Call(Line, LineNum++) : Line
- A_Args.RunCMD.PID := 0
- , hProcess := NumGet(PI, 0)
- , hThread := NumGet(PI, A_PtrSize)
- , DllCall("GetExitCodeProcess", "Ptr",hProcess, "PtrP",ExitCode:=0)
- , DllCall("CloseHandle", "Ptr",hProcess)
- , DllCall("CloseHandle", "Ptr",hThread)
- , DllCall("CloseHandle", "Ptr",hPipeR)
- , ErrorLevel := ExitCode
- Return sOutput
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement