Pregunta

There is my list of processes:

public class lab2 {
    public static void main(String args[]) {
        Kernel32 kernel32 = Kernel32.INSTANCE;
        User32 user32 = User32.INSTANCE;
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
        WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(
                Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
        char path[] = new char[512];
        HWND hWnd = User32.INSTANCE.GetWindowModuleFileName(hWnd, path, 512);

        try {
            while (kernel32.Process32Next(snapshot, processEntry)) {
                System.out.println(Native.toString(processEntry.szExeFile)
                        + "\t" + Native.toString(path));
            }
        } finally {
            kernel32.CloseHandle(snapshot);
        }
    }
}

I tried to set a variable path the full path to the file. I got an error @Type mismatch: cannot convert from int to WinDef.HWND@ in HWND hWnd = User32.INSTANCE.GetWindowModuleFileName(hWnd, path, 512); Where did I go wrong? How to do it right? Thank you.

¿Fue útil?

Solución

You're using the function wrong.

  • You're using the hWnd variable on the same line as you declare it.
  • And hWnd doesn't yet hold a reference to a viable window.
  • And I have no idea why you're trying to put the int returned into an HWND variable. This makes no sense, and is the source of your errors.
  • Again for the function to work, your HWND variable, hWnd needs to refer to a viable window handle. You may need to call another JNA function to get this handle.

e.g.,

  User32 user32 = User32.INSTANCE;
  char path[] = new char[512];

  long sleepTime = 2000;
  try {
     Thread.sleep(sleepTime);
  } catch (InterruptedException e) {}

  HWND hWnd = user32.GetForegroundWindow();
  user32.GetWindowModuleFileName(hWnd, path, 512);
  System.out.println("Foreground Window Module FileName: " + 
          Native.toString(path));

  user32.GetWindowText(hWnd, path, 512);
  System.out.println("Window text is: " + Native.toString(path));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top