Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import com.sun.jna.Native;
- import com.sun.jna.platform.win32.User32;
- import com.sun.jna.platform.win32.WinDef.HWND;
- import java.util.TimerTask;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class EnumerateWindows extends TimerTask{
- private static final int MAX_TITLE_LENGTH = 1024;
- //public static boolean exitStatus =false;
- /*
- * This Piece of code uses a Timer to schedule some TimerTask
- * The task itself is to get the active window
- * If the active window title matches the regular expression then the application is supposed to exit
- *
- * */
- @Override
- public void run() {
- char[] buffer = new char[MAX_TITLE_LENGTH * 2];
- HWND hwnd = User32.INSTANCE.GetForegroundWindow();
- User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
- System.out.println("Active window title: " + Native.toString(buffer));
- Pattern p = Pattern.compile("(sniff|wireshark|dump|network analyzer|packet analyzer|packet tracer|network tracer)");
- Matcher m = p.matcher(Native.toString(buffer));
- int start = 0;
- while (m.find(start)) {
- System.out.println("Match found: " + m.group(1) + " at position: " + m.start());
- if((m.group(1)=="dump") || (m.group(1) =="sniff") || (m.group(1)=="wireshark") || (m.group(1)=="analyzer")){
- //Code To exit the program
- }
- start = m.end();
- }
- }
- }
- ///This is piece of code that You need to put in any Initilizing method so as
- // To implement the Timertask of getting acitve window and exiting the application
- //It has been commented purposefuly cause the initilization method does not belong to this class under JavaFX
- /*
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- //EnumerateWindows.enumTitles();
- Timer timer = new Timer();
- timer.schedule(new EnumerateWindows(), 0, 5000);
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement