Redirecting A Console Output Using API functions In PowerBuilder
Redirect Console Output In PowerBuilder
![](https://pblib.com/wp-content/uploads/2021/03/RedirectConsoleOutput.gif)
![](https://pblib.com/wp-content/uploads/2021/03/RedirectConsoleOutput.png)
Create objects nvo_console , nvo_console_arg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | forward global type nvo_console from nonvisualobject end type type str_startup_info from structure within nvo_console end type type str_process_info from structure within nvo_console end type type str_security_attributes from structure within nvo_console end type end forward type str_startup_info from structure long cb long lpreserved long lpdesktop long lptitle long dwx long dwy long dwxsize long dwysize long dwxcountchars long dwycountchars long dwfillattribute long dwflags long wshowwindow long cbreserved2 long lpreserved2 long hstdinput long hstdoutput long hstderror end type type str_process_info from structure long hprocess long hthread long dwprocessid long dwthreadid end type type str_security_attributes from structure Long nLength Long lpSecurityDescriptor Long bInheritHandle end type global type nvo_console from nonvisualobject end type global nvo_console nvo_console type prototypes Function ULong CreatePipe(Ref ULong phReadPipe,Ref ULong phWritePipe, Ref str_security_attributes lpPipeAttributes,ULong nSize) Library "kernel32.dll" Function Long ReadFile( ULong hFile, Ref Blob lpBuffer, Long nNumberOfBytesToRead, Ref Long lpNumberOfBytesRead, ULong lpOverlapped) Library "kernel32.dll" Function ULong GetLastError() Library "kernel32.dll" Function Boolean CloseHandle(ULong h) Library 'kernel32.dll' Function ULong PeekNamedPipe(ULong hNamedPipe,Ref Blob lpBuffer,ULong nBufferSize, Ref ULong lpBytesRead,Ref ULong lpTotalBytesAvail,Ref ULong lpBytesLeftThisMessage) Library "kernel32.dll" Function Boolean CreateProcess ( String lpApplicationName, String lpCommandLine, ULong lpProcessAttributes, ULong lpThreadAttributes, Boolean bInheritHandles, & ULong dwCreationFlags, ULong lpEnvironment, String lpCurrentDirectory, str_startup_info lpStartupInfo, Ref str_process_info lpProcessInformation ) Library "kernel32.dll" Alias For "CreateProcessA;Ansi" end prototypes type variables Private str_process_info istr_proc_info String is_msg UnsignedLong iul_process_handle = 0 nvo_console_arg invo_arg end variables forward prototypes public function integer of_run () public subroutine of_ini (nvo_console_arg anvo_arg) end prototypes public function integer of_run ();//==================================================================== // Function: nvo_console.of_run() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2021/03/22 //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Constant ULong STARTF_USESHOWWINDOW = 1 Constant ULong STARTF_USESTDHANDLES = 256 Constant ULong DUPLICATE_SAME_ACCESS = 2 Constant ULong BUFSIZE = 1024 Constant ULong PIPE_READMODE_BYTE = 0 Constant ULong PIPE_NOWAIT = 1 Long ll_CreationFlags, ll_null, lBytesRead, lPipeOutLen String ls_null, ls_app_name, ls_cmd, ls_dir, ls_read_string UnsignedLong lul_null, luOverLapped, lul_err UnsignedLong hPipeRead,hPipeWrite UnsignedLong lpBytesRead, lpTotalBytesAvail, lpBytesLeft, lpBufOutLen Boolean lb_created Blob lbl_read Int i String ls_msg str_startup_info lstr_startup_info str_security_attributes lstr_secure_attrib ls_cmd = invo_arg.is_cmd invo_arg.ib_run = True If ls_cmd = "" Then invo_arg.ib_run = False Return 1 End If If iul_process_handle > 0 Then invo_arg.of_refresh("Error create pipe failed") invo_arg.ib_run = False Return -1 End If lbl_read = Blob(Space(BUFSIZE)) SetNull(ll_null) SetNull(ls_null) SetNull(lul_null) luOverLapped = 0 ls_msg = "" ll_CreationFlags = 0 lstr_secure_attrib.nLength = 12 lstr_secure_attrib.bInheritHandle = 1 lstr_secure_attrib.lpSecurityDescriptor = 0 lstr_startup_info.cb = 17 * 4 lstr_startup_info.dwFlags = STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES lstr_startup_info.wShowWindow = 0 If CreatePipe(hPipeRead, hPipeWrite, lstr_secure_attrib, 0) = 0 Then invo_arg.of_refresh("Error create pipe failed") invo_arg.ib_run = False Return -1 End If lstr_startup_info.hStdOutput = hPipeWrite lstr_startup_info.hStdError = hPipeWrite lstr_startup_info.hStdInput = hPipeWrite ls_app_name = ls_null ls_dir = ls_null lb_created = CreateProcess(ls_app_name, ls_cmd, ll_null, ll_null, True, ll_CreationFlags, ll_null, ls_dir, lstr_startup_info, istr_proc_info) If lb_created Then iul_process_handle = istr_proc_info.hprocess CloseHandle(hPipeWrite) CloseHandle(istr_proc_info.hThread) lBytesRead = ReadFile( hPipeRead, lbl_read, BUFSIZE,lPipeOutLen, 0) If lBytesRead = 0 Then lul_err = GetLastError() If lul_err = 109 Then invo_arg.of_refresh("error reading error 109 - broken pipe") Else invo_arg.of_refresh("Error Eeading " + String(lul_err)) End If End If Do While lBytesRead > 0 And lPipeOutLen > 0 ls_msg = String(BlobMid(lbl_read,1, lPipeOutLen), encodingANSI!) is_msg += ls_msg invo_arg.of_refresh(ls_msg) lBytesRead = ReadFile( hPipeRead, lbl_read, BUFSIZE,lPipeOutLen, 0) Loop End If closeHandle(hPipeRead) closeHandle(istr_proc_info.hprocess) iul_process_handle = 0 invo_arg.ib_run = False Return 1 end function public subroutine of_ini (nvo_console_arg anvo_arg);invo_arg = anvo_arg end subroutine on nvo_console.create call super::create TriggerEvent( this, "constructor" ) end on on nvo_console.destroy TriggerEvent( this, "destructor" ) call super::destroy end on event destructor;if iul_process_handle > 0 then closeHandle(iul_process_handle) end if end event |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | forward global type nvo_console_arg from nonvisualobject end type end forward global type nvo_console_arg from nonvisualobject end type global nvo_console_arg nvo_console_arg type variables window iw String is_cmd nvo_console invo_console Boolean ib_run end variables forward prototypes public function integer of_run (string as_cmd) public subroutine of_refresh (string as_msg) end prototypes public function integer of_run (string as_cmd);//==================================================================== // Function: nvo_console_arg.of_run() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_cmd //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2021/03/22 //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== If ib_run then return 0 If Not IsValid(invo_console) Then invo_console = Create nvo_console End If is_cmd = as_cmd invo_console.of_ini(This) invo_console.Post Function of_run() Return 1 end function public subroutine of_refresh (string as_msg);If IsValid(iw) Then iw.Dynamic Function of_refresh(as_msg) End If end subroutine on nvo_console_arg.create call super::create TriggerEvent( this, "constructor" ) end on on nvo_console_arg.destroy TriggerEvent( this, "destructor" ) call super::destroy end on event constructor;ErrorReturn lErrorReturn lErrorReturn = SharedObjectRegister("nvo_console", "invo_console") lErrorReturn = SharedObjectGet("invo_console", invo_console) end event event destructor;ErrorReturn lErrorReturn lErrorReturn = SharedObjectUnRegister("invo_console") destroy invo_console end event |
Create Window Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | forward global type w_main from window end type type st_1 from statictext within w_main end type type cb_run from commandbutton within w_main end type type mle_log from multilineedit within w_main end type type sle_cmd from singlelineedit within w_main end type end forward global type w_main from window integer width = 2345 integer height = 1296 boolean titlebar = true string title = "Redirect Console Output" boolean controlmenu = true boolean minbox = true boolean maxbox = true boolean resizable = true long backcolor = 67108864 string icon = "AppIcon!" boolean center = true st_1 st_1 cb_run cb_run mle_log mle_log sle_cmd sle_cmd end type global w_main w_main type variables nvo_console_arg invo_arg end variables forward prototypes public subroutine of_refresh (string as_msg) end prototypes public subroutine of_refresh (string as_msg);mle_log.Text += as_msg mle_log.Scroll(mle_log.LineCount()) end subroutine on w_main.create this.st_1=create st_1 this.cb_run=create cb_run this.mle_log=create mle_log this.sle_cmd=create sle_cmd this.Control[]={this.st_1,& this.cb_run,& this.mle_log,& this.sle_cmd} end on on w_main.destroy destroy(this.st_1) destroy(this.cb_run) destroy(this.mle_log) destroy(this.sle_cmd) end on type st_1 from statictext within w_main integer x = 46 integer y = 48 integer width = 210 integer height = 76 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 string text = "Cmd:" alignment alignment = right! boolean focusrectangle = false end type type cb_run from commandbutton within w_main integer x = 1851 integer y = 40 integer width = 265 integer height = 104 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Run" end type event clicked;If Not IsValid(invo_arg) Then invo_arg = Create nvo_console_arg End If If invo_arg.ib_run Then Return String ls_cmd mle_log.text = "" ls_cmd = sle_cmd.Text If IsNull(ls_cmd) Or Len(Trim(ls_cmd)) = 0 Then Return End If invo_arg.iw = Parent invo_arg.of_run(ls_cmd) end event type mle_log from multilineedit within w_main integer x = 133 integer y = 172 integer width = 2030 integer height = 948 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 boolean hscrollbar = true boolean vscrollbar = true boolean autohscroll = true boolean autovscroll = true borderstyle borderstyle = stylelowered! end type type sle_cmd from singlelineedit within w_main integer x = 283 integer y = 44 integer width = 1550 integer height = 92 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 string text = "ping localhost -n 10" borderstyle borderstyle = stylelowered! end type |
Find Projects On Github click here
Good Luck!