简体   繁体   English

带字符串的邮槽

[英]Mailslot with string

I need to make a message concating various parts. 我需要发表一条消息,概括各个部分。 So I used ostringstream. 所以我用了ostringstream。 Now I have a std::string or a const * char to send by mailslot. 现在我有一个std :: string或const * char通过邮筒发送。 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. 我想使用std :: cout或printf读取此提示。

Regards, Leandro Lima 问候,利安德罗·利马

Storing the result of c_str() is safe as long as the string objects are not modified, but that's beside the point. 只要不修改字符串对象,存储c_str()的结果就是安全的,但这并不重要。 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. 将&a和&alarm更改为a和Alarm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM