我正在尝试在Mac OS X上使用JNA的诀窍。我想访问一个Carbon库,它没有与Cocoa对应的库,所以Cocoa无法帮助我(我认为...)
当我试图调用一个需要CFStringRef作为参数的Carbon函数时,我遇到了问题。如何从Java String创建CFStringRef?
这是我到目前为止的尝试:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
public class JnaTest {
public interface AXUIElement extends Library {
AXUIElement INSTANCE = (AXUIElement) Native.loadLibrary("Carbon", AXUIElement.class);
Pointer AXUIElementCreateApplication(int pid);
// HELP: String is clearly wrong here but what should I use?
int AXUIElementCopyAttributeValue(Pointer element, String attribute, PointerByReference value);
}
public static void main(String[] args) {
final int pid = 5264; // make sure this is a valid pid
final Pointer elementRef = AXUIElement.INSTANCE.AXUIElementCreateApplication(pid);
// HELP: attribute should be of type CFStringRef
final String attribute = "AXWindows";
PointerByReference value = new PointerByReference();
final int error = AXUIElement.INSTANCE.AXUIElementCopyAttributeValue(elementRef, attribute, value);
if (error == 0) {
System.out.println("value = " + value);
} else {
System.out.println("Failure: " + error);
}
}
}发布于 2009-10-27 02:14:30
我想出了这个:
public static CFStringRef toCFString(String s) {
final char[] chars = s.toCharArray();
int length = chars.length;
return AXUIElement.INSTANCE.CFStringCreateWithCharacters(null, chars, AXAPI.createCFIndexFor(length));
}有了这个定义:
CFStringRef CFStringCreateWithCharacters(
Void alloc, // always pass NULL
char[] chars,
CFIndex numChars
);https://stackoverflow.com/questions/1612520
复制相似问题