C# 以编程方式获取命名管道的系统名称
我正在使用WCF NetNamedPipeBinding编写进程间通信 我的目标是让服务在“net”上运行。pipe://localhost/service,因此我运行最简单的主机:C# 以编程方式获取命名管道的系统名称,c#,.net,wcf,wcf-binding,named-pipes,C#,.net,Wcf,Wcf Binding,Named Pipes,我正在使用WCF NetNamedPipeBinding编写进程间通信 我的目标是让服务在“net”上运行。pipe://localhost/service,因此我运行最简单的主机: host = new ServiceHost(contract, new Uri[] { "net.pipe://localhost" }); host.AddServiceEndpoint(typeof (IContract), new NetNamedPipeBinding(), "service");
host = new ServiceHost(contract, new Uri[] { "net.pipe://localhost" });
host.AddServiceEndpoint(typeof (IContract),
new NetNamedPipeBinding(), "service");
host.Open();
据
该名称隐藏在系统生成的guid后面
问题来了
是否有任何可能的方法可以在我的程序中获取系统生成的名称(guid),这样我就可以像procexp一样获取“\Device\NamedPipe\guid”这样的路径,以便更容易地嗅探它?
(除了在单独的进程中运行sys internal可执行文件并解析其输出之外?正如您链接到的文章所示,WCF将命名管道名称存储在内存映射文件中。要访问它,您需要执行以下操作:
- 端点:net。pipe://localhost/TradeService/Service1
- 规范化端点:net.pipe://++/TRADESERVICE/SERVICE1/
- 基本64表示法:BMV0LNBPCGU6LY8RL1RSKURFU0VSVKLDRS9TRVJWSUNFMS8=
- 最终内存映射文件:net.pipe:EbmV0LnBpcGU6Ly8rL1RSQURFU0VSVklDRS9TRVJWSUNFMS8=
//打开MMF。
使用(var mmf=MemoryMappedFile.OpenExisting(namedPipeMMFName))
{
//从开始创建16字节(GUID大小)的访问器
//抵销5(如本条所述)
使用(var访问器=mmf.CreateViewAccessor(5,16))
{
Guid管道Guid;
读取(0,out pipeGuid);
WriteLine(“这应该是管道名称:“+pipeGuid”);
}
}
在做了大量的摆弄和把头撞在墙上之后,我终于成功了:
Guid pipeGuid;
if (PipeName.Equals("*", StringComparison.InvariantCultureIgnoreCase) || PipeName.Equals("localhost", StringComparison.InvariantCultureIgnoreCase))
PipeName = "*";
string s = string.Format(@"net.pipe://{0}/", PipeName.ToUpper());
if(!string.IsNullOrWhiteSpace(ServiceName))
s = string.Format(@"net.pipe://*/{0}/", ServiceName.ToUpper());
var bytes = Encoding.UTF8.GetBytes(s);
var base64 = Convert.ToBase64String(bytes);
string namedPipeMMFName = string.Format(@"Global\net.pipe:E{0}", base64);
MemoryMappedFileSecurity mSec = new MemoryMappedFileSecurity();
mSec.AddAccessRule(new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow));
using (var mmf = MemoryMappedFile.OpenExisting(namedPipeMMFName, MemoryMappedFileRights.Read))
{
using (var accessor = mmf.CreateViewAccessor(4, 45, MemoryMappedFileAccess.Read))
{
accessor.Read<Guid>(0, out pipeGuid);
}
}
using (NamedPipeClientStream client = new NamedPipeClientStream(GetResolvedText(ServerName), pipeGuid, PipeDirection.InOut,
PipeOptions.None, TokenImpersonationLevel.Impersonation))
{
client.Connect(10000);
}
Guid-pipeGuid;
if(PipeName.Equals(“*”,StringComparison.InvariantCultureInogoreCase)| | PipeName.Equals(“localhost”,StringComparison.InvariantCultureInogoreCase))
PipeName=“*”;
string s=string.Format(@“net.pipe://{0}/”,PipeName.ToUpper());
如果(!string.IsNullOrWhiteSpace(ServiceName))
s=string.Format(@“net.pipe://*/{0}/”,ServiceName.ToUpper());
var bytes=Encoding.UTF8.GetBytes(s);
var base64=Convert.tobase64字符串(字节);
string namedPipeMMFName=string.Format(@“Global\net.pipe:E{0}”,base64);
MemoryMappedFileSecurity mSec=新的MemoryMappedFileSecurity();
mSec.AddAccessRule(新的AccessRule(新的SecurityIdentifier(WellKnownSidType.WorldSid,null)、MemoryMappedFilers.FullControl、AccessControlType.Allow));
使用(var mmf=MemoryMappedFile.OpenExisting(namedPipeMMFName,memorymappedfiles.Read))
{
使用(var accessor=mmf.CreateViewAccessor(4,45,MemoryMappedFileAccess.Read))
{
读取(0,out pipeGuid);
}
}
使用(NamedPipeClientStream client=new NamedPipeClientStream(GetResolvedText(ServerName))、pipeGuid、PipeDirection.InOut、,
PipeOptions.None,TokenImpersonationLevel.Impersonation)
{
客户端连接(10000);
}
我必须感谢罗德尼·维亚纳(Rodney Viana)和@Avner Shahar Kashtan(Avner Shahar Kashtan)的回答以及我读到的许多其他文章。我希望我的回答能对将来的人有所帮助。谢谢。我没有想到这件事。此解决方案有效,标记为有效解决方案。此处唯一要做的更改是mmf.CreateViewAccessor(4,45)。我尝试了5,16,但一直得到一个错误的指导。这太棒了——在我看来,比公认的答案要好:)。尽管公认的答案确实解释了为什么这是可行的。
Guid pipeGuid;
if (PipeName.Equals("*", StringComparison.InvariantCultureIgnoreCase) || PipeName.Equals("localhost", StringComparison.InvariantCultureIgnoreCase))
PipeName = "*";
string s = string.Format(@"net.pipe://{0}/", PipeName.ToUpper());
if(!string.IsNullOrWhiteSpace(ServiceName))
s = string.Format(@"net.pipe://*/{0}/", ServiceName.ToUpper());
var bytes = Encoding.UTF8.GetBytes(s);
var base64 = Convert.ToBase64String(bytes);
string namedPipeMMFName = string.Format(@"Global\net.pipe:E{0}", base64);
MemoryMappedFileSecurity mSec = new MemoryMappedFileSecurity();
mSec.AddAccessRule(new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow));
using (var mmf = MemoryMappedFile.OpenExisting(namedPipeMMFName, MemoryMappedFileRights.Read))
{
using (var accessor = mmf.CreateViewAccessor(4, 45, MemoryMappedFileAccess.Read))
{
accessor.Read<Guid>(0, out pipeGuid);
}
}
using (NamedPipeClientStream client = new NamedPipeClientStream(GetResolvedText(ServerName), pipeGuid, PipeDirection.InOut,
PipeOptions.None, TokenImpersonationLevel.Impersonation))
{
client.Connect(10000);
}