Kalec
Kalec

Reputation: 2881

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

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.

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.

Server receives a message from the client, returns max and min numbers.

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:

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. No idea why.

Also I don't know how to mess with CreationDisposition / FlagsAndAttributes (last 2 parameters at CreateFile), are they OK ?

Upvotes: 0

Views: 1594

Answers (1)

Ronaldo Nazarea
Ronaldo Nazarea

Reputation: 366

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. Based on the API description, this parameter can ONLY be NULL if the fifth param, lpOverlapped, is NOT null.

See similar topic here: Why does WriteFile crash when writing to the standard output?


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?

If failed, can you print the value returned by GetLastError() immediately after the call so that we can see the specific error?

Upvotes: 1

Related Questions