views:

529

answers:

3

Hi,

I have the following enum how do i map in jna ??

This enum is further referenced in structure.

typedef enum
{
 eFtUsbDeviceNotShared,
 eFtUsbDeviceSharedActive,
 eFtUsbDeviceSharedNotActive,
 eFtUsbDeviceSharedNotPlugged,
 eFtUsbDeviceSharedProblem
} eFtUsbDeviceStatus;

Abdul Khaliq

A: 

There is not much difference in syntax between C++ and Java for an enum.

enum eFtUsbDeviceStatus {
   eFtUsbDeviceNotShared,
   eFtUsbDeviceSharedActive,
   eFtUsbDeviceSharedNotActive,
   eFtUsbDeviceSharedNotPlugged,
   eFtUsbDeviceSharedProblem
}

You can reference it as eFtUsbDeviceStatus.

Rusty
+1  A: 

If you're using JNA you probably want to explicitly specify the values of the enumeration in Java. By default, Java's basic enum type doesn't really give you that functionality, you have to add a constructor for an EnumSet (see this and this).

A simple way to encode C enumerations is to use public static final const ints wrapped in a class with the same name as the enum. You get most of the functionality you'd get from a Java enum but slightly less overhead to assign values.

Some good JNA examples, including the snippets below (which were copied) are available here.

Suppose your C code looks like:

enum Values {
     First,
     Second,
     Last
};

Then the Java looks like:

public static interface Values {
    public static final int First = 0;
    public static final int Second = 1;
    public static final int Last = 2;
}
Mark E
+1  A: 

When referencing this enum in your structure, you just want to declare it as an int, not eFtUsbDeviceStatus or anything like that. As an example see AcOnLineWake below:

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public class JNAPlayground
{

    public interface PowrProf extends StdCallLibrary
    {
        PowrProf INSTANCE = (PowrProf) Native.loadLibrary(
                "C:\\WINDOWS\\system32\\PowrProf.dll", PowrProf.class);

/*  
typedef struct {
  ULONG Granularity;
  ULONG Capacity;
}BATTERY_REPORTING_SCALE, *PBATTERY_REPORTING_SCALE; */
        public static class BATTERY_REPORTING_SCALE extends Structure
        {
            public long Granularity;
            public long Capacity;
        }

/*
typedef struct {
  BOOLEAN                 PowerButtonPresent;
  BOOLEAN                 SleepButtonPresent;
  BOOLEAN                 LidPresent;
  BOOLEAN                 SystemS1;
  BOOLEAN                 SystemS2;
  BOOLEAN                 SystemS3;
  BOOLEAN                 SystemS4;
  BOOLEAN                 SystemS5;
  BOOLEAN                 HiberFilePresent;
  BOOLEAN                 FullWake;
  BOOLEAN                 VideoDimPresent;
  BOOLEAN                 ApmPresent;
  BOOLEAN                 UpsPresent;
  BOOLEAN                 ThermalControl;
  BOOLEAN                 ProcessorThrottle;
  BYTE                    ProcessorMinThrottle;
  BYTE                    ProcessorMaxThrottle;
  BOOLEAN                 FastSystemS4;
  BYTE                    spare2[3];
  BOOLEAN                 DiskSpinDown;
  BYTE                    spare3[8];
  BOOLEAN                 SystemBatteriesPresent;
  BOOLEAN                 BatteriesAreShortTerm;
  BATTERY_REPORTING_SCALE BatteryScale[3];
  SYSTEM_POWER_STATE      AcOnLineWake; // enum
  SYSTEM_POWER_STATE      SoftLidWake;
  SYSTEM_POWER_STATE      RtcWake;
  SYSTEM_POWER_STATE      MinDeviceWakeState;
  SYSTEM_POWER_STATE      DefaultLowLatencyWake;
}SYSTEM_POWER_CAPABILITIES, *PSYSTEM_POWER_CAPABILITIES;
 */
        public static class SYSTEM_POWER_CAPABILITIES extends Structure
        {
            public boolean PowerButtonPresent;
            public boolean SleepButtonPresent;
            public boolean LidPresent;
            public boolean SystemS1;
            public boolean SystemS2;
            public boolean SystemS3;
            public boolean SystemS4;
            public boolean SystemS5;
            public boolean HiberFilePresent;
            public boolean FullWake;
            public boolean VideoDimPresent;
            public boolean ApmPresent;
            public boolean UpsPresent;
            public boolean ThermalControl;
            public boolean ProcessorThrottle;
            public int ProcessorMinThrottle;
            public int ProcessorMaxThrottle;
            public boolean FastSystemS4;
            public int spare2[] = new int[3];
            public boolean DiskSpinDown;
            public int spare3[] = new int[8];
            public boolean SystemBatteriesPresent;
            public boolean BatteriesAreShortTerm;
            public BATTERY_REPORTING_SCALE BatteryScale[] =  new BATTERY_REPORTING_SCALE[3];
            public int AcOnLineWake;
            public int SoftLidWake;
            public int RtcWake;
            public int MinDeviceWakeState;
            public int DefaultLowLatencyWake;
        }

        // http://msdn.microsoft.com/en-us/library/aa372691(VS.85).aspx
        void GetPwrCapabilities( SYSTEM_POWER_CAPABILITIES result );
    }

    public static void main( String[] args )
    {
        PowrProf lib2 = PowrProf.INSTANCE;
        PowrProf.SYSTEM_POWER_CAPABILITIES systemPOWERCAPABILITIES = new PowrProf.SYSTEM_POWER_CAPABILITIES();
        lib2.GetPwrCapabilities(systemPOWERCAPABILITIES);

        System.out.println("Lid present:" + systemPOWERCAPABILITIES.LidPresent);
    }
}
Erik B