java 如何在Java中传递uint?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/844053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass uint in Java?
提问by omikun
I'm trying to call SendMessage with an uint parameter from Java, but I can't convert int to uint. I can't change the uint parameter in SendMessage because it is a windows function. Is there some way of doing this?
我正在尝试使用 Java 中的 uint 参数调用 SendMessage,但我无法将 int 转换为 uint。我无法更改 SendMessage 中的 uint 参数,因为它是一个 Windows 函数。有没有办法做到这一点?
Background: I'm actually using Processing, and I'm following the following tutorials to access user32.dll: http://processing.org/hacks/hacks:jnative
背景:我实际上在使用 Processing,我正在按照以下教程访问 user32.dll:http://processing.org/hacks/hacks: jnative
and http://jnative.free.fr/SPIP-v1-8-3/article.php3?id_article=8
和http://jnative.free.fr/SPIP-v1-8-3/article.php3?id_article=8
And I'm following this to call SendMessage http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx
我正在按照此调用 SendMessage http://www.codeproject.com/KB/cs/Monitor_management_guide.aspx
Here's my code, GetActiveWindow works fine, it's from the second link above.
这是我的代码,GetActiveWindow 工作正常,它来自上面的第二个链接。
int SC_MONITORPOWER = 0xF170;
int WM_SYSCOMMAND = 0x0112;
int MONITOR_ON = -1;
int MONITOR_OFF = 2;
int MONITOR_STANDBY = 1;
HWND ValidHWND = org.xvolks.jnative.util.User32.GetActiveWindow();
org.xvolks.jnative.util.User32.SendMessage(ValidHWND, (UINT)WM_SYSCOMMAND,
(WPARAM)SC_MONITORPOWER, (LPARAM)MONITOR_STANDBY);
last line is where the error happens, it says the expected UINT in type User32 is not applicable to the LONG I provided. I can't modify SendMessage either
最后一行是发生错误的地方,它表示 User32 类型中的预期 UINT 不适用于我提供的 LONG。我也不能修改 SendMessage
Here's the corresponding java file the call above invokes, the GetActiveWindow part works fine since it's from a tutorial. I'm trying to tailor Sendmessage to follow it, but I haven't figured it all out yet. However, I don't think it matters to the error I'm getting, since changing the parameters to SendMessage here doesn't change what the compiler expects. I've tried changing int Msg to uint Msg to long Msg, the compiler still expects uint from the code above.
这是上面调用调用的相应 java 文件,GetActiveWindow 部分工作正常,因为它来自教程。我正在尝试调整 Sendmessage 以遵循它,但我还没有完全弄清楚。但是,我认为这与我得到的错误无关,因为在此处将参数更改为 SendMessage 并不会改变编译器的预期。我已经尝试将 int Msg 更改为 uint Msg 到 long Msg,编译器仍然期望来自上述代码的 uint。
public class Paul_s_User32 extends User32 {
public HANDLE GetActiveWindow() {
JNative GetActiveWindow = new JNative(DLL_NAME, "GetActiveWindow");
GetActiveWindow.setRetVal(Type.INT);
GetActiveWindow.invoke();
HWND handle = new HWND(GetActiveWindow.getRetValAsInt());
GetActiveWindow.dispose();
return handle;
}
public IntPtr SendMessage(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam) {
JNative SendMessage = new JNative(DLL_NAME, "SendMessage");
//SendMessage.setRetVal(Type.long);
SendMessage.invoke();
SendMessage(ValidHWND, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_STANDBY);
}
采纳答案by jdigital
If you are using the org.xvolks.jnative package, all of the parameters (HWND, UINT, WPARAM, and LPARAM) are classes in org.xvolks.jnative.misc.basicStructures. So you should be able to do something like this (untested code):
如果您使用 org.xvolks.jnative 包,则所有参数(HWND、UINT、WPARAM 和 LPARAM)都是 org.xvolks.jnative.misc.basicStructures 中的类。所以你应该能够做这样的事情(未经测试的代码):
import org.xvolks.jnative.misc.basicStructures.*;
...
User32.SendMessage(
new HWND(ValidHWND),
new UINT(WM_SYSCOMMAND),
new WPARAM(SC_MONITORPOWER),
new LPARAM(MONITOR_STANDBY);
For your reference, here's the implementation of SendMessage in org.xvolks.jnative.util.User32. As you can see, the jnative code does not care at all about signed and unsigned. You might find it useful to grab the jnative source code.
供您参考,这里是 org.xvolks.jnative.util.User32 中 SendMessage 的实现。如您所见,jnative 代码根本不关心有符号和无符号。您可能会发现获取jnative 源代码很有用。
public static LRESULT SendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) throws NativeException, IllegalAccessException
{
JNative SendMessage = new JNative(DLL_NAME, "SendMessageA");
SendMessage.setRetVal(Type.INT);
int pos = 0;
SendMessage.setParameter(pos++, hWnd.getValue());
SendMessage.setParameter(pos++, Msg.getValue());
SendMessage.setParameter(pos++, wParam.getValue());
SendMessage.setParameter(pos++, lParam.getValue());
SendMessage.invoke();
pos = SendMessage.getRetValAsInt();
SendMessage.dispose();
return new LRESULT(pos);
}
And if you decide that you don't like JNative, you could take a look at Java Native Access (JNA).
如果您决定不喜欢 JNative,您可以查看Java Native Access (JNA)。
回答by jdigital
From a data perspective, a uint and an int are the same thing -- a 32 bit value. The only difference is how opcodes interpret the uppermost bit. When passing between Java and C, you're just passing 32 bits, so the sign is not relevant. The solution is to just declare the uint parameter as an int.
从数据的角度来看,uint 和 int 是同一个东西——一个 32 位的值。唯一的区别是操作码如何解释最高位。在 Java 和 C 之间传递时,您只是传递 32 位,因此符号不相关。解决方案是将 uint 参数声明为 int。
BTW, the issue of unsigned/signed is not significant here because the uMsg value for SendMessage is positive for all Microsoft messages, and for that matter, is more than likely positive for anything else you're likely to encounter.
顺便说一句,未签名/签名的问题在这里并不重要,因为 SendMessage 的 uMsg 值对于所有 Microsoft 消息都是正值,就此而言,对于您可能遇到的任何其他消息,它很可能是正值。

