9

我有以下枚举我如何在 jna 中映射?

这个枚举在结构中被进一步引用。

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

阿卜杜勒哈利克

4

5 回答 5

12

如果您使用 JNA,您可能希望在 Java 中明确指定枚举的值。默认情况下,Java 的基本枚举类型并没有真正为您提供该功能,您必须为 EnumSet 添加构造函数(参见thisthis)。

编码 C 枚举的一种简单方法是使用封装在与枚举同名的类中的 public static final const int。您可以获得从 Java 枚举中获得的大部分功能,但分配值的开销略少。

一些优秀的 JNA 示例,包括下面的片段(已复制)可在此处获得。

假设您的 C 代码如下所示:

enum Values {
     First,
     Second,
     Last
};

然后Java看起来像:

public static interface Values {
    public static final int First = 0;
    public static final int Second = 1;
    public static final int Last = 2;
}
于 2009-08-31T18:40:05.817 回答
8

在我的博客上,我写了一种方便的方法来使用真正的Javaenum和 JNA,而不仅仅是任意int的。它有点复杂,但它有几个优点:

  • 您获得了大部分类型安全和错误预防
  • 您的 IDE 可以建议/自动完成事情
  • 您可以制作更高级且更简单的 Java API

基本上,您需要为 . 使用自定义TypeConverterenum并通过简单的TypeMapper. 大多数额外的代码是为了避免需要TypeConverter为每个不同的enum类单独制作。(就我而言,我必须制作很多。)


你可以在我的jhllib项目中看到一些真实世界的代码。特别是,查看HlTypeMapperEnumConverterJnaEnum的定义和用法。

于 2010-12-03T00:44:47.473 回答
2

在结构中引用此枚举时,您只想将其声明为 int,而不是 eFtUsbDeviceStatus 或类似的东西。作为示例,请参见下面的 AcOnLineWake:

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);
    }
}
于 2010-03-10T19:44:42.717 回答
0

枚举的 C++ 和 Java 在语法上没有太大区别。

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

您可以将其称为 eFtUsbDeviceStatus。

于 2009-07-31T03:48:13.720 回答
0

JNA 现在包含一个EnumConverter

于 2021-04-30T14:35:00.303 回答