Pedro Neves
Pedro Neves

Reputation: 374

sending struct array through pipe: win32, C

I'm trying to send a *var, that is in fact a 4 slot array, from one app to another with pipes in win32. How can I do this correctly?

As far as I know, I'm doing it correctly:

    //sending like this:
    if (!WriteFile(hPipeWriteGhosts[i],Ghosts, (DWORD) sizeof(map)*4, &n, NULL)) 
                {  
                    printf("[ERROR] Writing in the pipe... (WriteFile)\n");
                    exit(1);
                }


    //receiving like this
    ret = ReadFile(HReadSPipe, Ghosts, sizeof(map)*4, &n, NULL);   // Lê até ao numero de bytes for zero (pipe fechado) pk o cliente escreveu fim e n escreveu mais nada
        if (!ret || !n)
            MessageBox(hDlgGlobal, str, "Error", MB_YESNO | MB_ICONINFORMATION);  // But in fact it shows me this all the time.

How can I do this correctly?

Thanks in advance.

Upvotes: 0

Views: 1075

Answers (2)

Glenn Sallis
Glenn Sallis

Reputation: 421

You need to transfer the physical data contained within your struct map.

Generally, I use byte arrays for transferring data via pipe. For data items like structs or objects it is better to copy the individual data members of the struct or object into the byte array, potentially also recursively, if you have nested structs for example or arrays containing strings or any other data type with a non-fixed size. Then at the other end of the pipe rebuild the struct or object based on the data received in the byte array. For any data which does not have a fixed size such as int, I write the size of that item into the byte array followed by the data itself.

Also, if it does not appear to be reading correctly at the server end, or potentially not writing correctly at the client end, please send us the any runtime errors which are occuring, if any and also show us how you are declaring and building up your buffer variable, ghost. Are you sure you are not inadvertently transferring the end buffer position instead of the start position, for example?

Upvotes: 0

zmbq
zmbq

Reputation: 39013

OK, your problem is that you're moving handles and pointers across process boundries. Your map contains a char * and an HBITMAP. You transfer char * value, but not the data it points to. You transfer the bitmap's handle, but not the actual bitmap.

Upvotes: 1

Related Questions