#include <stdio.h>
#include <iostream.h>
#pragma comment(linker,"/MERGE:.rdata=.text")
#pragma comment(linker,"/FILEALIGN:512 /SECTION:.text,EWRX /IGNORE:4078")
#pragma comment(linker,"/OPT:NOWIN98")
typedef struct {HANDLE hRead; HANDLE hWrite;} PIPE;
PIPE input, output;
PROCESS_INFORMATION pi;
void init(void)
{
SECURITY_ATTRIBUTES sa;
STARTUPINFO si;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
CreatePipe(&input.hRead, &input.hWrite, &sa, 0);
CreatePipe(&output.hRead, &output.hWrite, &sa, 0);
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = input.hRead;
si.hStdOutput = output.hWrite;
si.hStdError = output.hWrite;
CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
}
int main()
{
init();
DWORD dwBuff;
char buff[4096];
char cmd[1024];
Sleep(500);
ReadFile(output.hRead,buff ,4096, &dwBuff, NULL);
buff[dwBuff]='\0';
printf("%s",buff);
while(1)
{
cmd[0]='\0';
cin.getline(cmd,1024);
strcat(cmd,"\r\n");
WriteFile(input.hWrite, cmd, strlen(cmd), &dwBuff, NULL);
do
{
ReadFile(output.hRead, buff, 4096, &dwBuff, NULL);
buff[dwBuff]='\0';
printf("%s",buff);
if(buff[dwBuff-1]=='>')
break;
buff[0]='\0';
}while(dwBuff!=0);
}
return 0;
}
Option Explicit
Private Type PIPE
hRead As Long
hWrite As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_HIDE = 0
Private Const STARTF_USESTDHANDLES = &H100
Private input1 As PIPE
Private output1 As PIPE
Private pi As PROCESS_INFORMATION
Private Sub init()
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
sa.nLength = Len(sa)
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
Call CreatePipe(input1.hRead, input1.hWrite, sa, 0)
Call CreatePipe(output1.hRead, output1.hWrite, sa, 0)
si.cb = Len(si)
si.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
si.wShowWindow = SW_HIDE
si.hStdInput = input1.hRead
si.hStdOutput = output1.hWrite
si.hStdError = output1.hWrite
Call CreateProcessA(vbNullString, "CMD.exe", 0&, 0&, 1&, _
0, 0&, vbNullString, si, pi)
End Sub
Private Sub Command1_Click()
Dim dwBuff As Long
Dim buff As String * 4096
Dim cmd As String * 1024
cmd = vbNullString
cmd = InputBox("Команда")
cmd = RTrim(cmd) & vbCrLf
Call WriteFile(input1.hWrite, cmd, Len(cmd), dwBuff, ByVal 0&)
Sleep (500)
Do
Call ReadFile(output1.hRead, buff, 4096, dwBuff, ByVal 0&)
buff = Left(buff, dwBuff)
Text1.Text = Text1.Text & RTrim(buff)
If (Right(RTrim(buff), 1) = ">") Then Exit Do
buff = vbNullString
Loop While (dwBuff <> 0)
End Sub
Private Sub Form_Load()
Dim dwBuff As Long
Dim buff As String * 4096
Dim cmd As String * 1024
init
Sleep (500)
Call ReadFile(output1.hRead, buff, 4096, dwBuff, ByVal 0&)
Text1.Text = RTrim(buff)
End Sub
Сейчас этот форум просматривают: Google-бот, SemrushBot и гости: 54