LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

read void pointer from dll

Solved!
Go to solution

Hello all,

 

I am developing a .dll that uses the Paho library to communicate over MQTT, but I can't figure out how to pass the reference the MQTT client I am creating in the dll. The MQTTclient is defined as a void pointer.

 

typedef void* MQTTClient;

So, in my first function I tried this:

int DLL_EXPORT MQTT_ConnectLocal(MQTTClient client)
{
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; //create connectOptions struct with default values

    MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);//Creates the client
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    int rc = 42;
    rc = MQTTClient_connect(client, &conn_opts);//connects the client to the server
    char text[128];
    sprintf(text, "MQTTclient: %d", client);
    MessageBox(0, text, "", 0);
    return rc;
}

In the call library function node, I configured the return value to be int32 and for the client I tried all possible options. The function gets called alright and I see the connection on my MQTT server. The MessageBox displays different Values each time I call it. As an aside: When I let the messagebox display &client, the value stays constant.

The client output of the node returns 0. I have tried all possible configurations of that output after the different topics I found suggested various datatypes, whicha ll didn't work for me.

 

I can typecast the reference to int64_t and return it, but then I don't know how to assign that value to an MQTTclient variable in my second call.

 

Can anyone please help me figure this out? Any help is appreciated!



Remember Cunningham's Law
0 Kudos
Message 1 of 3
(2,237 Views)
Solution
Accepted by topic author PeterFoerster

 

typedef void* MQTTClient;

With this the library declares an opaque pointer that somehow contains the session. How exactly this is done is on puprose hidden behind the opaque pointer and you are not supposed to make any assumptions about it.

 

But in order for a C function to be able to return a value to a caller through its parameters this parameter needs to be declared as reference. So rather than:

 

int DLL_EXPORT MQTT_ConnectLocal(MQTTClient client);

you want to declare this function as

 

int DLL_EXPORT MQTT_ConnectLocal(MQTTClient *client);

if you program in Standard C and if you would program in C++ you actually would better declare it as

 

int DLL_EXPORT MQTT_ConnectLocal(MQTTClient &client);

 

Then you of course also need to adjust the contents of your function to properly use the reference value. There is a reason why the function MQTTClient_create is declared as:

MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context);

In the LabVIEW Call Library Node you then configure this parameter as:

 

Type: Numeric, Data Type: (Unsigned or Signed) pointer sized Integer, Pass: Pointer to Value

 

All the other functions that receive such a value to use you can leave this parameter declared as

someFun(MQTTClient client, ....);

and in the Call Library Node you configure that parameter accordingly as:

Type: Numeric, Data Type: (Unsigned or Signed) pointer sized Integer, Pass: Value

 

All controls on subVI frontpanels that are used to pass this parameter around should be of type (Unsigned or Signed) 64-bit Integer.

Rolf Kalbermatter
My Blog
Message 2 of 3
(2,211 Views)

Thank you for the insight! This is exactly the solution.

 

I had tried defining it like you said before, but got an error 1097 and thought I was on the wrong path. Turns out, I forgot to change the parameter in the second function call accordingly, which threw the error.



Remember Cunningham's Law
0 Kudos
Message 3 of 3
(2,178 Views)