Hello!

There is a program written in JAVA (a module launched as an add-in simultaneously with a third-party application).

From this java-program (add-on), you need to change the title of the windows of a third-party program.

I have good programming skills in java, but have not yet encountered similar tasks. I know that there are JNI and JNA and a bunch of third-party libraries, but in this area I am not experienced yet.

Without problems, I can solve this problem in C ++ or Delphi.

But the task is to make the change of the header of another program using JAVA .

The bosses really do not want to produce modules written in the project that are not completely written in java (if nothing else is done, they will put up with it) .

I consider options for catching handle, window headers, id process, window class ... God! Yes, somehow, the main thing to work!

I’m happy to accept tips, links, comments, and code snippets from you.

It would be great if someone shares the source of a similar task.

Examples with JNA, JNI will accept with joy!

  • one
    Java Access Bridge already watched? - Igor Kudryashov
  • @Igor, thank you, I read ... - Vitaly Vikhlyaev
  • one
    I'm sorry, I think I deceived you. Access Bridge is rather the other way - from Windows to Java, and you need the opposite. Obviously, without JNI / JNA you can't do here and here there are examples of reading the window title. As I understand the Windows API you own, so can fix for your needs. - Igor Kudryashov
  • @Igor Kudryashov thanks for the JNI / JNA examples, I'm just trying to figure out this technology ... I read the Internet, everywhere they write that in java, only through JNI you can steer windows under windows ... - Vitaly Vihlyaev

1 answer 1

Of course, JNA is your choice. You need to download two JARs here , or connect via maven. Here is an example sketched:

import com.sun.jna.Native; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.win32.StdCallLibrary; import com.sun.jna.win32.W32APIOptions; public class SetWindowTitle { public static interface User32 extends StdCallLibrary { final User32 instance = (User32) Native.loadLibrary ("user32", User32.class, W32APIOptions.UNICODE_OPTIONS); HWND FindWindow(String className, String windowName); boolean SetWindowText(HWND hwnd, String newText); } public static void main(String[] args) { if(args.length != 2) { System.out.println("Нужно два аргумента: имя окна (или класс) и новое имя"); return; } HWND hwnd = User32.instance.FindWindow(args[0], null); if(hwnd == null) { hwnd = User32.instance.FindWindow(null, args[0]); } if(hwnd == null) { System.out.println("Окно не найдено"); return; } System.out.println("Нашли окно: "+hwnd); boolean result = User32.instance.SetWindowText(hwnd, args[1]); System.out.println(result ? "Успешно" : "Упс"); } } 

Compile:

 javac -cp jna-4.1.0.jar;jna-platform-4.1.0.jar;. SetWindowTitle.java 

Run

 java -cp jna-4.1.0.jar;jna-platform-4.1.0.jar;. SetWindowTitle "old-title" "new-title" 

JNA works very comfortably. You simply declare methods in your interface with the same names as in user32 and with a compatible signature. Internally, JNA creates a proxy class that, for each method, searches for the corresponding inside user32.dll and appropriately converts the arguments.