Barun
Barun

Reputation: 1915

c++ / c# Problem in named pipe

This is my c++ code

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

This is my C# code

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);

In sixth line of C# code giving error. That End of the stream. I dont have any idea why it is giving error.

Here is the structure

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

Upvotes: 0

Views: 907

Answers (2)

user415789
user415789

Reputation:

simply you have reached end of stream so end of stream exception is thrown. If it's the first read command from file then your file is empty

Upvotes: 0

Alex F
Alex F

Reputation: 43311

LPWSABUF is pointer, its size is 32 or 64 bit. Possibly you mean this:

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

Upvotes: 1

Related Questions