0

http://www.codeguru.com/cpp/wp/win32/tutorials/article.php/c10849/Setting-a-System-Environment-Variable.htm

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

如何将字符串参数“环境”传递给 user32.PostMessage ???

并且不在线程“main”java.lang.IllegalArgumentException 中获得异常:参数值 0x6d6e6f7269766e45 超出本机容量(4 个字节)掩码 = 0xffffffff00000000

谢谢

4

1 回答 1

2

您收到该错误是因为您尝试将 64 位值 ( myPointer.getLong(0)) 写入 32 位容器 ( LPARAM)。

您已经有了 in 所需的指针LPARAMmyPointer;“强制转换”指针的推荐方法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

于 2013-03-25T12:23:41.853 回答