Can't use FindWindow

345 views
Skip to first unread message

Guilherme Ritter

unread,
Mar 20, 2015, 7:12:54 PM3/20/15
to jna-...@googlegroups.com
Hi everyone.

I'm trying to make a program like a simplified version of ArtMoney (memory editor). I tried C/C++ and Java, and it seems to be easier (for me) with Java. I'm using JNA and I'm following stuff I've found online. However, it seems that everything I find online regarding JNA is for an older version of it.

Firstly, I downloaded jna.jar and jna-platform.jar from here and added it to my project. Then, I tried:

Pointer process = kernel32.OpenProcess(bla, bla, bla);

Everywhere it seems that OpenProcess returns a Pointer, while NetBeans showed it returns a HANDLE. After a lot of wall banging, I realized the solution was quite simple (thanks autocomplete):

Pointer process = kernel32.OpenProcess(bla, bla, bla).getPointer();

Now, I'm trying to use:

user32.GetWindowThreadProcessId(user32.FindWindowA(null, window), pid);

However, Netbeans says

cannot find symbol
  symbol
: method FindWindowA(<null>, String)
  location
: variable user32 of type User32

This is the name that I find everywhere on the internet. But NetBeans was saying it was wrong. Suspecting the method's name, I removed the "A" and found out that indeed that's what NetBeans considers correct. However, when I run the file ("Could not find the specified procedure" is a translation):

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'FindWindow': Could not find the specified procedure.

  at com.sun.jna.Function.<init>(Function.java:208)
  at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
  at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
  at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
  at com.sun.jna.Library$Handler.invoke(Library.java:199)
  at com.sun.proxy.$Proxy1.FindWindow(Unknown Source)
  at main
.Main.getProcessId(Main.java:23)
  at main
.Main.main(Main.java:34)
Java Result: 1

Suspecting that NetBeans was having trouble with the library, I tried compiling with the "A" back in the name. As I suspected:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
  at main
.Main.getProcessId(Main.java:23)
  at main
.Main.main(Main.java:34)
Java Result: 1

Then I went to the library's Javadocs. The correct method's name is "FindWindow", and I found no mention to a deprecated "FindWindowA" neither anything useful. Further searches resulted nothing.

Regarding the questions here under Community and Support:

• Windows 7 Ultimate / Intel Core i3-2100 / 64 bit
• I don't think it applies.
• I don't think it applies either.
• None.
• In the text above.

So, what do I do to make FindWindow work?

Thanks in advance.

Daniel Doubrovkine

unread,
Mar 21, 2015, 8:38:22 AM3/21/15
to jna-...@googlegroups.com
This is about ANSI/Wide functions in Windows. What is user32 in your code? Try using the default INSTANCE, eg. 

HWND h = User32.INSTANCE.FindWindow("OpusApp", null);


--
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.



--

Guilherme Ritter

unread,
Mar 21, 2015, 10:33:22 AM3/21/15
to jna-...@googlegroups.com
Thanks, it worked! I was using

static User32 user32 = (User32) Native.loadLibrary("user32", User32.class);

as in the tutorial I'm following, which is this one by the way. It's just weird that I was able to get the PID for "Solitaire", but not for "ePSXe - Enhanced PSX emulator". Must be the spaces. I tried escaping them and/or adding escaped quotes, but nothing worked. But it doesn't matter, now that it worked with "Solitaire", I was able to verify it returns the same PID as it appears on the Task Manager, so I don't really need this method anymore.

Everything else is working. I just need to find the right address now, as the addresses that I found with ArtMoney didn't worked (the one without the emulator offset provided by ArtMoney returned "Invalid Memory Access"; the one with the offset returned 0 instead of the correct value). But I think I can solve this.

Thanks again!

Timothy Wall

unread,
Mar 21, 2015, 6:41:48 PM3/21/15
to jna-...@googlegroups.com

> On Mar 20, 2015, at 7:12 PM, Guilherme Ritter <gui.a....@gmail.com> wrote:
>
>
> Firstly, I downloaded jna.jar and jna-platform.jar from here and added it to my project. Then, I tried:
>
> Pointer process = kernel32.OpenProcess(bla, bla, bla);
>
> Everywhere it seems that OpenProcess returns a Pointer, while NetBeans showed it returns a HANDLE. After a lot of wall banging, I realized the solution was quite simple (thanks autocomplete):
>
> Pointer process = kernel32.OpenProcess(bla, bla, bla).getPointer();
>

The dangers of programming by guessing. What type does MSDN say OpenProcess returns? Randomly converting it to your desired type using a method you find that just happens to convert to that type is *not* a good strategy.

NetBeans may be using its own version of JNA internally. You need to ensure that you don’t accidentally use theirs. There are version strings and functions within JNA you can use to verify which you’re using.


Reply all
Reply to author
Forward
0 new messages