c# - c++/c# 命名管道中的问题

标签 c# c++

这是我的c++代码

HANDLE hPipe = ::CreateNamedPipe(_T("\\\\.\\pipe\\FirstPipe"),
                                 PIPE_ACCESS_DUPLEX,
                                 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
                                 PIPE_UNLIMITED_INSTANCES,
                                 4096,
                                 4096,
                                 0,
                                 NULL);

ConnectNamedPipe(hPipe, NULL);

DWORD bytesWritten = 0;
WriteFile(hPipe, lpBuffers, sizeof(LPWSABUF), &bytesWritten, NULL);//LPWSABUF is structure and lpBuffers is a variable of this structure

这是我的C#代码

uint dataLen = (uint)(br.ReadInt32());
string len = (dataLen).ToString();
listBox1.Items.Add(len);                        
IntPtr dataAdd = IntPtr.Zero;
string data = "";
if (IntPtr.Size == 4) dataAdd = (IntPtr)br.ReadInt32(); //ERROR
else dataAdd = (IntPtr)br.ReadInt64();
byte[] b = new byte[(int)dataLen];
Marshal.Copy(b, 0, dataAdd, (int)dataLen);
data = Encoding.Unicode.GetString(b);
listBox2.Items.Add(data);

在 C# 代码的第六行给出错误。 流的那端。我不知道为什么会出错。

结构如下

typedef struct _WSABUF {
    ULONG len;     /* the length of the buffer */
    __field_bcount(len) CHAR FAR *buf; /* the pointer to the buffer */
}  WSABUF, FAR * LPWSABUF;

最佳答案

LPWSABUF 是指针,大小为 32 或 64 位。可能你的意思是:

WriteFile(hPipe, lpBuffers, sizeof(WSABUF), &bytesWritten, NULL);

关于c# - c++/c# 命名管道中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4245644/

相关文章:

c# - 如何按字母分组并放入相同的字母大小

c# - Request.QueryString 找不到但有值

c# - 无法在 ABP 中的 Hangfire 循环作业上访问处置对象(在 DbContext 上)错误

c++ - 为什么类中不允许函数模板特化?

c++ - C++中多个容器的自定义迭代器

c++ - 使用预处理器打印 C++ 类中的默认函数

c# - throttle 事件处理程序

c# - 如何在 C# 中以编程方式居中一堆控件

c++ - 我正在寻找使用相同代码定义两个具有不同名称的库

c++ - 在 C++ 中计算大阶乘