β

Named pipe client throw UnauthorizedAccessExceptio

User zangw - Stack Overflow 372 阅读

I am trying to write named pipe client with c#, but I get UnauthorizedAccessException when SetAccessControl() is invoked.

{System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.
   at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)
   at System.Security.AccessControl.NativeObjectSecurity.Persist(SafeHandle handle, AccessControlSections includeSections)
   at System.IO.Pipes.PipeSecurity.Persist(SafeHandle handle)
   at NamePipeEx.Program.Main(String[] args) in c:\Users\zangw\Documents\Visual Studio 2013\Projects\NamePipeEx\NamePipeEx\Program.cs:line 27
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()}

and here are my codes

    PipeAccessRule pr = new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
    PipeSecurity ps = new PipeSecurity();
    ps.AddAccessRule(pr);
    var client = new NamedPipeClientStream(".", "logpipe", PipeDirection.In, PipeOptions.WriteThrough);
    // connect to the server
    client.Connect();
    client.SetAccessControl(ps);
    client.ReadMode = PipeTransmissionMode.Message;

Please help me to figure out the failure reason of my codes.

Edit add named pipe server code in C++

HANDLE hPipe;
LPTSTR lpPipeName = TEXT("\\\\.\\pipe\\logpipe");
// try to open a named pipe
hPipe = CreateNamedPipe(lpPipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT, 
                        PIPE_UNLIMITED_INSTANCES, 2048, 2048, NMPWAIT_USE_DEFAULT_WAIT, NULL);
if (hPipe == INVALID_HANDLE_VALUE){
    printf("Pipe handler error");
    //return -1;
}
if (!ConnectNamedPipe(hPipe, NULL)){
    if (GetLastError() != ERROR_PIPE_CONNECTED){
        printf("FAILED to connect ");
    }
}
printf("Client named pipe connected...");
// send message to named pipe
LPTSTR msg = TEXT("Default message from server...\r\n");
DWORD cbToWrite = lstrlen(msg) + 1;
while (true){
    DWORD cbWritten = 0;
    bool fSuccess = WriteFile(hPipe, msg, cbToWrite, &cbWritten, NULL);
    if (!fSuccess)
        printf("Failed to writefile pipe");
}
作者:User zangw - Stack Overflow
most recent 30 from stackoverflow.com
原文地址:Named pipe client throw UnauthorizedAccessExceptio, 感谢原作者分享。

发表评论