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

Just starting out? Need help? Post your questions and find answers here.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post 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
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

Someone can help?
User avatar
mk-soft
Always Here
Always Here
Posts: 6274
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post by mk-soft »

Yon don't net OpenConsole for this.

You can use RunProgram and OpenWindow with flag #PB_Window_Invisible
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

How?
Bitblazer
Enthusiast
Enthusiast
Posts: 762
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

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

Post 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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

I am trying to send a command to node js & then after that, return the data back.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post 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$ )
Bitblazer
Enthusiast
Enthusiast
Posts: 762
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

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

Post 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.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

how to fix?
Bitblazer
Enthusiast
Enthusiast
Posts: 762
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

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

Post 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 ;)
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

It returned '0' however, my node js does exist. i just typed node into the command prompt & it said welcome to node.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post by Mythros »

what to do?
User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

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

Post 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)
Hygge
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

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

Post 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.
User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

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

Post by Kiffi »

Mythros wrote:I thought ...
:| I'm out. May the others take care of your problem now.
Hygge
Post Reply