Я сделал сервер и клиент в winapi.Трубы не работают в WinApi на Windows 7, но работают на Windows 10
Клиент отправляет номер и базу, и сервер возвращает номер в этой базе. Моя проблема в том, что он работает в Windows 10, но он не работает в Windows 7, и я не понимаю, почему. Некоторая помощь?
Клиент:
#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;
}
Сервер:
#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;
}
Где ваша [обработка ошибок] (http://stackoverflow.com/documentation/winapi/2573/error-reporting-and-handling#t = +201612112131423457057)? – IInspectable
Начните с проверки на наличие ошибок. Все ли «ReadFile» и «WriteFile» преуспевают? Вы не знаете. И если кто-то терпит неудачу, вызовите 'GetLastError', чтобы узнать, почему. –
@CareyGregory Я не могу понять, почему он работает в Windows10 и Windows7 падает. –