如何在 JNA 中映射枚举

发布于 2024-07-28 00:52:46 字数 271 浏览 4 评论 0原文

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

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

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

阿卜杜勒·哈利克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最好是你 2024-08-04 00:53:23

JNA 现在包含 枚举转换器

JNA now includes an EnumConverter.

岁月静好 2024-08-04 00:53:17

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

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

您可以将其引用为 eFtUsbDeviceStatus。

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.

无边思念无边月 2024-08-04 00:53:11

在结构中引用此枚举时,您只想将其声明为 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);
    }
}

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);
    }
}
只涨不跌 2024-08-04 00:53:07

在我的博客上,我写了一种使用real Java enum<的便捷方法/code> 与 JNA,而不仅仅是任意 int。 它有点复杂,但它有几个优点:

  • 您可以获得大部分的类型安全性和错误预防功能
  • 您的 IDE 可以建议/自动完成一些事情
  • 您可以制作更经典、更简单的 Java API

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


您可以在我的 jhllib< 中看到一些真实世界的代码/a> 项目。 特别是查看 HlTypeMapper< 的定义和用法/a>、EnumConverter 和 < a href="https://github.com/DHager/jhllib/search?utf8=%E2%9C%93&q=JnaEnum" rel="nofollow">JnaEnum。

On my blog I wrote up a convenient way to use real Java enums with JNA, rather than just arbitrary ints. It's a bit more complex, but it has several advantages:

  • You get most of the type-safety and error-prevention
  • Your IDE can suggest/autocomplete things
  • You can make a much class-ier and easier Java API

Basically, you need to use a custom TypeConverter for the enum, and provide that to JNA through a simple TypeMapper. Most of the extra code is to avoid needing to make a separate TypeConverter for each different enum class. (In my case, I had to make a lot of them.)


You can see some real-world code in my jhllib project. In particular, look at the definitions and usages of HlTypeMapper, EnumConverter, and JnaEnum.

薄情伤 2024-08-04 00:53:01

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

对 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文