0
votes

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

SendMessage( HWND_BROADCAST , WM_SETTINGCHANGE , 0 , (LPARAM) "Environment" );

JNA and windows xp: call to notify that Environment has been changed

see link: twall.github.com/jna/3.5.1/javadoc/

see link: 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)

This function places a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message.

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

How to pass String parameter "Environment" to user32.PostMessage ???

And not to get Exception in thread "main" java.lang.IllegalArgumentException: Argument value 0x6d6e6f7269766e45 exceeds native capacity (4 bytes) mask=0xffffffff00000000

Thx

1

1 Answers

2
votes

You're getting that error because you're trying to write a 64-bit value (myPointer.getLong(0)) into a 32-bit container (LPARAM).

You already have the pointer value you need for the LPARAM in myPointer; the recommended way to "cast" the pointer to LPARAM is to simply declare a version of PostMessage which takes an appropriately-typed fourth argument, e.g.

void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, Pointer lParam);
void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, String lParam);

This is preferable and more type-safe than manually converting between disparate types (i.e. from String or Pointer to an integer type).