Calling API console functions using JNA

Keywords: Windows Java

Official API Function Call Introduction Website:

https://docs.microsoft.com/en-us/windows/console/console-reference

First of all, we need to use the native definition method, referring to the method in "kernel32".

static {
		Native.register("kernel32");
	}

The most important redefinition is to retrieve the handle of the specified standard device using the GetStdHandle method (it's not very clear exactly what to do, but the method I use later must transmit the value obtained by this method).

Then you need to define the methods you need to use, such as GetConsoleScreen Buffer Info "Get information about the console screen buffer":

static native boolean GetConsoleScreenBufferInfo(int hConsoleOutput,
			PCONSOLE_SCREEN_BUFFER_INFO.ByReference lpConsoleScreenBufferInfo);

Notice the type definition of the parameter, and I studied it with another colleague for three days, only to find that the reason why it has been ineffective is because of the parameter problem.

For example, PCONSOLE_SCREEN_BUFFER_INFO type:

public static class PCONSOLE_SCREEN_BUFFER_INFO extends Structure {

		public static class ByReference extends PCONSOLE_SCREEN_BUFFER_INFO implements Structure.ByReference {
		}

		public static class ByValue extends PCONSOLE_SCREEN_BUFFER_INFO implements Structure.ByValue {
		}

		public COORD.ByValue dwSize = new COORD.ByValue();
		public COORD.ByValue dwCursorPosition = new COORD.ByValue();
		public short wAttributes;
		public SMALL_RECT.ByValue srWindow = new SMALL_RECT.ByValue();
		public COORD.ByValue dwMaximumWindowSize = new COORD.ByValue();

		@Override
		protected List<String> getFieldOrder() {
			return Arrays.asList("dwSize", "dwCursorPosition", "wAttributes", "srWindow", "dwMaximumWindowSize");
		}
	}

Among them, ByReference and ByValue are very important. It must be clear when passing values in the future, which is the method's value passing or the reference passing. Generally speaking, reference passing is used when passing the whole object, and value passing is used when obtaining some parameters that change the object.

For example, the GetConsoleScreenBufferInfo method is used to get the cursor position of the console:

public static COORD.ByValue getPointer(int handle) {
		WinKernel32.PCONSOLE_SCREEN_BUFFER_INFO.ByReference lpConsoleScreenBufferInfo = new WinKernel32.PCONSOLE_SCREEN_BUFFER_INFO.ByReference();
		if (!WinKernel32.GetConsoleScreenBufferInfo(handle, lpConsoleScreenBufferInfo)) {
			checkLastError();
		}
		return lpConsoleScreenBufferInfo.dwCursorPosition;
	}

 

COORD.ByValue pointer = getPointer(hOut);//hOut Gets the value for GetStdHandle
pointer.x;//To get the coordinate type SHORT, this type is not short in java
pointer.y;//It's import com.sun.jna.platform.win32.WinDef.SHORT.

Above are personal summaries and understandings. If there are any deviations, you are welcome to point out.

Posted by Cyberspace on Thu, 03 Oct 2019 14:04:50 -0700