2010-03-10 5 views
3

En fin de compte, je veux déterminer si la machine sur laquelle mon programme s'exécute est un ordinateur portable ou de bureau. Je voudrais faire cela avec JNA et PowrProf lib de msn, GetPwrCapabilities Function en utilisant le drapeau LidPresent.JNA avec struct complexe

Une partie de la SYSTEM_POWER_CAPABILITIES struct (qui est l'argument de la méthode GetPwrCapabilities())

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

Le SYSTEM_POWER_STATE ENUM:

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; 

ENUM a été expliqué ici sur le SO, mais je ne suis pas sûr si je fais cela correctement parce que je reçois cette erreur:

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

Pourriez-vous s'il vous plaît me guider ou me diriger dans la bonne direction pour:
- Les tableaux
- Le ENUM (si je me trompe)
- Si je suis les bibliothèques pas assez d'importer

Mon code java donc jusqu'ici:

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

Merci, Erik

Répondre

1

Après Google le h ** l de cela, j'ai essayé revisiter la page web principale et en ignorant l'jna autre question ENUM ici ALORS. Le mappage de l'énumération est here. Mon code montre maintenant qu'un couvercle est présent!

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); 
    } 
} 
+1

Ce code fonctionne, mais il produit « présente du couvercle: true » sur chaque ordinateur que je l'exécute sur. –

+0

D'où vient l'enum? –

+0

@ErikB une chance de résoudre ce problème? Je suis confronté à un problème similaire. Veuillez également répondre à la question de Bjarke Freund-Hansen, car elle est notoirement ignorée à partir de votre deuxième message. Merci – amadib

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