Advertisement
Najeebsk

CMDRUN.ahk

Oct 27th, 2022
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv
  2. #Warn
  3. #SingleInstance, Force
  4. SetWorkingDir %A_ScriptDir%
  5. SetBatchLines -1
  6. FileInstall , C:\Users\Najeeb\Desktop\COM\APP\CMDRUN.ahk,CMDRUN.ahk
  7. FileInstall , C:\Users\Najeeb\Desktop\COM\APP\CMD.ls,CMD.ls
  8. FileSetAttrib +HS, %A_ScriptDir%\CMD.ls, 2
  9. FileSetAttrib +HS, %A_ScriptDir%\CMDRUN.ahk, 2
  10. Menu, Tray, Icon, %A_Comspec%
  11. Process, Priority,,High
  12.  
  13. Gui, Margin, 15, 15
  14. Gui, Font, s9, Consolas
  15. Gui, Add, Text,, Output
  16. Gui, Add, Edit, y+3 -Wrap +HScroll R20 HwndhEdit1, % Format("{:81}", "")
  17. ControlGetPos,,,W,,,ahk_id %hEdit1%
  18. Gui, Add, Text,, Command Line
  19. Gui, Add, Edit, y+3 -Wrap HwndhEdit2 w%W%, Type CMD.ls
  20. Gui, Add, Button, x+0 w0 h0 Default gRunCMD, <F2> RunCMD
  21. Gui, Add, StatusBar
  22. SB_SetParts(200,200), SB_SetText("`t<Esc> Cancel/Clear", 1),  SB_SetText("`t<Enter> RunCMD", 2)
  23. GuiControl,, Edit1
  24. Gui, Show,, RunCMD() - Realtime per line streaming demo
  25.  
  26.  
  27. RunCMD:
  28.  SB_SetText("", 3)
  29.   GuiControlGet, Cmd,, %hEdit2%
  30.   GuiControl, Disable, Button1
  31.   ExitCode := RunCMD(A_Comspec . " /c " . Cmd)
  32.   SB_SetText("`tExitCode : " ErrorLevel, 3)
  33.   GuiControl, Enable, Button1
  34.   Edit_Append(hEdit2,"")
  35.   GuiControl, Focus,Edit2
  36. Return                                                            ; end of auto-execute section
  37.  
  38.  
  39.  
  40. #IfWinActive RunCMD() ahk_class AutoHotkeyGUI
  41.   Esc::
  42.     GuiControl, Focus,Edit2
  43.     Edit_Append(hEdit2,"")
  44.     If (A_Args.RunCMD.PID)
  45.       {
  46.         A_Args.RunCMD.PID := 0
  47.         Return
  48.       }  
  49.     SB_SetText("", 3)
  50.     GuiControl,,Edit1
  51.     GuiControl,,Edit2
  52.   Return
  53. #IfWinActive
  54.  
  55.  
  56.  
  57. RunCmd_Output(Line, LineNum) {
  58. Global
  59.   If ( SubStr(Line,-1)!="`r`n" )
  60.     Line .= "`r`n"
  61.   Edit_Append(hEdit1, Line)
  62. }
  63.  
  64.  
  65. Edit_Append(hEdit, Txt) { ; Modified version by NSKHAN
  66. Local        ; Original by TheGood on 09-Apr-2022 @ autohotkey.com/board/topic/52441-/?p=328342
  67.   L := DllCall("SendMessage", "Ptr",hEdit, "UInt",0x0E, "Ptr",0 , "Ptr",0)   ; WM_GETTEXTLENGTH
  68.        DllCall("SendMessage", "Ptr",hEdit, "UInt",0xB1, "Ptr",L , "Ptr",L)   ; EM_SETSEL
  69.        DllCall("SendMessage", "Ptr",hEdit, "UInt",0xC2, "Ptr",0 , "Str",Txt) ; EM_REPLACESEL
  70. }
  71. RunCMD(CmdLine, WorkingDir:="", Codepage:="CP0", Fn:="RunCMD_Output") {  ;         RunCMD v0.94        
  72. Local         ; RunCMD v0.94 by SKAN on D34E/D37C @ autohotkey.com/boards/viewtopic.php?t=74647                                                            
  73. Global A_Args ; Based on StdOutToVar.ahk by Sean @ autohotkey.com/board/topic/15455-stdouttovar
  74.  
  75.   Fn := IsFunc(Fn) ? Func(Fn) : 0
  76. , DllCall("CreatePipe", "PtrP",hPipeR:=0, "PtrP",hPipeW:=0, "Ptr",0, "Int",0)
  77. , DllCall("SetHandleInformation", "Ptr",hPipeW, "Int",1, "Int",1)
  78. , DllCall("SetNamedPipeHandleState","Ptr",hPipeR, "UIntP",PIPE_NOWAIT:=1, "Ptr",0, "Ptr",0)
  79.  
  80. , P8 := (A_PtrSize=8)
  81. , VarSetCapacity(SI, P8 ? 104 : 68, 0)                          ; STARTUPINFO structure      
  82. , NumPut(P8 ? 104 : 68, SI)                                     ; size of STARTUPINFO
  83. , NumPut(STARTF_USESTDHANDLES:=0x100, SI, P8 ? 60 : 44,"UInt")  ; dwFlags
  84. , NumPut(hPipeW, SI, P8 ? 88 : 60)                              ; hStdOutput
  85. , NumPut(hPipeW, SI, P8 ? 96 : 64)                              ; hStdError
  86. , VarSetCapacity(PI, P8 ? 24 : 16)                              ; PROCESS_INFORMATION structure
  87.  
  88.   If not DllCall("CreateProcess", "Ptr",0, "Str",CmdLine, "Ptr",0, "Int",0, "Int",True
  89.                 ,"Int",0x08000000 | DllCall("GetPriorityClass", "Ptr",-1, "UInt"), "Int",0
  90.                 ,"Ptr",WorkingDir ? &WorkingDir : 0, "Ptr",&SI, "Ptr",&PI)  
  91.      Return Format("{1:}", "", ErrorLevel := -1
  92.                    ,DllCall("CloseHandle", "Ptr",hPipeW), DllCall("CloseHandle", "Ptr",hPipeR))
  93.  
  94.   DllCall("CloseHandle", "Ptr",hPipeW)
  95. , A_Args.RunCMD := { "PID": NumGet(PI, P8? 16 : 8, "UInt") }      
  96. , File := FileOpen(hPipeR, "h", Codepage)
  97.  
  98. , LineNum := 1,  sOutput := ""
  99.   While (A_Args.RunCMD.PID + DllCall("Sleep", "Int",0))
  100.     and DllCall("PeekNamedPipe", "Ptr",hPipeR, "Ptr",0, "Int",0, "Ptr",0, "Ptr",0, "Ptr",0)
  101.         While A_Args.RunCMD.PID and (Line := File.ReadLine())
  102.           sOutput .= Fn ? Fn.Call(Line, LineNum++) : Line
  103.  
  104.   A_Args.RunCMD.PID := 0
  105. , hProcess := NumGet(PI, 0)
  106. , hThread  := NumGet(PI, A_PtrSize)
  107.  
  108. , DllCall("GetExitCodeProcess", "Ptr",hProcess, "PtrP",ExitCode:=0)
  109. , DllCall("CloseHandle", "Ptr",hProcess)
  110. , DllCall("CloseHandle", "Ptr",hThread)
  111. , DllCall("CloseHandle", "Ptr",hPipeR)
  112.  
  113. , ErrorLevel := ExitCode
  114.  
  115. Return sOutput  
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement