如何解析 JNA 中的 DWORD
我正在尝试使用 JNA 查询 Java 中 Windows 服务的状态。我正在使用以下 Windows API 函数:
QuerySerivceStatusEx( SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
LPBYTE lpBuffer
内部是一个指向结构的指针。在结构内部,它将当前状态存储为 DWORD 形式。根据 JNA 文档,DWORDs
在 Java 中映射为 ints
,并且根据 WinSvc.h,与运行关联的 DWORD
是 0x00000004
code> 因此,在我的代码中,我定义了一个值为 0x00000004
的最终 int,就像
public static final int SERVICE_RUNNING = 0x00000004
当我运行代码并且我正在查询的服务正在运行时,我将返回一个值 16
code> 在 WinSvc.h 中根本没有定义。我缺少某种翻译吗?
编辑:这里要澄清的是服务可以处于的所有状态及其相关值:
public static final int SERVICE_STOPPED = 0x00000001;
public static final int SERVICE_START_PENDING = 0x00000002;
public static final int SERVICE_STOP_PENDING = 0x00000003;
public static final int SERVICE_RUNNING = 0x00000004;
public static final int SERVICE_CONTINUE_PENDING = 0x00000005;
public static final int SERVICE_PAUSE_PENDING = 0x00000006;
public static final int SERVICE_PAUSED = 0x00000007;
I'm trying to query the status of a Windows service in Java using JNA. I'm using the following Windows API function:
QuerySerivceStatusEx( SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Inside the LPBYTE lpBuffer
is a pointer to a structure. And inside the structure it stores the current state as a DWORD
. According to JNA documentation DWORDs
map into ints
in Java, and according to WinSvc.h the DWORD
associated with running is 0x00000004
so in my code I defined a final int with value 0x00000004
like
public static final int SERVICE_RUNNING = 0x00000004
When I run the code and the service I'm querying is running I'm getting back a value of 16
which isn't defined at all in WinSvc.h. Is there some kind of translation I'm missing?
EDIT: To clairfy here are all the states a service can be in and their associated values:
public static final int SERVICE_STOPPED = 0x00000001;
public static final int SERVICE_START_PENDING = 0x00000002;
public static final int SERVICE_STOP_PENDING = 0x00000003;
public static final int SERVICE_RUNNING = 0x00000004;
public static final int SERVICE_CONTINUE_PENDING = 0x00000005;
public static final int SERVICE_PAUSE_PENDING = 0x00000006;
public static final int SERVICE_PAUSED = 0x00000007;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终弄清楚了我的问题。无论服务是启动还是停止,都会返回 16。事实证明我没有返回结构, SERVICE_STATUS_PROCESS,按照与本机库中定义的顺序相同的顺序定义。我不知道需要这样做,但您需要按照它们在本机代码中的顺序定义结构。
Ended up figuring out my problem. 16 was being returned regardless of whether the service was started or stopped. As it turns out I didn't have the structure being returned, SERVICE_STATUS_PROCESS, defined in the same order that it is defined in the native library. I was unaware this needed to be done, but you need to define your structures in the same order that they are in in the native code.
像这样的值通常是许多标志值或组合在一起。例如,要同时指示
4
和2
标志,您将得到4 | 2
或6
。然后,您可以通过将结果 (6
) 与每个标志相加并查看其是否为真(6 & 4
或6 & 2
为真,但6 & 1
则不然,所以您知道标志4
和2
是设置但未设置1
)。话虽这么说,16 将是一个基本值(它是 2 的幂),所以我不确定。
Values like that are often many flag values or'ed together. e.g., to indicate both a
4
and a2
flag, you would get4 | 2
or6
. You could then figure out which flags are set by anding together the result (6
) with each of the flags and seeing if it's true (6 & 4
or6 & 2
would be true, but6 & 1
would not be, so you know flags4
and2
are set but not1
).That being said, 16 would be a basic value (it's a power of 2), so I'm unsure.