简体   繁体   English

JNA具有复杂的结构

[英]JNA with complicated struct

Ultimately I want to determine if the machine my program is running on is a laptop or desktop. 最后,我想确定我的程序运行的机器是笔记本电脑还是台式机。 I'd like to do this with JNA and msn's PowrProf lib, GetPwrCapabilities Function using the LidPresent flag. 我想用JNA和msn的PowrProf lib, GetPwrCapabilities函数使用LidPresent标志来做这个。

Part of the SYSTEM_POWER_CAPABILITIES struct (which is the argument for the GetPwrCapabilities() method) SYSTEM_POWER_CAPABILITIES结构的一部分(它是GetPwrCapabilities()方法的参数)

  BYTE                    spare2[3];
  BYTE                    spare3[8];
  BATTERY_REPORTING_SCALE BatteryScale[3];
  SYSTEM_POWER_STATE      AcOnLineWake;

The SYSTEM_POWER_STATE enum: SYSTEM_POWER_STATE枚举:

typedef enum _SYSTEM_POWER_STATE {
  PowerSystemUnspecified   = 0,
  PowerSystemWorking       = 1,
  PowerSystemSleeping1     = 2,
  PowerSystemSleeping2     = 3,
  PowerSystemSleeping3     = 4,
  PowerSystemHibernate     = 5,
  PowerSystemShutdown      = 6,
  PowerSystemMaximum       = 7 
} SYSTEM_POWER_STATE, *PSYSTEM_POWER_STATE;

The enum was explained here on SO but I'm not sure if I'm doing this right because I get this error: 枚举在这里解释了SO,但我不确定我是否正确这样做因为我收到此错误:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid Structure field in class JNAPlayground$PowrProf$SYSTEM_POWER_CAPABILITIES, field name 'AcOnLineWake', interface JNAPlayground$PowrProf$SYSTEM_POWER_STATE: The type "JNAPlayground$PowrProf$SYSTEM_POWER_STATE" is not supported: Native size for type "JNAPlayground$PowrProf$SYSTEM_POWER_STATE" is unknown 线程“main”中的异常java.lang.IllegalArgumentException:类JNAPlayground中的Invalid Structure字段$ PowrProf $ SYSTEM_POWER_CAPABILITIES,字段名称'AcOnLineWake',接口JNAPlayground $ PowrProf $ SYSTEM_POWER_STATE:不支持类型“JNAPlayground $ PowrProf $ SYSTEM_POWER_STATE”:Native类型“JNAPlayground $ PowrProf $ SYSTEM_POWER_STATE”的大小未知

Could you please guide me or point me in the right direction for: 能否指导我或指出正确的方向:
- The arrays - 阵列
- The enum(if I have this wrong) - 枚举(如果我错了)
- If I'm not importing enough libraries - 如果我没有导入足够的库

My java code so thus far: 到目前为止我的java代码:

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

    public static interface SYSTEM_POWER_STATE
    {
        public static final int owerSystemUnspecified = 0;
        public static final int PowerSystemWorking = 1;
        public static final int PowerSystemSleeping1 = 2;
        public static final int PowerSystemSleeping2 = 3;
        public static final int PowerSystemSleeping3 = 4;
        public static final int PowerSystemHibernate = 5;
        public static final int PowerSystemShutdown = 6;
        public static final int PowerSystemMaximum = 7;

    }

    public static class BATTERY_REPORTING_SCALE extends Structure
    {
        public long Granularity;
        public long Capacity;
    }

    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 SYSTEM_POWER_STATE AcOnLineWake;
        public SYSTEM_POWER_STATE SoftLidWake;
        public SYSTEM_POWER_STATE RtcWake;
        public SYSTEM_POWER_STATE MinDeviceWakeState;
        public SYSTEM_POWER_STATE DefaultLowLatencyWake;
    }

    void GetPwrCapabilities( SYSTEM_POWER_CAPABILITIES result );
}

Thanks, Erik 谢谢,Erik

After google the h**l out of this, I tried revisiting jna's main web page and ignoring the other enum question here on SO. 在google h ** l之后,我尝试重新访问jna的主要网页,而忽略了其他枚举问题。 The mapping of the enum is here . 枚举的映射在这里 My code is now showing that a lid is present! 我的代码现在显示有盖子!

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);

        public static class BATTERY_REPORTING_SCALE extends Structure
        {
            public long Granularity;
            public long Capacity;
        }

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

        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);
    }
}
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public class SystemProfilerWindowsImpl
{
  public static void main(String[] args)
  {
    Log.info("SystemProfilerWindowsImpl", "main", "is laptop:" + isLaptop());
  }

  public static boolean isLaptop()
  {
    byte batteryFlag = getBatteryFlag();
    boolean isLaptop = false;

    if (batteryFlag == -128)
    {
      Log.debug("batt flag - no batt");
    }
    else if (batteryFlag == -1)
    {
      Log.debug("batt flag - unknown");
    }
    else
    {
      Log.debug("battery flag " + batteryFlag);
      isLaptop = true;
    }
    return isLaptop;
  }

  private static byte getBatteryFlag()
  {
    Kernel32 lib = Kernel32.INSTANCE;
    SystemProfilerWindowsImpl.Kernel32.SYSTEM_POWER_STATUS status = new SystemProfilerWindowsImpl.Kernel32.SYSTEM_POWER_STATUS();
    lib.GetSystemPowerStatus(status);
    if (status.BatteryLifePercent == -1)
    {
      Log.debug("battery life percent is unknown");
    }
    else
    {
      Log.debug("battery life percent is " + status.BatteryLifePercent);
    }

    byte batteryFlag = status.BatteryFlag;
    return batteryFlag;
  }

  public String getModel()
  {
    if (isLaptop())
    {
      return "WinLaptop";
    }

    return "WinDesktop";
  }

  public static abstract interface Kernel32 extends StdCallLibrary
  {
    public static final Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", 
      Kernel32.class);

    public abstract void GetSystemPowerStatus(SYSTEM_POWER_STATUS paramSYSTEM_POWER_STATUS);

    public static class SYSTEM_POWER_STATUS extends Structure
    {
      public byte ACLineStatus;
      public byte BatteryFlag;
      public byte BatteryLifePercent;
      public byte Reserved1;
      public int BatteryLifeTime;
      public int BatteryFullLifeTime;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM