编辑:这是完整的代码,忽略罗马尼亚注释。罗马尼亚语也未翻译2或3个名称:http://pastebin.com/JjtayvXX
我正在尝试学习OS的基础知识,现在我正在使用Windows下的命名管道,但我不知道出了什么问题。
老实说,我正在做一个朋友做的榜样,但他和我一样糟糕,即使不是更糟。虽然hi的程序可以运行(尽管它可以执行其他操作),但他无法解释任何内容,很可能只是从某个地方复制而来,仍然...不重要,我想说的是我从示例中学到的,而不是专业的。
服务器从客户端收到一条消息,并返回最大和最小数字。
Server.c:
#include "windows.h"
#include "stdio.h"
struct Msg {
int numbers[20];
int length;
};
...
int main () {
HANDLE inputPipe, outputPipe;
Msg msg;
while (true) {
inputPipe = CreateNamedPipe ("\\\\.\\pipe\\Client2Server",
PIPE_ACCESS_INBOUND,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
0, //Numb of output bytes
sizeof(Msg), // Numb of input bytes
0, // Wait forever
NULL); // Don't know how to use security
ConnectNamedPipe (inputPipe,NULL);
// Here is where the server dies
ReadFile (inputPipe, &msg,sizeof(Msg),NULL,NULL);
现在Client.c:
struct Msg {
int numbers[20];
int length;
};
int main () {
HANDLE outputPipe, inputPipe;
Msg msg;
// @misc: read data from keyboard, create msg
outputPipe = CreateFile ("\\\\.\\pipe\\Client2Server",
GENERIC_WRITE,
FILE_SHARE_READ, // * comment after code
NULL, // again, I know nothing about security attributes
CREATE_ALWAYS, // either create or overwrite
0,
NULL);
// Here is where it dies
WriteFile (outputPipe, &msg, sizeof(Msg), NULL, NULL);
我收到访问冲突写入位置0x00000000。不知道为什么。
我希望该进程仅写入,而另一个进程(服务器)仅读取。
FILE_SHARE_READ
可以吗?另外我也不知道如何弄乱CreationDisposition / FlagsAndAttributes(在
CreateFile
处的最后2个参数),它们还好吗? 最佳答案
编辑:添加了实际答案,参考其他主题,我自己尝试过WriteFile()'s
第四个参数(指向将存储字节数的变量的指针)不应为null。根据API描述,如果第五个参数lpOverlapped
不为null,则此参数只能为NULL。
在此处查看类似的主题:
Why does WriteFile crash when writing to the standard output?
您可以检查/打印ReadFile()
的返回值(如果return = 0
或FALSE
失败)和client.c CreateFile()
(如果返回INVALID_HANDLE_VALUE
失败)以查看它们是否成功?
如果失败,可以在调用后立即打印GetLastError()
返回的值,以便我们可以看到特定的错误吗?
关于c - 我的第一个Windows命名为pipe,不确定出什么问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13552033/