#Passing C enum to function and read in TypeScript

1 messages · Page 1 of 1 (latest)

smoky moth
#

First of all I am not a trained software developper, so please forgive me if I am missing some basics. I am trying to build a wrapper around a dynamic library to IEC 61850 communication.

On the the exposed functions of that library is IedConnection_connect(..., IedClientError error, ...) , where IedClientError is a enum with 23 entries.

To be able to use the library I need to

  1. create the enumeration in TypeScript and change it to a type array? to be able to pass it down
  2. but then I also need to read the changed error again in TypeScript

I am not sure how enumerations are coded and how to decode those in TypeScript again and I was hoping one of you can help me out 🙂

BR,
Jakob

north sparrow
sleek shell
#

They are just numbers
You need to find the enum declaration in the header file (something like IedClientError.h) and see what value is assigned to each enum option. If they are not assigned anything, by default they are assigned a number starting from 0. So if you want to use the first enum option, you pass 0

smoky moth
#

How would+

#

This means that I could do something like this?

let error = 0;
lib.symbols.IedConnection_connect(... , error, ...);
console.log(error)

#

This gives me a Segmentation fault (core dumped) error. I think the enum has to be instantiated as an ArrayBuffer, but do not know how to read that buffer correctly.

sleek shell
#

That should work, without an ArrayBuffer
Are you declaring the enum as i32?
IedConnection_connect: {parameters: [..., "i32", ...]}

#

ooh sorry I just reread OP

#

the function will change the value of the error you pass?

smoky moth
#

Yes exactly that

sleek shell
#

I'm stupid

smoky moth
#

It does work like so for me. But I am not sure if this is the propper way of doing it:

const error = new Uint8Array(1);
lib.symbols.IedConnection_connect(conn, error, host, port);

#

The result is then error[0]

#

And I declare the variable as buffer in the symbols object

sleek shell
#

That's correct

smoky moth
#

Thank you for the fast response 😄

sleek shell
#

BTW, to make sure the function is actually writing to the buffer, I suggest you fill up the buffer with something before calling it, just to see if it goes back to zero