internal static IpcPort Connect(String portName, bool secure, TokenImpersonationLevel impersonationLevel, int timeout) { string pipeName = prefix + portName; uint impersonation = NativePipe.SECURITY_SQOS_PRESENT; // convert the impersonation Level to the correct flag if (secure) { switch (impersonationLevel) { case TokenImpersonationLevel.None: impersonation = NativePipe.SECURITY_SQOS_PRESENT; break; case TokenImpersonationLevel.Identification: impersonation = NativePipe.SECURITY_SQOS_PRESENT | NativePipe.SECURITY_IDENTIFICATION; break; case TokenImpersonationLevel.Impersonation: impersonation = NativePipe.SECURITY_SQOS_PRESENT | NativePipe.SECURITY_IMPERSONATION; break; case TokenImpersonationLevel.Delegation: impersonation = NativePipe.SECURITY_SQOS_PRESENT | NativePipe.SECURITY_DELEGATION; break; } } while (true) { // Invoke CreateFile with the pipeName to open a client side connection PipeHandle handle = NativePipe.CreateFile(pipeName, NativePipe.GENERIC_READ | NativePipe.GENERIC_WRITE, NativePipe.FILE_SHARE_READ | NativePipe.FILE_SHARE_WRITE, IntPtr.Zero, NativePipe.OPEN_EXISTING, NativePipe.FILE_ATTRIBUTE_NORMAL | NativePipe.FILE_FLAG_OVERLAPPED | impersonation, IntPtr.Zero); if (handle.Handle.ToInt32() != NativePipe.INVALID_HANDLE_VALUE) { return(new IpcPort(portName, handle)); } int error = Marshal.GetLastWin32Error(); if (error != NativePipe.ERROR_PIPE_BUSY) { throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ConnectIpcFailed"), GetMessage(error))); } if (!NativePipe.WaitNamedPipe(pipeName, timeout)) { throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_Busy"), GetMessage(error))); } } }
internal static IpcPort Connect(string portName, bool secure, TokenImpersonationLevel impersonationLevel, int timeout) { string lpFileName = @"\\.\pipe\" + portName; uint num = 0x100000; if (secure) { switch (impersonationLevel) { case TokenImpersonationLevel.None: num = 0x100000; break; case TokenImpersonationLevel.Identification: num = 0x110000; break; case TokenImpersonationLevel.Impersonation: num = 0x120000; break; case TokenImpersonationLevel.Delegation: num = 0x130000; break; } } while (true) { PipeHandle handle = NativePipe.CreateFile(lpFileName, 0xc0000000, 3, IntPtr.Zero, 3, 0x40000080 | num, IntPtr.Zero); if (handle.Handle.ToInt32() != -1) { return(new IpcPort(portName, handle)); } int errorCode = Marshal.GetLastWin32Error(); if (errorCode != 0xe7L) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_ConnectIpcFailed"), new object[] { GetMessage(errorCode) })); } if (!NativePipe.WaitNamedPipe(lpFileName, timeout)) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_Busy"), new object[] { GetMessage(errorCode) })); } } }