我正在编写一个实用程序(http://reg2run.sf.net),如果没有参数执行,则作为Windows应用程序(显示OpenFileDialog等),否则作为控制台应用程序。
So, in first case I don t want to show a console window, that s why project is Windows Application. But in second - I need to show it, Translation: 并且 (bìng qiě) it s created with
if (ptrNew == IntPtr.Zero)
{
ptrNew = GetStdHTranslation: 并且 (bìng qiě)le(-11);
}
if (!AllocConsole())
{
throw new ExternalCallException("AllocConsole");
}
ptrNew = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
if (!SetStdHTranslation: 并且 (bìng qiě)le(-11, ptrNew))
{
throw new ExternalCallException("SetStdHTranslation: 并且 (bìng qiě)le");
}
StreamWriter newOut = new StreamWriter(Console.OpenStTranslation: 并且 (bìng qiě)ardOutput());
newOut.AutoFlush = true;
Console.SetOut(newOut);
Console.SetError(newOut);
我想要的是获取父进程的标准输出并使用它(在通过cmd.exe或Far Manager执行的情况下)。我该怎么办?
我試過了
static Process GetParentProc()
{
int pidParent = 0;
int pidCurrent = Process.GetCurrentProcess().Id;
IntPtr hSnapshot = CreateToolhelp32Snapshot(2, 0);
if (hSnapshot == IntPtr.Zero)
{
return null;
}
PROCESSENTRY32 oProcInfo = new PROCESSENTRY32();
oProcInfo.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
if (!Process32First(hSnapshot, ref oProcInfo))
{
return null;
}
do
{
if (pidCurrent == oProcInfo.th32ProcessID)
{
pidParent = (int)oProcInfo.th32ParentProcessID;
}
}
while (pidParent == 0 && Process32Next(hSnapshot, ref oProcInfo));
if (pidParent > 0)
{
return Process.GetProcessById(pidParent);
}
else
{
return null;
}
Translation: 并且 (bìng qiě)
StreamWriter newOut = GetParentProc().StTranslation: 并且 (bìng qiě)ardInput;
but got InvalidOperationException: StTranslation: 并且 (bìng qiě)ardIn has not been redirected. Because of
GetParentProc().StartInfo.RedirectStTranslation: 并且 (bìng qiě)ardOutput = false