请教C#进程间匿名管道的使用

NIJIA72 2010-12-23 10:02:46
本人长期开发WEB项目, 对winodws编程不是太灵光, 最近要做这么一个东西,实在吃力, 请懂的朋友帮帮忙

一个控制台程序, 创建一个进程, 打开一个子程序,
子程序会往匿名管道内写内容,主程序读匿名管道内容输出

我现在的有的DEMO如下


//子程序
using System;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
static void Main(string[] args)
{
if (args.Length > 0)
{
using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0]))
{

Console.WriteLine("Client Current TransmissionMode: {0}.",
pipeClient.TransmissionMode);

// Anonymous Pipes do not support Message mode.
try
{
Console.WriteLine("Client Setting ReadMode to \"Byte\".");
pipeClient.ReadMode = PipeTransmissionMode.Byte;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}

using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("client receive:" + temp);
}
}
}
}
Console.Write("Press Enter to continue...");
Console.ReadLine();
}
}


//----------------------------------------------------------------------------------------


//主程序
using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
static void Main()
{
Process pipeClient = new Process();
pipeClient.StartInfo.FileName = "PipeClient.exe";

using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
{
Console.WriteLine("Server Current TransmissionMode: {0}.",
pipeServer.TransmissionMode);

// Anonymous pipes do not support Message mode.
try
{
Console.WriteLine("Server Setting ReadMode to \"Byte\".");
pipeServer.ReadMode = PipeTransmissionMode.Byte;
}
catch (NotSupportedException e)
{
Console.WriteLine("EXCEPTION: {0}", e.Message);
}

// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
pipeClient.StartInfo.UseShellExecute = false;
pipeClient.Start();

pipeServer.DisposeLocalCopyOfClientHandle();

try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.WriteLine("Enter text: ");
sw.WriteLine(Console.ReadLine());

}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}

pipeClient.WaitForExit();
pipeClient.Close();
}
}




问题一, 为什么主程序打开子程序, 子程序没窗口
问题二, 以上是主程打开子程序, 传给子程序句柄, 但我的需求是子程序是别人写的, 人家没有接收句柄的代码
只能主程序打开子程序时, 得到子程序的句柄,
比如 IntPtr intptr = pipeClient.Handle ;
如何用这个打开子程序时得到的子程序句柄 IntPtr, 在主程序内得到子程序的管道信息呢

DEMO, 连接文章, 都可以, 请大家教我
...全文
184 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
NIJIA72 2010-12-24
  • 打赏
  • 举报
回复
顶一下, 有没有人教我

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧