与GetNamedPipeClientComputerName的C#互操作

问题描述 投票:0回答:1

我正在尝试使用互操作调用来获取C#中命名管道客户端的进程ID和计算机名称:

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetNamedPipeClientProcessId(IntPtr Pipe, out uint ClientProcessId);

private static uint GetClientProcessID(NamedPipeServerStream pipeServer)
{
    uint processId;
    IntPtr pipeHandle = pipeServer.SafePipeHandle.DangerousGetHandle();
    if (GetNamedPipeClientProcessId(pipeHandle, out processId))
    {
        return processId;
    }
    return 0;
}

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetNamedPipeClientComputerName(IntPtr Pipe, out string ClientComputerName, uint ClientComputerNameLength);

private static string GetClientComputerName(NamedPipeServerStream pipeServer)
{
    string computerName;
    uint buffer = 32768;
    IntPtr pipeHandle = pipeServer.SafePipeHandle.DangerousGetHandle();
    if (GetNamedPipeClientComputerName(pipeHandle, out computerName, buffer))
    {
        return computerName;
    }
    return null;
}

GetNamedPipeClientProcessId调用正在工作,但是GetNamedPipeClientComputerName返回false。是什么导致那个失败?

c# interop named-pipes
1个回答
1
投票

您应该使用StringBuilder而不是String

© www.soinside.com 2019 - 2024. All rights reserved.