如何解析 JNA 中的 DWORD

发布于 2024-11-05 01:36:00 字数 1192 浏览 4 评论 0原文

我正在尝试使用 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,与运行关联的 DWORD0x00000004 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此生挚爱伱 2024-11-12 01:36:00

最终弄清楚了我的问题。无论服务是启动还是停止,都会返回 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.

奶茶白久 2024-11-12 01:36:00

像这样的值通常是许多标志值或组合在一起。例如,要同时指示 42 标志,您将得到 4 | 26。然后,您可以通过将结果 (6) 与每个标志相加并查看其是否为真(6 & 46 & 2 为真,但 6 & 1 则不然,所以您知道标志 42 是设置但未设置1)。

话虽这么说,16 将是一个基本值(它是 2 的幂),所以我不确定。

Values like that are often many flag values or'ed together. e.g., to indicate both a 4 and a 2 flag, you would get 4 | 2 or 6. 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 or 6 & 2 would be true, but 6 & 1 would not be, so you know flags 4 and 2 are set but not 1).

That being said, 16 would be a basic value (it's a power of 2), so I'm unsure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文