gpt4 book ai didi

java - 如何使用 JNA 传递结构数组

转载 作者:行者123 更新时间:2023-12-01 05:05:11 24 4
gpt4 key购买 nike

我引用了另一篇 stackoverflow 文章“如何将指针映射到 JNA 中的结构数组”,得出以下代码来枚举 Windows 服务依赖项。

结构和函数声明:

static class SERVICE_STATUS extends Structure {
public int dwServiceType;
public int dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
public SERVICE_STATUS(){}
}

static class ENUM_SERVICE_STATUS extends Structure {
public ENUM_SERVICE_STATUS(){ }

public WString lpServiceName;
public WString lpDisplayName;
SERVICE_STATUS serviceStatus;
}

boolean EnumDependentServicesW(Pointer hService, int serviceState, ENUM_SERVICE_STATUS serviceStatuses, int size, IntByReference bytesNeeded, IntByReference servicesReturned);

如果只有一个服务依赖项,则以下代码有效:

IntByReference bytesNeeded = new IntByReference();
IntByReference numberOfServices = new IntByReference();
Advapi32.ENUM_SERVICE_STATUS serviceStatus = new Advapi32.ENUM_SERVICE_STATUS();
Advapi32.ENUM_SERVICE_STATUS[] serviceStatuses = (Advapi32.ENUM_SERVICE_STATUS[]) serviceStatus.toArray(1);

if (!advapi32.EnumDependentServicesW(serviceHandle, Advapi32.SERVICE_ACTIVE, null, 0, bytesNeeded, numberOfServices)) {
if (advapi32.EnumDependentServicesW (serviceHandle, Advapi32.SERVICE_ACTIVE, serviceStatuses[0], bytesNeeded.getValue(), bytesNeeded, numberOfServices)) {
for(int i = numberOfServices.getValue() - 1; i >= 0; i--){
logger.debug("Service Name: " + serviceStatuses[i].lpServiceName.toString());
}
}

如果有 2 个服务依赖项,我会在 logger.debug 调用中收到 lpServiceName 的 NullPointerException:

IntByReference bytesNeeded = new IntByReference();
IntByReference numberOfServices = new IntByReference();
Advapi32.ENUM_SERVICE_STATUS serviceStatus = new Advapi32.ENUM_SERVICE_STATUS();
Advapi32.ENUM_SERVICE_STATUS[] serviceStatuses = (Advapi32.ENUM_SERVICE_STATUS[]) serviceStatus.toArray(2);

if (!advapi32.EnumDependentServicesW(serviceHandle, Advapi32.SERVICE_ACTIVE, null, 0, bytesNeeded, numberOfServices)) {
if (advapi32.EnumDependentServicesW (serviceHandle, Advapi32.SERVICE_ACTIVE, serviceStatuses[0], bytesNeeded.getValue(), bytesNeeded, numberOfServices)) {
for(int i = numberOfServices.getValue() - 1; i >= 0; i--){
logger.debug("Service Name: " + serviceStatuses[i].lpServiceName.toString());
}
}

上面代码的 numberOfServices 值为 2,正如预期的那样。我试图传递结构体数组而不是指针,因为我希望 JNA 进行内存同步。我应该如何传递/使用结构数组?

最佳答案

根据docs对于 EnumDependentServices ,

lpServices [out, optional]

A pointer to an array of ENUM_SERVICE_STATUS structures that receives the name and service status information for each dependent service in the database. The buffer must be large enough to hold the structures, plus the strings to which their members point.

您几乎忽略了 bytesNeeded 报告的所需缓冲区大小。您应该使用bytesNeeded值(value)创造 Memory所需大小的实例,然后使用 Memory实例创建一个新的 ENUM_SERVICE_STATUS实例,而不是独立于所需的缓冲区大小创建结构。

关于java - 如何使用 JNA 传递结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com