简体   繁体   English

我的第一个Windows名为Pipe,不确定出什么问题

[英]My first windows named pipe, not sure what is wrong

Edit: Here is the entire code, ignore Romanian comments. 编辑:这是完整的代码,忽略罗马尼亚注释。 Also 2 or 3 names are untranslated from Romanian: http://pastebin.com/JjtayvXX 罗马尼亚语也未翻译2或3个名称: http : //pastebin.com/JjtayvXX

I am trying to learn the basics of OS, now I'm working with named pipes under windows and I can't tell what's wrong. 我正在尝试学习OS的基础知识,现在我正在使用Windows下的命名管道,但我不知道出了什么问题。

Honestly I'm working off an example a friend did, but he's just as bad as me if not worse. 老实说,我正在做一个朋友的榜样,但他和我一样糟糕,甚至还不至于更糟。 While hi's program works (albeit it does something else), he can't explain anything, most likely just copied from somewhere, still ... not important, what I was trying to say I'm learning from examples, and not professional ones. 虽然hi的程序可以运行(尽管它可以执行其他操作),但他无法解释任何内容,很可能只是从某个地方复制而来,仍然...不重要,我想说的是我从示例中学到的,而不是专业的。

Server receives a message from the client, returns max and min numbers. 服务器从客户端收到一条消息,并返回最大和最小数字。

Server.c: 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); 

Now Client.c: 现在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);

I get Access violation writing location 0x00000000. 我收到访问冲突写入位置0x00000000。 No idea why. 不知道为什么。

  • I would like that this process only writes, and another process (server) only reads. 我希望该进程仅写入,而另一个进程(服务器)仅读取。 Is FILE_SHARE_READ OK ? FILE_SHARE_READ是否可以?

Also I don't know how to mess with CreationDisposition / FlagsAndAttributes (last 2 parameters at CreateFile ), are they OK ? 另外我也不知道该如何搞乱CreationDisposition / FlagsAndAttributes( CreateFile最后两个参数),它们还好吗?

Edit: Added actual answer, reference to other topic, tried it myself 编辑:添加了实际答案,引用了其他主题,我自己尝试过

WriteFile()'s fourth parameter (pointer to variable that will store number of bytes) should not be null. WriteFile()'s第四个参数(指向将存储字节数的变量的指针)不应为null。 Based on the API description, this parameter can ONLY be NULL if the fifth param, lpOverlapped , is NOT null. 根据API描述,如果第五个参数lpOverlapped不为null,则此参数只能为NULL。

See similar topic here: Why does WriteFile crash when writing to the standard output? 在这里看到类似的主题: 写入标准输出时,为什么WriteFile崩溃?


Can you check/printf the return values of ReadFile() (failed if return = 0 or FALSE ) and client.c CreateFile() (failed if returns INVALID_HANDLE_VALUE ) to see if they succeed? 您可以检查/打印ReadFile()的返回值(如果return = 0FALSE失败)和client.c CreateFile() (如果返回INVALID_HANDLE_VALUE失败)以查看它们是否成功?

If failed, can you print the value returned by GetLastError() immediately after the call so that we can see the specific error? 如果失败,可以在调用后立即打印GetLastError()返回的值,以便我们可以看到特定的错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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