【问题标题】:Implement " named Pipe" [duplicate]实施“命名管道”[重复]
【发布时间】:2022-12-18 21:07:30
【问题描述】:

我想实现一个“命名管道”,用于在 C# 和 Python 进程之间交换双数组 有人知道吗?

我找到了一些代码,但我无法理解

【问题讨论】:

标签: python c#


【解决方案1】:
[DllImport("kernel32.dll")]
static extern unsafe int CreateNamedPipe(
    string lpName, uint dwOpenMode, uint dwPipeMode,
    uint nMaxInstances, int nOutBufferSize,
    int nInBufferSize, int nDefaultTimeOut,
    ref SECURITY_ATTRIBUTES lpSecurityAttributes
    );

[DllImport("kernel32.dll")]
static extern unsafe int ConnectNamedPipe(
    int hNamedPipe,
    ref OVERLAPPED lpOverlapped
    );

[DllImport("kernel32.dll")]
static extern unsafe int DisconnectNamedPipe(
    int hNamedPipe
    );

[DllImport("kernel32.dll")]
static extern unsafe int WriteFile(
    int hFile, void* lpBuffer,
    int nNumberOfBytesToWrite,
    int* lpNumberOfBytesWritten,
    ref OVERLAPPED lpOverlapped
    );

[DllImport("kernel32.dll")]
static extern unsafe int ReadFile(
    int hFile, void* lpBuffer,
    int nNumberOfBytesToRead,
    int* lpNumberOfBytesRead,
    ref OVERLAPPED lpOverlapped
    );

[DllImport("kernel32.dll")]
static extern unsafe int CloseHandle(
    int hObject
    );

A:

此方法用于使用 Windows 命名管道与单独的进程通信。如果你想在同一进程中的两个线程之间(或同一台机器上的两个进程之间)进行通信,你可以使用MemoryMappedFileP/InvokeCreateFileMappingMapViewOfFile

【讨论】:

  • 在 .net 框架中有一个完整的命名空间 System.IO.Pipes 用于处理命名管道,因此无需为此目的使用 C# 中的 p/invoke
猜你喜欢
  • 1970-01-01
  • 2017-08-18
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
相关资源
最近更新 更多