作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码深受 this answer 的启发。和 this answer ,我试图让它与 Kotlin 语言一起工作,而不是在那里的答案中给出的 Java。
它基本上基于 JNA(Java Native Access),它本质上是拉取 SYSTEM_BATTERY_STATE从 Windows native 库(在 C++ 上)获取有关笔记本电脑电池的信息。
该代码在 Java 版本中按预期工作,但如果我尝试对 Kotlin 做同样的事情,我会收到一个异常,说声明的字段名称是一个空列表/数组。
fun main() { getBatteryState() }
fun getBatteryState(): SYSTEM_BATTERY_STATE? {
val batteryState = SYSTEM_BATTERY_STATE()
val retrieveValue = PowrProf.CallNtPowerInformation(
5,
Pointer.NULL,
0,
batteryState,
batteryState.size().toLong()
)
return if (retrieveValue == 0) batteryState else null
}
interface PowrProf : StdCallLibrary {
@Suppress("FunctionName")
fun CallNtPowerInformation(informationLevel: Int, inBuffer: Pointer?, inBufferLen: Long, outBuffer: SYSTEM_BATTERY_STATE?, outBufferLen: Long): Int
companion object : PowrProf by Native.load("PowrProf", PowrProf::class.java)!!
}
class SYSTEM_BATTERY_STATE : Structure(ALIGN_MSVC), Structure.ByReference {
var AcOnLine: Byte = 0
var BatteryPresent: Byte = 0
var Charging: Byte = 0
var Discharging: Byte = 0
var Spare1_0: Byte = 0
var Spare1_1: Byte = 0
var Spare1_2: Byte = 0
var Spare1_3: Byte = 0
var MaxCapacity = 0
var RemainingCapacity = 0
var Rate = 0
var EstimatedTime = 0
var DefaultAlert1 = 0
var DefaultAlert2 = 0
override fun getFieldOrder(): List<String> {
return listOf(
"AcOnLine", "BatteryPresent", "Charging", "Discharging",
"Spare1_0", "Spare1_1", "Spare1_2", "Spare1_3",
"MaxCapacity", "RemainingCapacity", "Rate",
"EstimatedTime", "DefaultAlert1", "DefaultAlert2"
)
}
}
Exception in thread "main" java.lang.Error: Structure.getFieldOrder() on class com.animeshz.github.batteryinfo.SYSTEM_BATTERY_STATE returns names ([AcOnLine, BatteryPresent, Charging, DefaultAlert1, DefaultAlert2, Discharging, EstimatedTime, MaxCapacity, Rate, RemainingCapacity, Spare1_0, Spare1_1, Spare1_2, Spare1_3]) which do not match declared field names ([])
at com.sun.jna.Structure.getFields(Structure.java:1089)
at com.sun.jna.Structure.deriveLayout(Structure.java:1232)
at com.sun.jna.Structure.calculateSize(Structure.java:1159)
at com.sun.jna.Structure.calculateSize(Structure.java:1111)
at com.sun.jna.Structure.allocateMemory(Structure.java:414)
at com.sun.jna.Structure.<init>(Structure.java:205)
at com.sun.jna.Structure.<init>(Structure.java:193)
at com.sun.jna.Structure.<init>(Structure.java:180)
at com.animeshz.github.batteryinfo.SYSTEM_BATTERY_STATE.<init>(SYSTEM_BATTERY_STATE.kt:9)
at com.animeshz.github.batteryinfo.UtilKt.getBatteryState(util.kt:11)
at com.animeshz.github.batteryinfo.UtilKt.main(util.kt:60)
at com.animeshz.github.batteryinfo.UtilKt.main(util.kt)
最佳答案
JNA Structure
类使用反射来查找字段名称,这意味着它们必须声明为 public
字段/属性。 @JvmField
annotation in Kotlin删除 getter 并根据需要公开这些属性。我怀疑这应该对你有用(未经测试):
class SYSTEM_BATTERY_STATE(
@JvmField var AcOnLine: Byte = 0,
@JvmField var BatteryPresent: Byte = 0,
@JvmField var Charging: Byte = 0,
@JvmField var Discharging: Byte = 0,
@JvmField var Spare1_0: Byte = 0,
@JvmField var Spare1_1: Byte = 0,
@JvmField var Spare1_2: Byte = 0,
@JvmField var Spare1_3: Byte = 0,
@JvmField var MaxCapacity = 0,
@JvmField var RemainingCapacity = 0,
@JvmField var Rate = 0,
@JvmField var EstimatedTime = 0,
@JvmField var DefaultAlert1 = 0,
@JvmField var DefaultAlert2 = 0
) : Structure(ALIGN_MSVC), Structure.ByReference {
override fun getFieldOrder() = listOf(
"AcOnLine", "BatteryPresent", "Charging", "Discharging",
"Spare1_0", "Spare1_1", "Spare1_2", "Spare1_3",
"MaxCapacity", "RemainingCapacity", "Rate",
"EstimatedTime", "DefaultAlert1", "DefaultAlert2"
)
}
关于java - Kotlin JNA 将声明的字段名称设为空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62240901/
概述 CentOS Stream 成立于 2019 年,是“RHEL 下一步的滚动预览”。Red Hat 首席技术官 Chris Wright 和 CentOS 社区经理 Rich Bowen 各
我有一个使用 Mesosphere DC/OS 编排选项进行配置的 Azure 容器服务 (ACS) 集群。我可以在 Marathon UI 中创建一个应用程序。 但是,当我通过 Marathon U
我是一名优秀的程序员,十分优秀!