我正在尝试使用互操作调用来获取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。是什么导致那个失败?
您应该使用StringBuilder
而不是String
: