这是我的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/