0

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.

  • I would like that this process only writes, and another process (server) only reads. Is FILE_SHARE_READ OK ?

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

Kalec
  • 2,380
  • 6
  • 26
  • 42

1 Answers1

1

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?

Community
  • 1
  • 1
Ronaldo Nazarea
  • 346
  • 1
  • 7
  • I tried `try {WriteFile (outputPipe, &msg, sizeof(Msg), NULL, NULL);} catch(int e) { printf("%d, Error: %d", GetLastError, e); }` but it doesn't seem to work, still thinks exception wasn't handled. All I can say is I get an `Access violation writing location 0x00000000` within `KernelBase.dll` – Kalec Nov 25 '12 at 15:39
  • 1
    d'oh. `WriteFile()`'s fourth parameter is optional (can be null) ONLY if the fifth param, lpoverlapped, is NOT NULL. http://stackoverflow.com/questions/8195608/why-does-writefile-crash-when-writing-to-the-standard-output – Ronaldo Nazarea Nov 26 '12 at 04:24
  • Okay, I have no idea how I missed that. Thank you! – Kalec Nov 26 '12 at 10:21