B4J Question [SOLVED] Package Standalone Error (java 11)

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
I have this code
B4X:
'get windows open
        Dim JO As JavaObject
        Dim jo2 As JavaObject = JO.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE") '<<<<<<
        Dim hWnd As JavaObject = jo2.RunMethod("FindWindow", Array(Null, mapset.GetDefault("txtprg2titolo", "??"))) '
        countgen = 0
        Do While Not(hWnd.IsInitialized)
            Dim hWnd As JavaObject = jo2.RunMethod("FindWindow", Array(Null, mapset.GetDefault("txtprg2titolo", "??")))
            Sleep(0)
            If hWnd.IsInitialized Or countgen >= 8 Then Exit
        Loop
        If hWnd.IsInitialized Then
            Dim wu As JavaObject
            wu.InitializeStatic("com.sun.jna.platform.WindowUtils") '<<<<<<
            Dim awtrect As JavaObject = wu.RunMethod("getWindowLocationAndSize", Array(hWnd))
            Dim xpos As Int = awtrect.GetField("x") + mapset.Get("txtprg2x").As(Int)
            Dim ypos As Int = awtrect.GetField("y")+ mapset.Get("txtprg2y").As(Int)
            Dim w As Int = awtrect.GetField("width")
            Dim h As Int = awtrect.GetField("height")
            jo2.RunMethod("SetWindowPos", Array(hWnd, Null, xpos, ypos, w, h, 0))
        End If

In debug mode all work fine but with the compiled package the program crash.
Now I added this:
B4X:
#PackagerProperty: AdditionalModuleInfoString = uses com.sun.jna.platform.win32.User32;
#PackagerProperty: AdditionalModuleInfoString = uses com.sun.jna.platform.WindowUtils;
The program not crash but not work well.

Thanks for your advice.
 

micro

Well-Known Member
Licensed User
Longtime User
Most likely I need to add something in the build, definitely in #PackagerProperty:
surely I have to insert some correct reference to jna
but what?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
When I look at how much effort you have to put in to display a window and adjust its size and location, well, I think it proofs Erel's first statement to use a B4XPages solution:

Manipulating the form can be reduced to:
Resize window and change location:
Private Sub btnTest_Click
    Dim f As Form = B4XPages.GetNativeParent(Me)
    f.WindowWidth = f.WindowWidth * 0.8
    f.WindowHeight = f.WindowHeight * 0.8
    f.WindowTop = f.WindowTop +50
End Sub
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
When I look at how much effort you have to put in to display a window and adjust its size and location, well, I think it proofs Erel's first statement to use a B4XPages solution:
Surely you misinterpreted the code
I open an external program and intercept the window handle to move it, which is why I use jna.
The problem is in compiling as a standalone package in release mode, in debug work fine.
Surely I need to add references in #PackagerProperty:
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I open an external program and intercept the window handle to move it, which is why I use jna.
Oops ?, well ? maybe jna is also confused by the lack of the filename extension of the program to be started ??
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi
As per another thread where erel recommended using java's inline for using the jna library, (for same problem in compiling as a standalone package)
I am trying to fix it but I'm having problems with getWindowLocationAndSize function of com.sun.jna.platform.WindowUtils

Getting the window handle is no problem
B4X:
Sub TrovaWindow(titolo As String) As JavaObject
    Return Me.as(JavaObject).RunMethod("Find_Window", Array(titolo))
End Sub

#if Java
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.HWND;
public static Object Find_Window(String titolo)throws Exception {
HWND hwnd = User32.INSTANCE.FindWindow(null,titolo); // program name
return hwnd;
}
#End If
But as explained before with getWindowLocationAndSize I have problems
B4X:
Sub GetPosSizeWindow(handle As JavaObject) As JavaObject
    Return Me.as(JavaObject).RunMethod("GetInfoWindow", Array(handle))
End Sub

#if Java
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.WindowUtils;
public static Object GetInfoWindow(WinDef.HWND hwnd){
  Rectangle rect = getWindowLocationAndSize(hwnd); '<<<<<<error
 //Object rect = getWindowLocationAndSize(hwnd);
  return rect;
}
#End If

Thanks to all
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Fixed, now even compiled as a standalone package everything works fine using inline Java.

FindWindow
B4X:
Dim handle As JavaObject = TrovaWindow("tytle")

Sub TrovaWindow(titolo As String) As JavaObject
    Return Me.as(JavaObject).RunMethod("Find_Window", Array(titolo))
End Sub

#if Java
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.HWND;
public static Object Find_Window(String titolo)throws Exception {
HWND hwnd = User32.INSTANCE.FindWindow(null,titolo); // program name
return hwnd;
}
#End If

MoveWindow and possibly resize
B4X:
If handle.IsInitialized Then
    Sleep(0)
    Dim awtrec As JavaObject = GetPosSizeWindow(handle)
    Log(awtrec)
    If awtrec.IsInitialized Then
        Dim xpos As Int = awtrec.GetField("left") '+ xxxx For Move to x
        Dim ypos As Int = awtrec.GetField("top") ''+ yyyy For Move to y
        Dim width As Int = awtrec.GetField("right").As(Int)-awtrec.GetField("left").As(Int) 'actual size
        Dim height As Int = awtrec.GetField("bottom").As(Int)-awtrec.GetField("top").As(Int) 'actual size
        MuoviWindow(handle,xpos,ypos,width,height,False)
    End If
End If

Sub GetPosSizeWindow(handle As JavaObject) As JavaObject
    Return Me.as(JavaObject).RunMethod("GetInfoWindow", Array(handle))
End Sub

Sub MuoviWindow(handle As JavaObject, x As Int, y As Int, w As Int, h As Int, refresh As Boolean) As Boolean
    Return Me.as(JavaObject).RunMethod("MoveWindow", Array(handle,x,y,w,h,refresh))
End Sub

#if Java
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;
public static RECT GetInfoWindow(WinDef.HWND hwnd){
try{
  RECT rect = new RECT();
  User32.INSTANCE.GetWindowRect(hwnd, rect);
             return rect;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
}
#End If

#if Java
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;
public static boolean MoveWindow(WinDef.HWND hwnd, int x, int y, int w, int h, boolean refresh){
try{
  User32.INSTANCE.MoveWindow(hwnd, x, y, w, h, refresh);
             return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
}
#End If

Thanks to all.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…