Question

J'essaie de maîtriser l'utilisation de la JNA avec Mac OS X. Je souhaite accéder à une bibliothèque Carbon, pour laquelle il n'existe pas d'équivalent Cocoa, donc Rococoa ne peut pas m'aider (je pense ...)

Je suis bloqué lorsque j'essaie d'appeler une fonction Carbon qui requiert CFStringRef en tant que paramètre. Comment créer un CFStringRef à partir d’une chaîne Java?

Voici ma tentative jusqu'à présent:

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);
        }
    }

}
Était-ce utile?

La solution

Je suis venu avec ceci:

   public static CFStringRef toCFString(String s) {
        final char[] chars = s.toCharArray();
        int length = chars.length;
        return AXUIElement.INSTANCE.CFStringCreateWithCharacters(null, chars, AXAPI.createCFIndexFor(length));
    }

avec cette définition:

CFStringRef CFStringCreateWithCharacters(
        Void alloc, //  always pass NULL
        char[] chars,
        CFIndex numChars
);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top