这是一个关于如何将 JNA 与 MacOS API 结合使用的简单示例。我不会解释这一点,因为我仍在学习,但我将代码保持最少,以便更容易理解并了解如何将其用于其他目的的基本概念。
大部分代码来自 Intellij-Community(Apache 许可证)。
我们需要的是 jna-platform。
net.java.dev.jna jna-platform 5.13.0
源代码文件一共有4个。 ID 是从 IntelliJ 复制而来,未经修改。 MyFoundation 和 MyFoundationLibrary 分别从 Foundation 和 FoundationLibrary 中删除。
package org.example; import com.sun.jna.Pointer; import static org.example.MyFoundation.*; public class Application { public static void main(String[] args) throws Exception { ID processInfoCls = getObjcClass("NSProcessInfo"); Pointer processInfoSel = createSelector("processInfo"); Pointer processNameSel = createSelector("processName"); ID processInfo = invoke(processInfoCls, processInfoSel); ID processNameNSString = invoke(processInfo, processNameSel); String processName = toStringViaUTF8(processNameNSString); System.out.println(processName); } }
package org.example; import com.sun.jna.NativeLong; public final class ID extends NativeLong { public ID() { } public ID(long peer) { super(peer); } public static final ID NIL = new ID(0L); public boolean booleanValue() { return intValue() != 0; } }
public interface MyFoundationLibrary extends Library { int kCFStringEncodingUTF8 = 0x08000100; ID objc_getClass(String className); Pointer sel_registerName(String selectorName); int CFStringGetLength(ID theString); byte CFStringGetCString(ID theString, byte[] buffer, int bufferSize, int encoding); }
package org.example; import com.sun.jna.*; import java.lang.reflect.Proxy; import java.util.Collections; public class MyFoundation { private static final MyFoundationLibrary myFoundationLibrary; private static final Function myObjcMsgSend; static { myFoundationLibrary = Native.load("Foundation", MyFoundationLibrary.class, Collections.singletonMap("jna.encoding", "UTF8")); NativeLibrary nativeLibrary = ((Library.Handler) Proxy.getInvocationHandler(myFoundationLibrary)).getNativeLibrary(); myObjcMsgSend = nativeLibrary.getFunction("objc_msgSend"); } public static ID getObjcClass(String className) { return myFoundationLibrary.objc_getClass(className); } public static Pointer createSelector(String s) { return myFoundationLibrary.sel_registerName(s); } private static Object [] prepInvoke(ID id, Pointer selector, Object[] args) { Object[] invokArgs = new Object[args.length 2]; invokArgs[0] = id; invokArgs[1] = selector; System.arraycopy(args, 0, invokArgs, 2, args.length); return invokArgs; } public static ID invoke(final ID id, final Pointer selector, Object... args) { // objc_msgSend is called with the calling convention of the target method // on x86_64 this does not make a difference, but arm64 uses a different calling convention for varargs // it is therefore important to not call objc_msgSend as a vararg function return new ID(myObjcMsgSend.invokeLong(prepInvoke(id, selector, args))); } public static String toStringViaUTF8(ID cfString) { if (ID.NIL.equals(cfString)) return null; int lengthInChars = myFoundationLibrary.CFStringGetLength(cfString); int potentialLengthInBytes = 3 * lengthInChars 1; // UTF8 fully escaped 16 bit chars, plus nul byte[] buffer = new byte[potentialLengthInBytes]; byte ok = myFoundationLibrary.CFStringGetCString(cfString, buffer, buffer.length, MyFoundationLibrary.kCFStringEncodingUTF8); if (ok == 0) throw new RuntimeException("Could not convert string"); return Native.toString(buffer); } }
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3