1

Mac OS X で JNA を使用するコツをつかもうとしています。Cocoa に相当するものがない Carbon ライブラリにアクセスしたいので、Rococoa は役に立ちません (と思います...)。

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

}
4

1 に答える 1

1

私はこれを思いついた:

   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
);
于 2009-10-26T18:14:29.797 に答える