View Full Version : 重定向问题


老土
04-10-2007, 04:06 AM
如下代码:我翻译网上的c++例程。为何输出被visual studio的输出窗口截获了,我却取不到。程序运行第一次不错,可第二次就报错了,也不知道原因。

private void button3_Click(object sender, System.EventArgs e)
{
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.nLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;
sa.bInheritHandle = true;

System.IntPtr hread = IntPtr.Zero;
System.IntPtr hwrite = IntPtr.Zero;


if (!CreatePipe(ref hread, ref hwrite, ref sa, 0))
MessageBox.Show("管道创建失败!");

STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
GetStartupInfo(ref si);
si.hStdOutput = hwrite;
si.hStdError = hwrite;
si.wShowWindow = 0;
si.dwFlags = 0x100 | 0x1;

PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

if (!CreateProcess(null,"d:\\windows\\system32\\cmd.exe /c dir /?",
IntPtr.Zero,IntPtr.Zero,true,0x20, IntPtr.Zero,null,ref si, ref pi))
{
MessageBox.Show("进程创建失败!");
}

CloseHandle(hwrite);

System.Text.StringBuilder sb = new System.Text.StringBuilder(4096);
while (true)
{
int nNum=0;
if (false == ReadFile(hread,sb,4096,ref nNum,IntPtr.Zero))
break;
this.textBox1.Text += sb.ToString();
}

CloseHandle(hread);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}





[DllImport("Kernel32.dll")]
public static extern bool CreatePipe(
ref System.IntPtr hReadPipe,
ref System.IntPtr hWritePipe,
ref SECURITY_ATTRIBUTES lpPipeAttributes,
int nSize
);

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public System.IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}

[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public int wShowWindow;
public int cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}

[DllImport("Kernel32.dll")]
public static extern void GetStartupInfo(
ref STARTUPINFO lpStartupInfo
);

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[DllImport("Kernel32.dll")]
public static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
int dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);

[DllImport("Kernel32.dll")]
public static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
int dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);

[DllImport("Kernel32.dll")]
public static extern bool CloseHandle(
IntPtr hObject
);

[DllImport("Kernel32.dll")]
public static extern bool ReadFile(
IntPtr hFile,
System.Text.StringBuilder lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
IntPtr lpOverlapped
);

t-xguo@prcvap.microsoft.com
04-11-2007, 11:13 AM
ã

ض⣬Ȳοӣ
http://support.microsoft.com/kb/190351/zh-cn
http://www.codeguru.com/Cpp/misc/misc/article.php/c277



߼ֹ֧ʦ
΢ȫ֧
---------------------------------------------------------------------------------------
ǵķʱ䣺һ9:00-18:00ڼճ⣩ǽգ48СʱṩʼӦһо⡣΢鼼֧Ϣʣhttp://support.microsoft.com/gp/newsgroupsupport/zh-cn.

ʱĶʹáظ(Reply to Group)⽫ûл
---------------------------------------------------------------------------------------
ԡ״ṩûκεͬʱҲûκȨ

t-xguo@prcvap.microsoft.com
04-13-2007, 03:26 AM
ã
еĹSTARTUPINFOĸֵЩӦ
si.hStdOutput = hread;
si.hStdError = hread;
si.hStdInput = hwrite;
ڴ⣬Ƿʣκʣӭ΢ۡ


߼ֹ֧ʦ
΢ȫ֧
---------------------------------------------------------------------------------------
ǵķʱ䣺һ9:00-18:00ڼճ⣩ǽգ48СʱṩʼӦһо⡣΢鼼֧Ϣʣhttp://support.microsoft.com/gp/newsgroupsupport/zh-cn.

ʱĶʹáظ(Reply to Group)⽫ûл
---------------------------------------------------------------------------------------
ԡ״ṩûκεͬʱҲûκȨ

老土
04-13-2007, 10:08 AM
在.net中的重定向子进程的控制台输出,不用这么麻烦的。其实像下面这样几行就可以了。
private void button3_Click_2(object sender, System.EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "d:\\windows\\system32\\cmd.exe";
psi.Arguments = " /c dir /?";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;

Process pro = Process.Start(psi);
this.textBox1.Text += pro.StandardOutput.ReadToEnd();
pro.WaitForExit();
}

但原来的程序为什么不行,我还是不懂。还是很谢谢你的回答。我自己绕了个大弯,又回到了.net,呵呵

“t-xguo@prcvap.microsoft.com”编写:

> 您好:
> 代码中的关于STARTUPINFO的赋值有些错误,应该这样:
> si.hStdOutput = hread;
> si.hStdError = hread;
> si.hStdInput = hwrite;
> 关于此主题,您是否还有疑问?如有任何疑问,欢迎在微软中文新闻组讨论。
>
> 郭轩
> 在线技术支持工程师
> 微软全球技术支持中心
> ---------------------------------------------------------------------------------------
> 我们的服务时间:周一至周五9:00-18:00(节假日除外)。我们将在两个工作日(48小时)内提供初始回应,并和您一起研究并解决问题。更多微软新闻组技术支持信息,请访问:http://support.microsoft.com/gp/newsgroupsupport/zh-cn.
>
> 回帖时,请在您的新闻组阅读器中使用“回复组(Reply to Group)”,这将帮助其他用户从您的提问中获益
> ---------------------------------------------------------------------------------------
> 本贴子以”现状”提供且没有任何担保,同时也没有授予任何权利。