SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)“环境”);
JNA和windows xp:调用以通知环境已更改
见链接:twall.github.com/jna/3.5.1/javadoc /
见链接:twall.github.com/jna/3.5.1/javadoc/com/sun/jna/platform/win32/User32.html
PostMessage(WinDef.HWND hWnd,int msg,WinDef.WPARAM wParam,WinDef.LPARAM lParam)
此函数将消息放入与创建指定窗口的线程关联的消息队列中,然后返回而不等待线程处理消息。
import com.sun.jna.*;
import com.sun.jna.win32.*;
import com.sun.jna.platform.win32.*;
import com.sun.jna.ptr.*;
public class MainJNA {
public static void main (String [] args){
String myString = "Environment";
Pointer myPointer = new Memory(myString.length()+1);
myPointer.setString(0,myString);
Pointer HWND_BROADCAST = new Pointer(0xFFFF);
int msg = 0x001A; // WM_SETTINGCHANGE = WM_WININICHANGE = 0x001A
WinDef.HWND hWnd = new WinDef.HWND( HWND_BROADCAST );
WinDef.WPARAM wParam = new WinDef.WPARAM(0);
WinDef.LPARAM lParam = new WinDef.LPARAM( myPointer.getLong(0) );
// Exception in thread "main" java.lang.IllegalArgumentException:
// Argument value 0x6d6e6f7269766e45 exceeds native capacity (4 bytes)
// mask=0xffffffff00000000
User32 user32 = (User32) Native.loadLibrary(
"user32" , User32.class , W32APIOptions.DEFAULT_OPTIONS );
user32.PostMessage( hWnd , msg , wParam , lParam );
}
} // end of class MainJNA
如何将String参数“Environment”传递给user32.PostMessage ???
并且不要在线程“main”中获取异常java.lang.IllegalArgumentException:参数值0x6d6e6f7269766e45超出本机容量(4字节)mask = 0xffffffff00000000
THX
答案 0 :(得分:2)
您收到该错误是因为您尝试将64位值(myPointer.getLong(0)
)写入32位容器(LPARAM
)。
您已经拥有LPARAM
中myPointer
所需的指针值;将指针“强制转换”为LPARAM
的推荐方法是简单地声明PostMessage
的版本,该版本采用适当类型的第四个参数,例如
void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, Pointer lParam);
void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, String lParam);
与在不同类型之间手动转换(即从String
或Pointer
到整数类型)相比,这是更好的,更安全类型。