views:

97

answers:

1

Hi, I need to make a message concating various parts. So I used ostringstream. Now I have a std::string or a const * char to send by mailslot. I have tryed many ways to do it but every time I receive wrong messages. I would like to know a solution to send messages by mailslot and receive it and show it by console.

My code to generate and send the mail is:

std::ostringstream oss;
    oss << "RE" << "01" << "01:01:02.350" << "REMOTA 01 - MSG DESCARTADA";
    std::string alarm = oss.str();

    const char *a = alarm.c_str();
ASSERT(WriteFile(hMailslot, &a, strlen(a), &dwBytesSent, NULL),

"Impossible to sent message.");

and to receive the message is:

char alarm[42];
      DWORD bytesRead;
      ASSERT(ReadFile(hMailslot, &alarm, strlen(alarm), &bytesRead, NULL), 
       "Impossible to read file. ERROR: " << GetLastError());

I would like to use a std::cout or a printf to read this mensage.

Regards, Leandro Lima

+1  A: 

Storing the result of c_str() is safe as long as the string objects are not modified, but that's beside the point. The problem is you are passing the address of a pointer to a string instead of simply a pointer to a string. Change &a and &alarm to just a and alarm.

Luke