老土
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
);
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
);