|
本帖最后由 繁星 于 2015-12-17 11:02 编辑
原始代码:#include "windows.h"
#include "resource.h"
HWND hInst;
LRESULT CALLBACK PING(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD hWrite,hRead;
STARTUPINFO startupinfo;
PROCESS_INFORMATION pinfo;
SECURITY_ATTRIBUTES sat;
TCHAR szBuffer[1024],ipBuffer[20];
int bytesRead;
HWND hwndEdit;
switch (message)
{
case WM_INITDIALOG:
SendMessage(hDlg,WM_SETICON,ICON_BIG,LoadIcon(hInst,ICO_PING));
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_EXIT:
EndDialog(hDlg, LOWORD(wParam));
break;
case IDOK:
RtlZeroMemory(ipBuffer,20);
strcpy(ipBuffer,"ping ");
GetDlgItemText(hDlg,IDC_IPADDRESS,ipBuffer+5,20);
hwndEdit=GetDlgItem(hDlg,IDC_OUTINFOR);
sat.nLength=sizeof(SECURITY_ATTRIBUTES);
sat.bInheritHandle=TRUE;
sat.lpSecurityDescriptor=NULL;
if(CreatePipe(&hRead,&hWrite,&sat,0)==0) //创建匿名管道
MessageBox(hDlg,TEXT("创建管道失败"),TEXT("PING"),MB_OK);
else
{
startupinfo.cb=sizeof(STARTUPINFO);
GetStartupInfo(&startupinfo);
startupinfo.hStdOutput=hWrite; //用管道的写端代替控制台程序的输出端以便得到输出的信息
startupinfo.hStdError=hWrite;0
startupinfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
startupinfo.wShowWindow=SW_HIDE; //隐藏控制台程序窗口
if(CreateProcess(NULL, ipBuffer,NULL,NULL,TRUE,NULL,NULL,NULL,&startupinfo,&pinfo)==NULL)
MessageBox(hDlg,TEXT("创建进程失败"),TEXT("PING"),MB_OK);
else
{
CloseHandle(hWrite); //关闭写端,因为写端已经给了控制台程序,不能存在两个写端
while(TRUE)
{
RtlZeroMemory(szBuffer,1024);
if(ReadFile(hRead,szBuffer,1023,&bytesRead,NULL)==NULL) //注意ReadFile的第一个参数正是读端的句柄
break;
SendMessage(hwndEdit,EM_SETSEL,-1,0);
SendMessage(hwndEdit,EM_REPLACESEL,FALSE,szBuffer); //循环读入信息直到没有信息可读
}
}
CloseHandle(hWrite);
}
break;
}
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInst=hInstance;
DialogBoxParam(hInstance,(LPCTSTR)DLG_MAIN,NULL,(DLGPROC)PING,NULL);
return 0;
}
我写的:#include <WinAPI.au3>
#include <WinAPISys.au3>
#include <GuiEdit.au3>
#include <NamedPipes.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$CMDShell = GUICreate("CMDShell", 391, 257, 192, 124)
$Label1 = GUICtrlCreateLabel("命令:", 9, 7, 31, 17)
$Input1 = GUICtrlCreateInput("", 45, 4, 281, 21)
$Button1 = GUICtrlCreateButton("执行", 335, 1, 41, 25)
GUICtrlCreateGroup("执行结果", 6, 28, 377, 222)
$Edit1 = GUICtrlCreateEdit("", 12, 43, 365, 201)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
CaptureConsole()
EndSwitch
WEnd
Func CaptureConsole()
Local $hRead, $hWrite, $bytesRead, $SzBuffer = DllStructCreate('wchar SzBuffer[1024]'), $IpBuffer = DllStructCreate('wchar IpBuffer[20]')
Local $tagSTARTUPINFO = "int Size;ptr Reserved1;ptr Desktop;ptr Title;int X;int Y;int XSize;int YSize;int XCountChars;" & _
"int YCountChars;int FillAttribute;int Flags;short ShowWindow;short Reserved2;ptr Reserved3;int StdInput;" & _
"int StdOutput;int StdError"
Local $tagPROCESS_INFORMATION = "hwnd hProcess;hwnd hThread;int ProcessID;int ThreadID"
Local $tagSECURITY_ATTRIBUTES = "int Length;ptr Descriptor;int InheritHandle"
_WinAPI_ZeroMemory($IpBuffer, 20)
Local $startupinfo = DllStructCreate($tagSTARTUPINFO)
Local $pinfo = DllStructCreate($tagPROCESS_INFORMATION)
Local $sat = DllStructCreate($tagSECURITY_ATTRIBUTES)
DllStructSetData($sat, 'Length', DllStructGetSize($sat))
DllStructSetData($sat, 'InheritHandle', True)
DllStructSetData($sat, 'Descriptor', Null)
$IpBuffer = GUICtrlRead($Input1)
If _NamedPipes_CreatePipe($hRead, $hWrite, $sat) <> 1 Then
MsgBox(0, '', '创建匿名管道失败!')
Else
DllStructSetData($startupinfo, 'Size', DllStructGetSize($startupinfo))
$startupinfo = _WinAPI_GetStartupInfo()
DllStructGetData($startupinfo, 'StdOutput', $hWrite)
DllStructSetData($startupinfo, 'StdError', $hWrite)
DllStructSetData($startupinfo, 'Flags', $STARTF_USESHOWWINDOW & '|' & $STARTF_USESTDHANDLES)
DllStructSetData($startupinfo, 'ShowWindow', @SW_HIDE)
If _WinAPI_CreateProcess(Null, $IpBuffer, Null, Null, True, Null, Null, Null, $startupinfo, $pinfo) <> 1 Then
MsgBox(0, '', '创建子进程失败!')
Else
_WinAPI_CloseHandle($hWrite)
While 1
_WinAPI_ZeroMemory($szBuffer, 1024)
If _WinAPI_ReadFile($hRead, $szBuffer, 1023, $bytesRead, Null) = '' Then
ExitLoop
Else
_GUICtrlEdit_SetSel($Edit1, -1, 0)
_GUICtrlEdit_ReplaceSel($Edit1, $szBuffer, False)
EndIf
WEnd
EndIf
EndIf
EndFunc
|
|