简体   繁体   中英

Pipes doesn't work in WinApi on Windows 7 but works on Windows 10

I made a server and a client in winapi.

Client sends an number and a base and the server returns the number in that base. Windows7错误 My problem that it works in Windows 10, but it doesn't work in Windows 7 and I don't understand why. Some help?

Client:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <windows.h>
#include <string>

#define BUFFSIZE 512
using namespace std;


int main()
{
    LPDWORD bytesRead = 0;
    char res[50];
    int num, base;

    LPCTSTR Roura = TEXT("\\\\.\\pipe\\pipeline");
    HANDLE h = CreateFile(Roura, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);


    while (true) {
        for (int i = 0; i < 50; i++) {
            res[i] = 0;
        }
        printf("Number: ");
        cin >> num;
        WriteFile(h, &num, sizeof(int), NULL, NULL);
        if (num == 0) {
            CloseHandle(h);
            return 0;
        }
        printf("Base: ");
        cin >> base;
        WriteFile(h, &base, sizeof(int), NULL, NULL);
        ReadFile(h, res, BUFFSIZE, bytesRead, NULL);
        cout << res << endl;
    }

    return 0;
}

Server:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <windows.h>
#include <string>

#define _CRT_SECURE_NO_WARNINGS
#define BUFFSIZE 512
using namespace std;


int main()
{
    int num, base;
    LPDWORD bytesRead = 0;
    char result[50];
    char end[] = {"\0"};

    LPCTSTR Roura = TEXT("\\\\.\\pipe\\pipeline");

    HANDLE h = CreateNamedPipe(Roura, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, BUFFSIZE, BUFFSIZE, 0, NULL);


    assert(h != INVALID_HANDLE_VALUE);

    if (!ConnectNamedPipe(h, NULL)) return -1;

    while (true) {
        ReadFile(h, &num, BUFFSIZE, bytesRead, NULL);
        if (num == 0) {
            CloseHandle(h);
            return 0;
        }
        ReadFile(h, &base, BUFFSIZE, bytesRead, NULL);
        _itoa(num, result, base);
        WriteFile(h, result, strlen(result), NULL, NULL);
    }

    return 0;
}
#define BUFFSIZE 512
int num, base;
LPDWORD bytesRead = 0;
ReadFile(h, &num, BUFFSIZE, bytesRead, NULL);

this code complete of errors. need use

int num, base;
DWORD bytesRead;
ReadFile(h, &num, sizeof(num), &bytesRead, NULL);

instead. crash faster of all by bytesRead== 0

when

If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.

however this true on win7, but on say win10 - system let have lpNumberOfBytesRead == 0 - so no crash here

also this

WriteFile(h, &num, sizeof(int), NULL, NULL);

again - here already only 1 error compare ReadFile call

If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.

why ?

that it works in Windows 10, but it doesn't work in Windows 7

this is because begin from win 8.1 ( if I not mistake ) code of ReadFile / WriteFile check lpNumberOfBytes parameter and if it ==0 not assign to it actual number of bytes read or written. but on windows 7 system not do this check and unconditionally write data by 0 address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM