I want to read the text contents of the command prompt window. Let's say, I opened a command prompt, then ran a dir
command and then pwd
command. So the problem statement is that, what ever is present in the command prompt I should be able to read them. I am trying to use Java Native Access library for achieving this, but didn't get any luck with it. I have tried following code. But I am not getting any output.
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.WPARAM;
public class NativeExtractor {
public static void main(String ar[]) throws InterruptedException {
System.out.println(System.getProperty("sun.arch.data.model"));
executeNativeCommands();
}
public static void executeNativeCommands(){
User32 user32 = User32.INSTANCE;
//HWND notePadHwnd = user32.FindWindowA("Notepad",null );
HWND consoleHwnd = user32.FindWindowA("ConsoleWindowClass",null );
HWND editHwnd = user32.FindWindowExA(consoleHwnd, null, null, null);
byte[] lParamStr = new byte[512];
LRESULT resultBool = user32.SendMessageA(editHwnd, User32.WM_GETTEXT, 512, lParamStr);
System.out.println("The content of the file is : " + Native.toString(lParamStr));
}
interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
int WM_SETTEXT = 0x000c;
int WM_GETTEXT = 0x000D;
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
HWND FindWindowA(String lpClassName, String lpWindowName);
HWND FindWindowExA(HWND hwndParent, HWND hwndChildAfter, String lpClassName, String lpWindowName);
LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM);
LRESULT SendMessageA(HWND editHwnd, int wmGettext, long l, byte[] lParamStr);
int GetClassNameA(HWND hWnd, byte[] lpString, int maxCount);
void EnumChildWindows(HWND hwnd, WNDENUMPROC microsoft_word_document, Object o);
}
}
Nevertheless, I can read the text of the notepad using below. But things are not working for command prompt . Please help me in resolving this .
public static void executeNativeCommands(){
User32 user32 = User32.INSTANCE;
HWND notePadHwnd = user32.FindWindowA("Notepad",null );
HWND editHwnd = user32.FindWindowExA(notePadHwnd, null, null, null);
byte[] lParamStr = new byte[512];
LRESULT resultBool = user32.SendMessageA(editHwnd, User32.WM_GETTEXT, 512, lParamStr);
System.out.println("The content of the file is : " + Native.toString(lParamStr));
}