管道在 Windows 7 上的 WinApi 中不起作用,但在 Windows 10 上起作用
Pipes doesn't work in WinApi on Windows 7 but works on Windows 10
我用winapi做了一个服务器和一个客户端。
客户端发送一个号码和一个基地,服务器returns该基地中的号码。
我的问题是它在 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[] = {"[=13=]"};
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);
这段代码没有错误。需要使用
int num, base;
DWORD bytesRead;
ReadFile(h, &num, sizeof(num), &bytesRead, NULL);
相反。崩溃速度最快 bytesRead== 0
当
If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.
然而这在 win7 上是正确的,但在 win10 上 - 系统让 lpNumberOfBytesRead == 0
- 所以这里没有崩溃
还有这个
WriteFile(h, &num, sizeof(int), NULL, NULL);
再次 - 这里已经只有 1 个错误比较 ReadFile
调用
If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.
为什么?
that it works in Windows 10, but it doesn't work in Windows 7
这是因为从win 8.1开始(如果我没记错)ReadFile
/WriteFile
代码检查lpNumberOfBytes
参数,如果它 ==0
没有分配给它实际读取或写入的字节数。但在windows 7系统上不做这个检查并无条件地通过0地址写入数据
我用winapi做了一个服务器和一个客户端。
客户端发送一个号码和一个基地,服务器returns该基地中的号码。
客户:
#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[] = {"[=13=]"};
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);
这段代码没有错误。需要使用
int num, base;
DWORD bytesRead;
ReadFile(h, &num, sizeof(num), &bytesRead, NULL);
相反。崩溃速度最快 bytesRead== 0
当
If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.
然而这在 win7 上是正确的,但在 win10 上 - 系统让 lpNumberOfBytesRead == 0
- 所以这里没有崩溃
还有这个
WriteFile(h, &num, sizeof(int), NULL, NULL);
再次 - 这里已经只有 1 个错误比较 ReadFile
调用
If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL.
为什么?
that it works in Windows 10, but it doesn't work in Windows 7
这是因为从win 8.1开始(如果我没记错)ReadFile
/WriteFile
代码检查lpNumberOfBytes
参数,如果它 ==0
没有分配给它实际读取或写入的字节数。但在windows 7系统上不做这个检查并无条件地通过0地址写入数据