-2

OS: Windows 7

Can you please tell how to automatically identify the java-program running in the background, which window is active Windows now?

Translated by GoogleTranslate

TorchTT
  • 132
  • 2
  • 10
  • 1
    Learn how to use `win32 API` with java. For more information take a look at this [question](http://stackoverflow.com/q/6391439/776084). – RanRag May 22 '12 at 07:11
  • 1
    Why downvoting ? It's not the worse question I've seen answered in here. TorchTT please explain what have you tried so far. – BigMike May 22 '12 at 07:13

1 Answers1

1

RanRag, thanks for the link

To determine which program belongs to the active window, you must:

1) Download jna.jar and platform.jar from the JNA - Download page

2) Connect the library to the project

3) Add the code:

private static class Psapi {
        static { Native.register("psapi"); }
        public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
    }

    private static class Kernel32 {
        static { Native.register("kernel32"); }
        public static int PROCESS_QUERY_INFORMATION = 0x0400;
        public static int PROCESS_VM_READ = 0x0010;
        public static native int GetLastError();
        public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);
    }

    private static class User32DLL {
        static { Native.register("user32"); }
        public static native int GetWindowThreadProcessId(WinDef.HWND hWnd, PointerByReference pref);
        public static native WinDef.HWND GetForegroundWindow();
        public static native int GetWindowTextW(WinDef.HWND hWnd, char[] lpString, int nMaxCount);
    }

    private static final int MAX_TITLE_LENGTH = 1024;

    public void nameApplication() {

        Psapi ps = new Psapi();
        Kernel32 kr = new Kernel32();
        User32DLL us = new User32DLL();

        char[] buffer = new char[MAX_TITLE_LENGTH * 2];
        String nameApp;

        us.GetWindowTextW(us.GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);
        System.out.println("Active window title: " + Native.toString(buffer));

        PointerByReference pointer = new PointerByReference();
        us.GetWindowThreadProcessId(us.GetForegroundWindow(), pointer);
        Pointer process = kr.OpenProcess(kr.PROCESS_QUERY_INFORMATION | kr.PROCESS_VM_READ, false, pointer.getValue());
        ps.GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
        System.out.println("Active window process: " + Native.toString(buffer););

    }
Community
  • 1
  • 1
TorchTT
  • 132
  • 2
  • 10