Hello,
I try to use POSIX Message Queue API and in the doc it says that:

#include <mqueue.h>
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned msg_prio);

The mq_send() function shall add the message pointed to by the argument msg_ptr to the message queue specified by mqdes. The msg_len argument specifies the length of the message, in bytes, pointed to by msg_ptr. The value of msg_len shall be less than or equal to the mq_msgsize attribute of the message queue, or mq_send() shall fail.

As seen in the prototype of the function mq_send, the message element is represented by const char *msg_ptr. But what will it be if I need to send a message which is not a const char*, for example a structure object?

I wish if I explained my problem clearly. If not, I can try to explain more.
Thanks in advance.

That's history for you.

Ideally, it would be const void*, then you could point at anything you liked so long as you also set msg_len to be the right size.

For most machines, you can do (const char *)&myvar, sizeof(myvar) kinda thing.
A small wrapper function taking a void* instead would help keep your code nice and tidy (and give you an easy way out if pointer representations was an issue).

The standard C library functions which take "pointer to some memory and a size" have all (I think) been updated to use void*, rather than the historic char*. But POSIX seems to be sticking with the old for now.

For most machines, you can do (const char *)&myvar, sizeof(myvar)

Hi there,

I am following your advice given here to send a pointer to a class object via a POSIX message queue. It is giving me strange results and perhaps you might help me.

Here is part of my class.
class Message
{
char id;
char *send_buffer; /*Allocated in the constructor*/
unsigned short size;
}

_____

int main(void)
{
Message *M = new Message();
mq_send(queue_handle, (const char*)M, sizeof(M), 1);
}

On the recievig side, I am only able to retreive M->id. Both
*(M->send_buffer) and M->size turn out to be all zeros. Any ideas?

Thanks.
Karan

Hi there,

I am following your advice given here to send a pointer to a class object via a POSIX message queue. It is giving me strange results and perhaps you might help me.

Here is part of my class.
class Message
{
char id;
char *send_buffer; /*Allocated in the constructor*/
unsigned short size;
}

_____

int main(void)
{
Message *M = new Message();
mq_send(queue_handle, (const char*)M, sizeof(M), 1);
}

On the recievig side, I am only able to retreive M->id. Both
*(M->send_buffer) and M->size turn out to be all zeros. Any ideas?

Thanks.
Karan

Because you didn't specify the size of send_buffer.
try like that:

char send_buffer[BUF_LEN];

Hi xyzt,

Yes, you are right. I actually found the resolution to the problem many months ago.

Initially, I was thinking that the message-queues can be used to exchange pointers between processes (which is what shared-memory allows us to have). Of course, my assumption was wrong, and hence, it never behaved the way I had expected.

Finally, I switched my code to have a static array (of pre-declared size), after that I got successful transactions because the posix message queues could copy all of the data into the queue.

Thanks for your reply.

Karan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.