質問

Mac OS XでJNAを使用しようとしています。Cocoaに相当するものがないCarbonライブラリにアクセスしたいので、Rococoaは私を助けられません(...)

CFStringRefをパラメーターとして必要とするCarbon関数を呼び出そうとすると、スタックします。 Java文字列から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);
        }
    }

}
役に立ちましたか?

解決

これを思いついた:

   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
);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top