CreateProcess:TStream(Path$)

BlitzMax Forums/BlitzMax Programming/CreateProcess:TStream(Path$)

In BP there is the function CreateProcess(Path$).
Is in BMX anything like that, too?
If not, I'll try to write a module containing it myself.

I did write some functions to do it, but there was an unexplainable memory exception so I gave up after 110 hours of trying to find why... Good luck :D

No.

There's a function to execute a file. Here you go, http://www.blitzwiki.org/index.php/System_Commands

Hope that helps. It's not the same thing but it might help.

Yes it is, system_ is CreateProcess.

No it isn't. System_ waits for the file executed to finish before proceeding with your program. Now if the file you want executed is a server program (like for instance Apache) that "never" finished, your program is going to hang indefinitely.

More importantly tho', if your program is ended (for instance by someone killing it in Taskmanager, or an overzealous windows task scheduler that reckons your program "stopped responding") it's going to take the program you've executed with it.

It is not, by the most broad definition, the same as CreateProcess, which actually launched the program in question in a seperate process. Except on Linux, but that's thanks largely to the OS.

Here you go, a simple fix i just made.
Link

'First of all, thanks a lot to all of you!
'It really helps!

'Sorry for late answering but I found some functions which may help:
Extern "Win32"
'Sorry only usable on win32
  Function WinExec ( Cmd:Byte Ptr , Show )
  Function ShellExecuteA ( Wnd , Cmd:Byte Ptr , File:Byte Ptr , Params:Byte Ptr , Dir:Byte Ptr , Show )
  Function CreateProcessA ( App:Byte Ptr , Cmd:Byte Ptr , PrAt , ThAt , InHandels , Flags , Env:Byte Ptr , Dir:Byte Ptr , S , P )
EndExtern
'The last of them is very intressting but not easy not understand ( as I think ).
'ShellExecuteA(...) seems to be equal to ExecFile(Path$) in BB.


I started working on function doing this.
It's not finished, but it'll later so.

CreateProcess.c:
#include <windows.h>

int WinGetStartupInfo ( void * Info )
{
  GetStartupInfo ( Info ) ;
  return 0 ;
}


CreateProcess.bmx:
Strict

Import "CreateProcess.c"

Local Stream:TStream = CreateProcess ( "calc.exe" )

Delay 10000
While Not Eof ( Stream )
  Print ReadLine ( Stream )
Wend

Function CreateProcess:TStream ( Cmd$ )
?Win32
  Local Stream:TProcessStream = New TProcessStream

  Stream.ProcInfo = New TProcInfo
  Stream.StartInfo = New TStartInfo
'  Stream.StartInfo.Size = SizeOf Stream.StartInfo
DebugStop
  Print WinGetStartupInfo ( Stream.StartInfo )
  Print WinCreatePipe ( Varptr Stream.In , Varptr Stream.StartInfo.StdOutput , Null , 0 )
  Print WinCreatePipe ( Varptr Stream.StartInfo.StdInput , Varptr Stream.Out , Null , 0 )
  Stream.StartInfo.StdError = Stream.StartInfo.StdOutput
  Print WinCreateProcess ( "".ToCString ( ) , Cmd.ToCString ( ) , Null , Null , False , 0 , Null , Null , Stream.StartInfo , Stream.ProcInfo )
  Return Stream

  Type TProcInfo
    Field Process
    Field Thread
    Field ProcessId
    Field ThreadId
  EndType

  Type TStartInfo
    Field Size
    Field Reserved1
    Field Desktop
    Field Title
    Field X
    Field Y
    Field XSize
    Field YSize
    Field XCountChars
    Field YCountChars
    Field FillAttribute
    Field Flags
    Field ShowWindow:Short
    Field Reserved2:Short
    Field Reserved3:Byte
    Field StdInput
    Field StdOutput
    Field StdError
Field Test:Long
  EndType

  Type TProcessStream Extends TStream
    Field In
    Field Out
    Field StartInfo:TStartInfo
    Field ProcInfo:TProcInfo

    Method Eof ( )
    EndMethod
  EndType

  Extern "C"
    Function WinGetStartupInfo ( StartInfo:TStartInfo )
  EndExtern

  Extern "Win32"
    Function WinCreatePipe ( ReadPipe Ptr , WritePipe Ptr , PipeAttributes:Byte Ptr , Size ) = "CreatePipe@16"
    Function WinCreateProcess ( App:Byte Ptr , Cmd:Byte Ptr , PrAt:Byte Ptr , ThAt:Byte Ptr , IH , Flags , Env:Byte Ptr , Dir:Byte Ptr , S:TStartInfo , P:TProcInfo ) = "CreateProcessA@40"
    Function WinReadFile ( File , Buffer:Byte Ptr , ReadLen , BytesRead Ptr , Overlapped:Byte Ptr ) = "ReadFile"
    Function WinWriteFile ( File , Buffer:Byte Ptr , WriteLen , BytesWritten Ptr , Overlapped:Byte Ptr ) = "WriteFile"
    Function WinCloseHandle ( Handle , abc ) = "CloseHandle"
  EndExtern
?
  system_ Cmd
EndFunction

I've some problems with GetStartupInfo(...) function.
The program stops in debug mode by clicking any button.