Domanda

For a few days I've been trying to get a test-program running to send data from a 64-bit application to a 32-bit application via Named Pipes. On my Windows 8.1 laptop (64bit) the test-program in the code below runs perfectly fine. When I try to run the exact same code on a Windows 7 (64bit) desktop computer, the server side gets the pop-up 'pwrite.exe has stopped working'. This happens when I either compile it and run it on the desktop or when I run the executables (compiled on the laptop) on the desktop computer (just by copying the two .exe files).

The strange thing is that the client side receives the value send via the named pipe in the first WriteFile operation, but it will not get the value from the second operation in the for-loop. So after the client side receives the correct value and displays it in the console window, the server side gets stuck. It could be that for some reason I don't have permission to write and the system realizes this after one operation, but I would expect an error related to permission, which is not the case.

To give you a better idea:

  • Both laptop and desktop computer accounts are running as Administrator. I tried to set the security level to the lowest level in the Control Panel.

  • I disabled the Windows Firewall on the desktop computer

  • 'netstat -a' in the command window on both computers give under port 445, 135 and 137 'LISTENING', which I understood are the ports often used for Named Pipes.

  • I'm using Visual Studio 2012

  • Either the client and server side run independently without this problem occurring

  • Desktop is running at 3.4GHz, laptop is running at 1.8 GHz

  • Desktop has .NET framework 4.0, laptop has 4.5

I think the solution must either be found in security settings or a problem with a-synchronization, so that is the direction of my search for days, but without success.

Server:

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sstream>  
#include <string>
#include "globals.h"

using namespace std;

#define  THE_PIPE "\\\\.\\pipe\\testpipe"
int main()
{
HANDLE pipe = CreateFile(THE_PIPE, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
double i = 0;
boolean r = true;

while (r == true) {
std::ostringstream ostr; 
ostr << i+0.000010; 
std::string theNumberString = ostr.str(); 
string message = theNumberString;
WriteFile(pipe, message.c_str(), message.length() + 1, NULL, NULL); 
cout << GetLastError() << endl;
i++;    
    if (pipe == INVALID_HANDLE_VALUE)
{
    cout << "Error: " << GetLastError();
}
}

return 0;
}

Client:

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
#define  THE_PIPE "\\\\.\\pipe\\testpipe"

int main()
{
boolean t = true;
char key = '\0';

HANDLE pipe = CreateNamedPipe(THE_PIPE, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,      PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL);  //FILE_SHARE_READ
while (1) {

    if (_kbhit()) key = _getch();
    if (key == 'q') break;

if (pipe == INVALID_HANDLE_VALUE)
{
    cout << "Error: " << GetLastError();
}

char data[1024];
DWORD numRead;

ConnectNamedPipe(pipe, NULL);
ReadFile(pipe, data, 1024, &numRead, NULL);

if (numRead > 0){
    cout << data << endl;
}
}
CloseHandle(pipe);
}
È stato utile?

Soluzione

I can see a few things wrong here.

You open up the pipe with FILE_FLAG_OVERLAPPED. You need to provide an OVERLAPPED structure as the last parameter to WriteFile, but you have passed NULL.

See the WriteFile documentation. Now because of this, you may well be getting an error code on your 2nd attempt to write of ERROR_IO_PENDING.

My suggestion is to restructure your server code so that you're either correctly working with the overlapped paradigm, or turn off the overlap flag (meaning your lbNumberofBytesWritten parameter will have to be non-NULL).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top