[fpc-pascal] ExecuteProcess problem

Karoly Balogh (Charlie/SGR) charlie at scenergy.dfmk.hu
Tue Aug 25 15:52:15 CEST 2020


Hi,

On Tue, 25 Aug 2020, Koenraad Lelong via fpc-pascal wrote:

> I'm extending an old daemon so it writes the data to MQTT, using
> mosquitto_pub.
>
> To have a correct message (-m ...) I need to include double-quotes. But
> it seems ExecuteProcess(exec,cmdline) splits the cmdline according some
> rules if it's a single string.
> When I tried to do it in a single string :
> -h 127.0.0.1 -t solartopic -u user -P secret -i clientid -m "{\"Pac\":
> 458.00}"
> I got :
> Error: Unknown option 'Pac\":'.
> So I need to make cmdline an array. How I do that ?
> I tried :
>   MQTTStr : array [0..5] of ansistring;
> ...
>   MQTTStr[0]:='-h '+MQTTBroker;
>   MQTTStr[1]:='-t '+MQTTTopic;
>   MQTTStr[2]:='-u '+MQTTUser;
>   MQTTStr[3]:='-P '+MQTTPassw;
>   MQTTStr[4]:='-i '+MQTTClientID;
>   MQTTStr[5]:='-m "{\"'+ChanName+'\": '+TextValue+'}"';
>   ExecuteProcess(MQTTExec,MQTTStr));
> But then :
> Error: Unknown option '-h 127.0.0.1'.
> Use 'mosquitto_pub --help' to see usage.
>
> Thanks in advance

Just a guess, but I think you need to do:

MQTTStr[0]:='-h';
MQTTStr[1]:=MQTTBroker;
MQTTStr[2]:='-t';
MQTTStr[3]:=MQTTTopic;

... etc.
MQTTStr[n]:='-m';
MQTTStr[n+1]:=MQTTMessage;


Like this. It's maybe a bit counter intuitive, but you have to think from
the perspective of the other app, everything which you put into 1 string
will go into 1 "slot" of its "ParamStr(x)". And then it's maybe more
obvious how you need to split your arguments.

BTW, shameless plug, but this is a very very hacky way to talk to an MQTT
broker I think, I've made a raw C header conversion, and a Pascal-style
multithreaded OOP wrapper for libmosquitto, so you can properly integrate
MQTT communication in your Free Pascal app (tested on Linux, MacOS and
Windows):

https://github.com/chainq/mosquitto-p

I'm not sure of the complexity of what you want to do, but for anything
remotely more complex, than sending 1 message every minute or so, this
might be a better solution than trying to squeeze things through
mosquitto_pub's arguments.

Cheers,
--
Charlie


More information about the fpc-pascal mailing list