Send DWORD via named pipe

I'm trying to send an array of DWORDs over a named pipe, but I'm stuck trying to figure out how to send a single DWORD. This is what I have so far:

// Create a pipe to send data
HANDLE pipe = CreateNamedPipe(
        L"\\\\.\\pipe\\my_pipe",
        PIPE_ACCESS_OUTBOUND,
        PIPE_TYPE_BYTE,
        1,
        0,
        0,
        0,
        NULL
    );

/* Waiting for the other side to connect and some error handling cut out */

//Here I try to send the DWORD  
DWORD msg = 0xDEADBEEF;
DWORD numBytesWritten = 0;
result = WriteFile(
    pipe,
    (LPCVOID)msg, 
    sizeof(msg),
    &numBytesWritten, 
    NULL 
    );

      

But the call WriteFile(...)

fails and returns false

.

Final result:

/* CreateFile(...) */
DWORD msg[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
    pipe,
    msg, 
    127 * sizeof(DWORD), 
    &numBytesRead, 
    NULL 
    );

      

Am I failing or am I heading in the right direction?

+3


source to share


1 answer


result = WriteFile(
    pipe,
    &msg, // <---- change this line
    sizeof(msg),
    &numBytesWritten, 
    NULL 
    );

      

When you throw, the red flags should go to your head. In C ++, the typhus language, the moment you try to override types manually, you are in a danger zone. WriteFile

expects a pointer to data. You have provided the data yourself. Instead, you must point to a pointer to the data.



Also, learn to use GetLastError

for more information when a call fails.

+4


source







All Articles