[已解决]自己转换了一段代码帮忙看下哪里有问题
本帖最后由 繁星 于 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;
TCHARszBuffer,ipBuffer;
int bytesRead;
HWNDhwndEdit;
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'), $IpBuffer = DllStructCreate('wchar IpBuffer')
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
本帖最后由 ceoguang 于 2015-12-11 14:57 编辑
#include <WinAPI.au3>
#Include <WinAPIEx.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("ping www.baidu.com", 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('char SzBuffer')
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"
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,DllStructGetPtr($sat)) <> True Then
MsgBox(0, '', '创建匿名管道失败!')
Else
DllStructSetData($startupinfo, 'Size', DllStructGetSize($startupinfo))
;$startupinfo = _WinAPI_GetStartupInfo()
DllStructGetData($startupinfo, 'StdOutput', $hWrite)
DllStructSetData($startupinfo, 'StdError', $hWrite)
DllStructSetData($startupinfo, 'Flags', BitOR($STARTF_USESHOWWINDOW,$STARTF_USESTDHANDLES))
DllStructSetData($startupinfo, 'ShowWindow', @SW_HIDE)
If _WinAPI_CreateProcess('', GUICtrlRead($Input1), Null, Null, True, 0, Null, Null, DllStructGetPtr($startupinfo), DllStructGetPtr($pinfo)) <> 1 Then
MsgBox(0, '', '创建子进程失败!')
Else
_WinAPI_CloseHandle($hWrite)
While 1
_WinAPI_ZeroMemory(DllStructGetPtr($SzBuffer), DllStructGetSize($SzBuffer))
If _WinAPI_ReadFile($hRead, DllStructGetPtr($SzBuffer), DllStructGetSize($SzBuffer), $bytesRead) = False Then
ConsoleWrite(StringFormat('ReadFile error:%d\r\nReadFile error msg:%s\r\n',_WinAPI_GetLastError(),_WinAPI_GetLastErrorMessage()))
ExitLoop
Else
_GUICtrlEdit_SetSel($Edit1, -1, 0)
_GUICtrlEdit_ReplaceSel($Edit1, DllStructGetData($SzBuffer,1), False)
EndIf
WEnd
EndIf
_WinAPI_CloseHandle($hRead)
_WinAPI_CloseHandle(DllStructGetData($startupinfo,'hProcess'))
_WinAPI_CloseHandle(DllStructGetData($startupinfo,'hThread'))
EndIf
EndFunc ;==>CaptureConsole
回复 2# ceoguang
大哥,非常感谢,但为什么最后还是没有取不到值呢,XP测试 回复 4# ceoguang
我这里都是XP环境,我又换了台机子测试了一下,还是一样,取不到值,不知道哪里出问题了~ 回复 4# ceoguang
测试了C版的代码,完美通过,版主热心,学习了 测试了C版的代码 这个好,支持一下,一直还想问呢,想想还是先学基础,今日有幸看到,收藏学习了。 回复 4# ceoguang
C大,今天刚好在网吧windows7 64位测试了一下,确实可以的,但XP下却不行,为什么呢 回复 9# 繁星
没有XP环境,请在SITE中运行看看有没有输出,或者PM我提供联系方式 回复 10# ceoguang
可以运行,无错误
页:
[1]