Page 1 of 2

open console hidden, send command to, return data from it?

Posted: Mon Jun 01, 2020 1:51 am
by Mythros
Hi all! So I'm trying to open a console window but open it hidden, send a command to NodeJS & return the data into the program from it.

How is this possible?

Any help is GREATLY appreciated!

Thanks! <3

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 5:58 am
by Mythros
Someone can help?

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 11:45 am
by mk-soft
Yon don't net OpenConsole for this.

You can use RunProgram and OpenWindow with flag #PB_Window_Invisible

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 3:31 pm
by Mythros
How?

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 4:37 pm
by Bitblazer

Code: Select all

Procedure.s GetCommandOutput(Command$, Parameter$, *ExitCode = 0)
  Define Output$
  Define ThisCommand.i
  
  ThisCommand = RunProgram(Command$, Parameter$, "Your Path here", #PB_Program_Open | #PB_Program_Hide | #PB_Program_Read)
  Output$ = ""
  If (ThisCommand <> 0)
    While ProgramRunning(ThisCommand)
      If AvailableProgramOutput(ThisCommand)
        Output$ + ReadProgramString(ThisCommand) + Chr(13)
      EndIf
    Wend
    
    If (*ExitCode <> 0)
      PokeI(*ExitCode, ProgramExitCode(ThisCommand))
    EndIf
    
    CloseProgram(ThisCommand) ; Close the connection to the program
  EndIf
  
  ProcedureReturn Output$
EndProcedure
Just set "Your Path here" appropriately and use this. This generally works, but special cases like ffmpeg might require minor changes depending on your needs.

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 4:49 pm
by Mythros
I am trying to send a command to node js & then after that, return the data back.

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 7:32 pm
by Mythros
This code seems to work, but for some reason is not passing the nodejs ".help" parameter to the cmd :

Code: Select all

Procedure.s GetCommandOutput ( Command$, Parameter$, *ExitCode = 0 )

  Define Output$
  Define ThisCommand.i

  ThisCommand = RunProgram ( Command$, Parameter$, "/", #PB_Program_Open | #PB_Program_Hide | #PB_Program_Read | #PB_Program_Hide )

  Output$ = ""

  If ( ThisCommand <> 0 )

    While ProgramRunning ( ThisCommand )

      If AvailableProgramOutput ( ThisCommand )

        Output$ + ReadProgramString ( ThisCommand ) + Chr ( 13 )

        Break

      EndIf

    Wend

    ; Close the connection to the program

    CloseProgram ( ThisCommand )

  EndIf

  ProcedureReturn Output$

EndProcedure

cmd$ = GetCommandOutput ( "node", ".help", 0 )

Debug ( cmd$ )

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 8:04 pm
by Bitblazer
Maybe the resulting error code would tell you, but you dont handle it. My guess would be that the executable can't be found because of the path.

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 8:13 pm
by Mythros
how to fix?

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 9:05 pm
by Bitblazer
Change

Code: Select all

cmd$ = GetCommandOutput ( "node", ".help", 0 )
To

Code: Select all

define CmdErrorCode.i
cmd$ = GetCommandOutput ( "node", ".help", @CmdErrorcode )
and check what the errorcode says. If my guess is right, your "node" command simply can't be found. The errorcode should give you a clue. Always evaluate the return codes at least when a problem shows up and during the initial development. Shortcuts in your code can come later when it works and is tested for years ;)

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 9:28 pm
by Mythros
It returned '0' however, my node js does exist. i just typed node into the command prompt & it said welcome to node.

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 11:02 pm
by Mythros
what to do?

Re: open console hidden, send command to, return data from i

Posted: Mon Jun 01, 2020 11:26 pm
by Kiffi
This code snippet saves a JavaScript file in the Temp folder and executes it with Node:

Code: Select all

EnableExplicit

Define TempJsFilename.s = GetTemporaryDirectory() + "temp.js"

CreateFile(0, TempJsFilename)
WriteStringN(0, "console.log('hello from node');")
CloseFile(0)

Define Dummy.s
Define RPE.s ; ReadProgramError
Define RPS.s ; ReadProgramString
Define Exitcode

Define NodeJs = RunProgram("cmd.exe", "/c node " + Chr(34) + TempJsFilename + Chr(34), "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error | #PB_Program_Hide)

If NodeJs
  
  While ProgramRunning(NodeJs)
    
    Dummy = ReadProgramError(NodeJs)
    
    If Dummy 
      RPE + Dummy + #CRLF$
    EndIf
    
    Dummy = ReadProgramString(NodeJs)
    
    If Dummy 
      RPS + Dummy + #CRLF$
    EndIf
    
  Wend
  
  Exitcode = ProgramExitCode(NodeJs)
  
  CloseProgram(NodeJs) ; Close the connection to the program
  
EndIf

Define Output.s

If RPS <> ""
  Output + "ReadProgramString: " + #CRLF$ + RPS + #CRLF$
EndIf

If RPE <> ""
  Output + "ReadProgramError: " + #CRLF$ + RPE + #CRLF$
EndIf

If Exitcode <> 0
  Output + "ExitCode: " + Exitcode  + #CRLF$
EndIf

MessageRequester("", Output)

DeleteFile(TempJsFilename)

Re: open console hidden, send command to, return data from i

Posted: Tue Jun 02, 2020 12:11 am
by Mythros
I thought it was Node I needed, apparently not. I'm trying to send a command to JSDocs with some stored file to & from locations so I can overwrite some documentation I am writing for a library I made. Then read the data it returns back into my program.

Re: open console hidden, send command to, return data from i

Posted: Tue Jun 02, 2020 12:26 am
by Kiffi
Mythros wrote:I thought ...
:| I'm out. May the others take care of your problem now.