1

apologies in advance as I'm a novice.

I'm trying to work with a windows based game called rFactor with java and the JNA library. I have seen what I want to do by people using c++.

So far I have this (I copied the bulk of it from another stackoverflow post Java search for on-screen text field):

package au.gov.nsw.lpi.bds.jnatest;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.examples.win32.W32API.LPARAM;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public class IterateChildWindows {
public interface User32 extends StdCallLibrary {
    User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);

    int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);

    boolean FindWindowEx(HWND parent, HWND child, String className, String window);

    boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);

    boolean EnumChildWindows(HWND parent, WNDENUMPROC callback, LPARAM info);

    interface WNDENUMPROC extends StdCallCallback {
        boolean callback(HWND name, Pointer arg);
    }

    int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);

    int GetClassNameA(HWND in, byte[] lpString, int size);
}

public static void main(String[] args) {
    User32.INSTANCE.EnumWindows(new User32.WNDENUMPROC() {
        public boolean callback(HWND hWnd, Pointer userData) {
            byte[] textBuffer = new byte[512];
            User32.INSTANCE.GetWindowTextA(hWnd, textBuffer, 512);
            String wText = Native.toString(textBuffer);

            if (wText.contains("ISI Dedicated Server")) {

                if (User32.INSTANCE.FindWindowEx(hWnd, null, "Static", "Game Name:")) {
                    System.out.println(new String(textBuffer).trim() + " - " + hWnd);

                    User32.INSTANCE.EnumChildWindows(hWnd, new User32.WNDENUMPROC() {
                        int count = 1;

                        public boolean callback(HWND hWnd, Pointer userData) {
                            byte[] textBuffer = new byte[512];
                            User32.INSTANCE.GetClassNameA(hWnd, textBuffer, 512);

                            if ((new String(textBuffer).trim()).contains("Edit")) {
                                System.out.println(new String(textBuffer).trim());
                                System.out.println(hWnd);

                                // User32.INSTANCE.SendMessage(hWnd,
                                // WM_SETTEXT, msg.length(), (LPARAM) msg);
                            }

                            if ((new String(textBuffer).trim()).contains("Button")) {
                                if (count == 8) {
                                    System.out.println(new String(textBuffer).trim() + " " + count);
                                    System.out.println(hWnd);
                                }
                                count++;
                            }

                            return true;
                        }
                    }, null);
                }
            }

            return true;
        }
    }, null);
}
}

This produces

ISI Dedicated Server - native@0x207e0 (com.sun.jna.examples.win32.W32API$HWND@207e0)
Edit
native@0x207ac (com.sun.jna.examples.win32.W32API$HWND@207ac)
Button 8
native@0x207a8 (com.sun.jna.examples.win32.W32API$HWND@207a8)

I think I'm on the right track as I've isolated the right "EDIT" and "BUTTON" that I need. I believe I now need to put text into the edit field and then use the button using

int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);

This is an example from c++

SendMessage(chatHwnd, WM_SETTEXT, msgSB.Length, msgSB)

I've tried but had no luck getting it to work. Any help would be appreciated.

Edit, I've tried this (though I'm pretty much fumbling in the dark)

int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam);

and this

public boolean callback(HWND hWnd, Pointer userData) {
    byte[] textBuffer = new byte[512];
User32.INSTANCE.GetClassNameA(hWnd, textBuffer, 512);
    if ((new String(textBuffer).trim()).contains("Edit")) {
    User32.INSTANCE.SendMessage(hWnd,0xC, msg.length, 
            Native.toByteArray("MessageFromNick"));
                            }

if ((new String(textBuffer).trim()).contains("Button")) {
    if (count == 8) {
    User32.INSTANCE.SendMessage(hWnd, 0x0201, 0, 0);
}

count++;
}
return true;
}
3
  • What have you tried, and what exactly didn't work? You'll need to use Native.toByteArray(String) to obtain a properly null-terminated array of bytes (or array of char if you're using the unicode version of SendMessage).
    – technomage
    Sep 2, 2013 at 13:53
  • Thanks for answering. I edited the original post to contain the things I have tried. Regards, Nick
    – marcovanb
    Sep 4, 2013 at 2:16
  • I got it to work with this thanks.
    – marcovanb
    Sep 4, 2013 at 5:32

2 Answers 2

0

User32.INSTANCE.SendMessageA(hWnd, 0x000C, 0, Native.toByteArray("TextMessage"));

0x000C refers to WM_SETTEXT

1
  • Please provide the correct interface declaration and mark this as the answer @user2738572 Jul 6, 2016 at 21:21
0

Interface:

static Map UNICODE_OPTIONS = new HashMap() {
    {
        put("type-mapper", com.sun.jna.win32.W32APITypeMapper.UNICODE);
        put("function-mapper", com.sun.jna.win32.W32APIFunctionMapper.UNICODE);
    }
};

public interface UUser32 extends StdCallLibrary {
    UUser32 INSTANCE = (UUser32)(UUser32)Native.load("user32", UUser32.class, UNICODE_OPTIONS);

    int SendMessage(com.sun.jna.platform.win32.WinDef.HWND hWnd, int msg, int wParam, char[] lParam);
}

Call:

static final int WM_SETTEX = 0x000C;//from winuser.h
static public void setEditText(com.sun.jna.platform.win32.WinDef.HWND windowH, com.sun.jna.platform.win32.WinDef.HWND editH, String text){
    user32.SetForegroundWindow(windowH);//user32 is User32.INSTANCE defined in com.sun.jna.platform.win32.User32;
    char[] ctext = Native.toCharArray(text);
    UUser32.INSTANCE.SendMessage(editH, WM_SETTEX, 0, ctext);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.