B4J Question [SOLVED] Turn Off Display - programmatically / from C++ to Java, more specific javaobject

Magma

Expert
Licensed User
Longtime User
Hi there...

i need to turn off the display (monitor) of a pc programmatically...

Well the code in C++ is the following:
B4X:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    // Turn off monitor
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

..tried to use jna...
but as always (just a hole in the water) - i am not much java guy...

as far... but nothing at all... :-(
B4X:
    Dim user32 As JavaObject
    user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
    Dim ahwnd As JavaObject
    ahwnd=ahwnd.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.HWND","0xffff")
    Dim awmsys As JavaObject
    awmsys=awmsys.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.WPARAM","0xf170")
    Dim scmon As JavaObject
    scmon=scmon.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.LPARAM","0x112S")
    user32.RunMethod("SendMessage",Array(ahwnd,scmon,awmsys,2))

Can anyone help on that conversion of code..?
 

sfsameer

Well-Known Member
Licensed User
Longtime User
Hi there...

i need to turn off the display (monitor) of a pc programmatically...

Well the code in C++ is the following:
B4X:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    // Turn off monitor
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

..tried to use jna...
but as always (just a hole in the water) - i am not much java guy...

as far... but nothing at all... :-(
B4X:
    Dim user32 As JavaObject
    user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
    Dim ahwnd As JavaObject
    ahwnd=ahwnd.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.HWND","0xffff")
    Dim awmsys As JavaObject
    awmsys=awmsys.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.WPARAM","0xf170")
    Dim scmon As JavaObject
    scmon=scmon.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.LPARAM","0x112S")
    user32.RunMethod("SendMessage",Array(ahwnd,scmon,awmsys,2))

Can anyone help on that conversion of code..?
Hello,
there is a better and easier approach, using CMD batch file :

Create a CMD file (.bat)

Put the following code :
CMD:
%systemroot%\system32\scrnsave.scr /s

and run it using jShell library and you are good to go :)

Thank you,
Saif
 
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
@sfsameer Thank you - but this is something different... this is just screensaver... .... monitor off is different.... and ofcourse is better to have it with code..
This is to turn off the display by changing the screen timeout :

Put it in a .bat file and run it using jShell

CMD:
powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2)

:)
 
Last edited:
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
Ok....... what about this in javaobject :)

??? heh (because the powershell is not standard to all windows versions) :)
To be honest am not sure because windows is always easier to control unlike Mac OS and other platforms :)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
About the C++ code, find a JNA based code that does what you want and I'll help you port it.
Java:
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;

public class TurnOffMonitor {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int SC_MONITORPOWER = 0xF170;
      int SC_MONITOR_OFF = 2;
      int SC_MONITOR_ON = -1;

      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
            LPARAM paramLPARAM);

      LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
            LPARAM paramLPARAM);
   }

   private static final long SLEEP_TIME = 4 * 1000; // 4 seconds

   public static void main(String[] args) {
      final User32 user32 = User32.INSTANCE;
      System.out.println("Foo");

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_OFF));

      try {
         Thread.sleep(SLEEP_TIME);
      } catch (InterruptedException e) {}

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_ON));

   }
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#AdditionalJar: jna-5.0.0 
#AdditionalJar: jna-platform-5.0.0 
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI 
    Private Button1 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Dim jo As JavaObject
    jo.InitializeNewInstance("b4j.example.main$TurnOffMonitor", Null)
    jo.RunMethod("turnOff", Null)
    Sleep(3000)
    jo.RunMethod("turnOn", Null)
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub

#if JAVA
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;

public static class TurnOffMonitor {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int SC_MONITORPOWER = 0xF170;
      int SC_MONITOR_OFF = 2;
      int SC_MONITOR_ON = -1;

      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
            LPARAM paramLPARAM);

      LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
            LPARAM paramLPARAM);
   }

   private static final long SLEEP_TIME = 4 * 1000; // 4 seconds
    
   public static void turnOff () {
      final User32 user32 = User32.INSTANCE;

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_OFF));
   }
   
     public static void turnOn () {
      final User32 user32 = User32.INSTANCE;
      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_ON));
   }
   
   
  }
#End If
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
...A silly question about Java and JavaObject...

well...

if i have javaobjects with the same using library at Java (#If JAVA) + why JavaObject not working properly... for example:

B4X:
...
    Dim user32 As JavaObject
    user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
    Dim hwnd1 As JavaObject = user32.RunMethod("GetForegroundWindow",Null)
    Sleep(400)
    Dim Rect1 As JavaObject
    Rect1.InitializeNewInstance("com.sun.jna.platform.win32.WinDef.RECT",Null)
    Sleep(300)
    user32.runmethod("GetWindowRect",Array(hwnd1,Rect1))
    Sleep(300)

    LeftPixel=Rect1.GetField("left")
    TopPixel=Rect1.GetField("top")
    WidthPixel=(Rect1.GetField("right")-Rect1.GetField("left"))
    HeightPixel=(Rect1.GetField("bottom")-Rect1.GetField("top"))

    'if i log values getting "0" or -1 or 1 without IF JAVA working good... :-(
....
#if JAVA
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;

public static class TurnOffMonitor {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int SC_MONITORPOWER = 0xF170;
      int SC_MONITOR_OFF = 2;
      int SC_MONITOR_ON = -1;

      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
            LPARAM paramLPARAM);

      LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
            LPARAM paramLPARAM);
   }

   private static final long SLEEP_TIME = 4 * 1000; // 4 seconds
   
   public static void turnOff () {
      final User32 user32 = User32.INSTANCE;

      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_OFF));
   }
  
     public static void turnOn () {
      final User32 user32 = User32.INSTANCE;
      user32.SendMessageA(WinUser.HWND_BROADCAST, WinUser.WM_SYSCOMMAND,
            User32.SC_MONITORPOWER, new LPARAM(User32.SC_MONITOR_ON));
   }
  
  
  }
 

#End If

my mistake those "{}}}}".. things :)
 
Last edited:
Upvote 0
Top