1

I need to pass messages to my C program from PHP and I am doing this via message queues.

I have the message queues working and both sides can receive messages.

The problem is on the php side formatting the data. I am trying to send C style string, but php handles strings much differently. How would I convert the php string into a null temrinated C string?

Basically I need 'config1' to be the null terminated string.

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', true, false, $error);

It appears php stores strings like so: \"s:8:\\"config1\000\\"; where \ are just escapes.

Is there any way to do this, or a different way to parse this from the C side in order to convert it to a C string?

user623879
  • 4,066
  • 9
  • 38
  • 53

2 Answers2

2

You can try with

$nullTerminatedString = sprintf("config1%c", 0);
// or directly using escape sequence
$nullTerminatedString = "config\0";
echo strlen(sprintf("config1%c", 0)); # returns 8, so it should work

Taken from here

serialize The optional serialize controls how the message is sent. serialize defaults to TRUE which means that the message is serialized using the same mechanism as the session module before being sent to the queue. This allows complex arrays and objects to be sent to other PHP scripts, or if you are using the WDDX serializer, to any WDDX compatible client.

So your call should be:

msg_send($mq_id, $MSG_CHANGECONFIG, 'config1', **false**, false, $error);
Fabio
  • 18,856
  • 9
  • 82
  • 114
  • 1
    Looking at the data in the C program, php adds some characters to the beginning and end of the data...\"s:8:\\\"config1\\000\\\";...Pretty freaking annoying..I don't want to parse that in C – user623879 Sep 08 '11 at 21:37
  • @user: looks like a php serialized string – knittl Sep 08 '11 at 21:38
  • That is not chars from normal php string handling, that's the output from [serialize](http://php.net/manual/en/function.serialize.php) function. – Fabio Sep 08 '11 at 21:39
  • As knittl says, it's serialised data, which you are asking the msg_send() function to do (4th param). Changing this to false should prevent this. (http://www.php.net/manual/en/function.msg-send.php) – Craig A Rodway Sep 08 '11 at 21:42
0

If you only want to add a null byte to it, use an octal escape sequence (you need to use double quotes for that):

"config1\0"
NikiC
  • 100,734
  • 37
  • 191
  • 225