How to use `enum` with `ffi`?
See original GitHub issueHi! I’m working on a project that integrates a shared C library with Node. I’m using the ffi module for this which, in turn, uses the ref library for types. I’ve seen that @TooTallNate did a PR awhile back to implement the type interface so this could be used with ref. I can’t, for the life of me, figure out how to get this to work. Full disclosure: I’m not a C programmer at all.
Here’s the enum definition in the C code:
typedef enum {
DEVICE_UNKNOWN,
DEVICE_IOS_IPHONE,
DEVICE_IOS_IPOD,
DEVICE_IOS_IPAD,
DEVICE_WINDOWS_PC,
DEVICE_WINDOWS_PHONE,
DEVICE_WINDOWS_TABLET,
DEVICE_MAC
} E_DEVICE_TYPE;
There is a method called SetDeviceType that accepts DEVICE_MAC as it’s input parameter. In my Javascript I’ve done the following:
const deviceTypeEnum = new Enum([
'DEVICE_UNKNOWN',
'DEVICE_IOS_IPHONE',
'DEVICE_IOS_IPOD',
'DEVICE_IOS_IPAD',
'DEVICE_WINDOWS_PC',
'DEVICE_WINDOWS_PHONE',
'DEVICE_WINDOWS_TABLET',
'DEVICE_MAC',
], { name: 'E_DEVICE_TYPE' });
I’ve also tried:
const deviceTypeEnum = new Enum({
DEVICE_UNKNOWN: 0,
DEVICE_IOS_IPHONE: 1,
DEVICE_IOS_IPOD: 2,
DEVICE_IOS_IPAD: 3,
DEVICE_WINDOWS_PC: 4,
DEVICE_WINDOWS_PHONE: 5,
DEVICE_WINDOWS_TABLET: 6,
DEVICE_MAC: 7,
}, { name: 'E_DEVICE_TYPE' });
I then call my function (defined on ffi) like so:
this.client.setDeviceType(deviceTypeEnum.DEVICE_MAC);
and also tried:
this.client.setDeviceType(deviceTypeEnum.DEVICE_MAC.value);
On the ffi definition I’ve tried setting the parameter type as int and uint32. Unfortunately, this doesn’t seem to be setting the correct value in the C library. Can anyone see if I’m missing something? It’s probably something obvious but I’ve been bashing my head against this for a couple of days and thought I’d reach out for assistance. Thanks for any help you might be able to provide!
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
no need of defining enum in your javascript code , but at the place of enum define an int variable , so at the time of passing of argument pass only an integer value to the dll , dll at backend will accept this .
This thread has been closed since there has not been any recent activity. Please reopen if still needed.