How to fix garbled text with using WriteFile on a pipe?

I think the key here is to make sure that your strings are null terminated and that you send the termination character as well. You shouldn't have to send the entire buffer if the communications is synchronous or if you set it up in PIPE_READMODE_MESSAGE ReadFile will return when either the specified number of bytes has been read or a write operation completes on the other end of the pipe. I believe that the "garbled" text is really garbage in the read buffer on the client side of the pipe and because you are not transmitting the string termination character, it is including this in the text sent to the message box.

Either clear your read buffer before sending or send the string termination character with the message and I think it will work without the overhead of sending a full buffer Here is sample client from MSDN. Note how the client sends exactly the number of characters in the message 1 (including the termination character) and receives into a fixed buffer of size 512. If you look at a server example you'll see the same pattern.

I think the key here is to make sure that your strings are null terminated and that you send the termination character as well. You shouldn't have to send the entire buffer if the communications is synchronous or if you set it up in PIPE_READMODE_MESSAGE. ReadFile will return when either the specified number of bytes has been read or a write operation completes on the other end of the pipe.

I believe that the "garbled" text is really garbage in the read buffer on the client side of the pipe and because you are not transmitting the string termination character, it is including this in the text sent to the message box. Either clear your read buffer before sending or send the string termination character with the message and I think it will work without the overhead of sending a full buffer. Here is sample client from MSDN.

Note how the client sends exactly the number of characters in the message + 1 (including the termination character) and receives into a fixed buffer of size 512. If you look at a server example, you'll see the same pattern.

Thanks! I was now having a problem with half the string being sent. I needed the (lstrlen(..)+1) * sizeof(TCHAR), as shown in the example.

– emostar Mar 15 '09 at 13:30 Ah, yes. Multibyte characters. I didn't even think of that.

– tvanfosson Mar 15 '09 at 13:42 good catch, I updated my answer fixing it with *sizeof(TCHAR) – Brian R. Bondy Mar 15 '09 at 17:49.

Some observations on the code you posted: You need to either 1) explicitly send the null terminated byte, or 2) append one to the data you read. Since you are reading 512 bytes, you should also be sending exactly 512 bytes. You can send variable length strings instead by first sending the size of the string, and then sending that many bytes.

That way when you read the data you will know how many bytes to read for the actual string. The problem with what you did will be seen as soon as you send 2 things over the pipe, and you read past what you really want in the first read. If you are only sending 1 thing over the pipe, you can keep your code, but send size() + 1 when you write to the pipe.

ReadFile / WriteFile were meant to send binary data, not necessarily strings. So you can make a function called ReadString and WriteString that implements my suggestion about reading/writing first the size then the actual string. Try something like this: Here is the code for creating the pipe, and writing to it: myPipe = CreateNamedPipe(L"\\\\.

\\pipe\\testpipe", PIPE_ACCESS_OUTBOUND, PIPE_NOWAIT, 10, 512, 512, 10, NULL); TCHAR title128; GetWindowText(foundHwnd, title, 128); WriteFile(myPipe, title, 128*sizeof(TCHAR), &wrote, NULL);// 0) MessageBox(NULL, buf, L"Got Data", MB_OK).

Thanks, I didn't realize pipes needed the length to be exact. Thought they would do it that stuff for me. It is working now!

– emostar Mar 15 '09 at 12:52.

Conference Message From: coach_page2000 To: Other User Yahoo Messenger Archive: (Conference Message) From: coach_page2000 To: Other User Date E. -PO-SNIP File: C:\Program Files\Microsoft Office\OFFICE11\1033\PUBWIZ\SNIPE. POCLocal time at decoding: Tuesday, February 19, 2008 10:02 Decoded By Super Yahoo Messenger Archive Decoder!

V 33.07 (c) Copyright Piravi. Com, Read About Menu www.piravi.com coach_page2000 (): boac˜kpa#e2000coach_qageþüüübïIch_page2Õ:0bocdjpa'|2000coach_sage000aïIch_pge2Õ:0bocc&jpa¿q2000coach_sage000ïYch_page2ó20bObclgpa� M2000coach_ºageøɬ9óZach_page20�

� Ž‚qch_pAfe201hoact_pafEžÅ� AOí§’ sAzù804Tdc*_pafEžÅ�

AOXG“ sAzù804«´ecopage;00cojû   (Logoin to game)  coach_page2000 (): boac@ùpa#e2000coach_qageþüüübïYch_page2ó20bOcT÷pa‡Å2000coach_ºage™s_6'Éach_page200cocã@pagd200Õiocj_ÉgeD 00coach_pafe200cobã@_page200Õiock°ÉgeD 00coach_pafe200coqch_pAfe201@oact_pafEò‚Æ� AOëj‘ sA9µ504� úbc,_pafEò‚Æ�

AOëj‘ sA9µ504´^bcopage;00cojû   (Logoin to game) Â.

You shouldn't have to send the entire buffer if the communications is synchronous or if you set it up in PIPE_READMODE_MESSAGE. ReadFile will return when either the specified number of bytes has been read or a write operation completes on the other end of the pipe. I believe that the "garbled" text is really garbage in the read buffer on the client side of the pipe and because you are not transmitting the string termination character, it is including this in the text sent to the message box.

Either clear your read buffer before sending or send the string termination character with the message and I think it will work without the overhead of sending a full buffer. Here is sample client from MSDN. Note how the client sends exactly the number of characters in the message + 1 (including the termination character) and receives into a fixed buffer of size 512.

If you look at a server example, you'll see the same pattern.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions