|
Post by wbtcpip on Jun 1, 2023 16:00:26 GMT 1
I'm using System to run an external application (let's say ffmpeg for ex)
the gfa help show that System has an option
StdIn h Specifies a handle that will be used as the standard input handle to the process.
how i can write bytes to that handle?
is there a little example?
|
|
|
Post by (X) on Jun 1, 2023 16:56:18 GMT 1
I asked ChatGPT (Bing) Sure! Here’s an example of how to redirect standard input in VB6: Private Sub Command1_Click() Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim hPipeRead As Long, hPipeWrite As Long Dim security As SECURITY_ATTRIBUTES
security.nLength = Len(security) security.bInheritHandle = 1& security.lpSecurityDescriptor = 0&
If CreatePipe(hPipeRead, hPipeWrite, security, 0&) Then start.cb = Len(start) start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW start.hStdInput = hPipeRead start.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE) start.hStdError = GetStdHandle(STD_ERROR_HANDLE) start.wShowWindow = SW_HIDE
If CreateProcess(vbNullString, "cmd.exe", ByVal 0&, ByVal 0&, 1&, _ NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, start, proc) Then CloseHandle hPipeRead WriteToPipe "dir" & vbCrLf, hPipeWrite WriteToPipe "exit" & vbCrLf, hPipeWrite CloseHandle hPipeWrite WaitForSingleObject proc.hProcess, INFINITE CloseHandle proc.hThread CloseHandle proc.hProcess End If End If End Sub
Private Sub WriteToPipe(ByVal strData As String, ByVal hPipe As Long) Dim ret As Long, lngWritten As Long
ret = WriteFile(hPipe, ByVal strData, Len(strData), lngWritten, ByVal 0&) End Sub Copy This code creates a pipe and uses it to redirect the standard input of a new process (in this case cmd.exe). It then writes the commands dir and exit to the pipe. This causes the new process to execute these commands and then exit. Is there anything else you would like to know? I am not familiar with pipes etc Looks a bit complicated, but, what doesn't at first?...
|
|
|
Post by (X) on Jun 1, 2023 17:02:47 GMT 1
And in GFA-BASIC 32 for Windows? OpenW 1
Dim hRead As Handle, hWrite As Handle Dim security As SECURITY_ATTRIBUTES
security.nLength = Len(security) security.bInheritHandle = 1& security.lpSecurityDescriptor = 0&
If CreatePipe(hRead, hWrite, security, 0&) Then System "cmd.exe", StdIn hRead CloseHandle hRead WriteToPipe "dir" & vbCrLf, hWrite WriteToPipe "exit" & vbCrLf, hWrite CloseHandle hWrite End If
Do Sleep Until Me Is Nothing
Sub WriteToPipe(ByVal strData As String, ByVal hPipe As Handle) Dim ret As Long, lngWritten As Long ret = WriteFile(hPipe, ByVal strData, Len(strData), lngWritten, ByVal 0&) End Sub
SECURITY_ATTRIBUTES is a structure used in the Windows API to specify security information for objects such as pipes and processes. In GFA-BASIC 32 for Windows, you can define it using the Type statement like this:
Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type This structure has three members: nLength, which specifies the size of the structure; lpSecurityDescriptor, which is a pointer to a security descriptor; and bInheritHandle, which indicates whether the handle to the object can be inherited by child processes.
|
|
|
Post by (X) on Jun 1, 2023 17:11:41 GMT 1
The code provided appears to mimic opening a command console and typing dir followed by pressing the return key and then typing exit followed by pressing the return key. The code creates a pipe and uses it to redirect the standard input of the cmd.exe process to the read end of the pipe. Then it writes the commands dir and exit to the write end of the pipe, which causes them to be executed by the cmd.exe process.
|
|
|
Post by wbtcpip on Jun 1, 2023 17:13:02 GMT 1
i guess most of this stuff is not required if i use the command "System" of GB32. System will already return to me an stdin handle. i only wonder how to write to that handle.
|
|
|
Post by (X) on Jun 1, 2023 17:13:42 GMT 1
I'm using System to run an external application (let's say ffmpeg for ex)
the gfa help show that System has an option
StdIn h Specifies a handle that will be used as the standard input handle to the process.
how i can write bytes to that handle?
is there a little example?
Can you elaborate on: 'write bytes'?
|
|
|
Post by (X) on Jun 1, 2023 17:16:03 GMT 1
You can write bytes to the standard input handle of a process launched using the System command in a similar way to how it is done in the code you provided earlier. Here’s an example that shows how to write bytes to the standard input of an external application (in this case ffmpeg) using a pipe:
OpenW 1
Dim hRead As Handle, hWrite As Handle Dim security As SECURITY_ATTRIBUTES
security.nLength = Len(security) security.bInheritHandle = 1& security.lpSecurityDescriptor = 0&
If CreatePipe(hRead, hWrite, security, 0&) Then System "ffmpeg", StdIn hRead CloseHandle hRead WriteToPipe "your data here", hWrite CloseHandle hWrite End If
Do Sleep Until Me Is Nothing
Sub WriteToPipe(ByVal strData As String, ByVal hPipe As Handle) Dim ret As Long, lngWritten As Long ret = WriteFile(hPipe, ByVal strData, Len(strData), lngWritten, ByVal 0&) End Sub
This code creates a pipe and uses it to redirect the standard input of the ffmpeg process to the read end of the pipe. Then it writes data to the write end of the pipe using the WriteToPipe subroutine. You can modify the "your data here" string to contain the data you want to write to the standard input of the ffmpeg process.
Is this what you were looking for?
|
|
|
Post by wbtcpip on Jun 1, 2023 17:27:54 GMT 1
yes thank you i will give it a try
|
|
|
Post by (X) on Jun 1, 2023 17:32:15 GMT 1
What is the byte data you want to send? Is it actual bytes or a string of chars?
|
|