Workstation Lock / Unlock listener

1,708 views
Skip to first unread message

Ladislav Gallay

unread,
May 22, 2014, 7:14:05 AM5/22/14
to jna-...@googlegroups.com
Hi,

I need to detect when the workstation (windows 7) is locked or unlocked in Java. I'm new to JNA.

So far I discovered:

 
JFrame myCustomWindow = new JFrame();
myCustomWindow.setVisible(true);
 
HWND hwnd = new HWND();
hwnd.setPointer(Native.getWindowPointer(myCustomWindow));
 
Wtsapi32 wtsapi32 = (Wtsapi32) Native.loadLibrary("Wtsapi32", Wtsapi32.class);
wtsapi32.WTSRegisterSessionNotification(hwnd, Wtsapi32.NOTIFY_FOR_THIS_SESSION);


But I don't know how to listen to the event. What I expect is to call methods like:

public void ScreenLocked()
{
      System.out.println("Screen is locked");
}
 
public void ScreenUnocked()
{
      System.out.println("Screen is unlocke");
}

Can anyone help me please? 

Timothy Wall

unread,
May 22, 2014, 7:32:29 AM5/22/14
to jna-...@googlegroups.com
In most cases when listening for windows events, you need to run an additional event pump within your Java code. See the low-level keyboard/mouse hooks for examples.

I’m not entirely sure why you need an additional event pump (presumably the JVM is already running one to handle UI events), maybe someone with some knowledge of the win32 internals arcana could speak to that.
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

wolf....@gmx.net

unread,
May 22, 2014, 10:15:30 AM5/22/14
to jna-...@googlegroups.com
Hi there, you are lucky! There is a full example showing you lock, unlock, logout and all events you may need from a windows workstation. As you can remote connect, windows provides more events and not only th eone you need.

check that stuff:

com.sun.jna.platform.win32.Win32WindowDemo

it`s a java main class. Just run it maybe from eclipse. Lovk your workstation and unlock it and you see console output. Just adopt the code with you implementation and overide the code:

public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WinUser.WM_CREATE: {
onCreate(wParam, lParam);
return new LRESULT(0);
}
case WinUser.WM_DESTROY: {
User32.INSTANCE.PostQuitMessage(0);
return new LRESULT(0);
}
case WinUser.WM_SESSION_CHANGE: {
this.onSessionChange(wParam, lParam);
return new LRESULT(0);
}
case WinUser.WM_DEVICECHANGE: {
LRESULT lResult = this.onDeviceChange(wParam, lParam);
return lResult != null ? lResult :
User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);
}
default:
return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);

wolf....@gmx.net

unread,
May 22, 2014, 10:25:30 AM5/22/14
to jna-...@googlegroups.com
Just another note for your`re sample below. I`m just talking about Swing windowing theory.
With a Swing window like JFrame it would not work, because swing uses a lightweight model. Means the native window pointer you usually should not use, but there are ways to get it ;-), is only existing as long as the window is active. In my example I`m creating a new native windows with native code, because I wanted to make it indepented from the window a user has on the screen. Please take that in mind, but I don`t know your usecase in detail.
But I was working on a similar usecase recently and you can ask me if your need answers.


Am Donnerstag, 22. Mai 2014 13:14:05 UTC+2 schrieb Ladislav Gallay:

Ladislav Gallay

unread,
May 22, 2014, 11:31:11 AM5/22/14
to jna-...@googlegroups.com
Hi, thank you very much!

The code looks great. The use case is: TimeTracker - user clicks the button with some event name and it program will store current time as start time for that event. And later user can finish the item and program will store current time as finish time. And when user is inactive (his screen is locked) program should track somthing like "Away"or "Idle". It's pretty simple.
There will be only one window so it the pointer can be Swing window JFrame, but as you say independet solution would be better.

Meanwhile I figured out my own working solution (after 4 hours of browsing :D ):

public interface MyListener extends StdCallCallback
{
public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
}

public interface MyUser32 extends User32
{
    public static final MyUser32 MYINSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS);
    public int SetWindowLong(WinDef.HWND hWnd, int nIndex, Callback callback);
}

public class LockHook
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);

HWND hwnd = new HWND();
hwnd.setPointer(Native.getWindowPointer(frame));

Wtsapi32.INSTANCE.WTSRegisterSessionNotification(hwnd, Wtsapi32.NOTIFY_FOR_ALL_SESSIONS);

MyListener listener = new MyListener()
{
@Override
public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WinUser.WM_SESSION_CHANGE)
{
switch (wParam.intValue())
{
case Wtsapi32.WTS_SESSION_LOCK:
System.out.println("Locked " + new Date());
break;
case Wtsapi32.WTS_SESSION_UNLOCK:
System.out.println("Unlocked "  + new Date());
break;
}
}
return User32.INSTANCE.DefWindowProc(hWnd, uMsg, wParam, lParam);
}
};

MyUser32.MYINSTANCE.SetWindowLong(hwnd, MyUser32.GWLP_WNDPROC, listener);

// Wtsapi32.INSTANCE.WTSUnRegisterSessionNotification(hwnd);
}
}

So maybe this will also help someone who is new in this topic :)

I'm going to take a look at com.sun.jna.platform.win32.Win32WindowDemo.

Thank you very much for your help. Have a nice day


Dňa štvrtok, 22. mája 2014 16:25:30 UTC+2 wolf....@gmx.net napísal(-a):

Kai Tam

unread,
Oct 12, 2015, 2:08:43 PM10/12/15
to Java Native Access
Hi, According to above code its just listening to the  windows lock / unlock session, but how to use it to unlock that work station? I've tried to used advapi32.instance.logonUser() but this way it only authenticate the user it doesnt visually unlock the windows. 

Would u happen to know how? please urgent for 4th year project. Thank you 

Ladislav Gallay

unread,
Oct 12, 2015, 2:50:20 PM10/12/15
to Java Native Access
Hello,

I dont know and I doubt it is possible due to security issues.
Reply all
Reply to author
Forward
0 new messages