簡體   English   中英

Kotlin Native 使用 AXUIElement

[英]Kotlin Native use AXUIElement

我嘗試獲取所有應用程序的窗口。

fun main() = memScoped {
    NSWorkspace.sharedWorkspace.runningApplications()
        .map { it as NSRunningApplication }
        .filter { it.active }
        .forEach {
            val applicationRef = AXUIElementCreateApplication(it.processIdentifier)
            ...
        }
}

但我無法在 kotlin 本地庫中找到AXUIElement ,但AXUIElementRefAXUIElementRefVar 許多文章展示了獲取AXUIElement的 swift 代碼,但我不知道如何將代碼更改為 kotlin。

像這樣的swift代碼:

if let pid = proToolsApp?.processIdentifier {
    var result = [AXUIElement]()
    var windowList: AnyObject? = nil // [AXUIElement]

    let appRef = AXUIElementCreateApplication(pid)
    if AXUIElementCopyAttributeValue(appRef, "AXWindows" as CFString, &windowList) == .success {
            result = windowList as! [AXUIElement]
    }

    var docRef: AnyObject? = nil
    if AXUIElementCopyAttributeValue(result.first!, "AXDocument" as CFString, &docRef) == .success {
        let result = docRef as! AXUIElement
        print("Found Document: \(result)")
        let filePath = result as! String
        print(filePath)
    }
}

AXUIElement只是AXUIElementRef ( https://developer.apple.com/documentation/applicationservices/axuielementref?language=objc ) 的一個快速解釋版本

這在 kotlin 中應該是這樣的:

fun windowTest() = memScoped {
    NSWorkspace.sharedWorkspace.runningApplications()
        .map { it as NSRunningApplication }
        .filter { it.active }
        .forEach {
            val applicationRef = AXUIElementCreateApplication(it.processIdentifier)!!
            val output = alloc<CFTypeRefVar>()
            var result = AXUIElementCopyAttributeValue(
                applicationRef,
                attribute = kAXWindowsAttribute.toCFString(),
                value = output.ptr,
            )
            if (result != kAXErrorSuccess) {
                println("error $result")
                return@forEach
            }
            val firstOutput = CFArrayGetValueAtIndex(output.value as CFArrayRef, 0)!!
            result = AXUIElementCopyAttributeValue(
                element = firstOutput.reinterpret(),
                attribute = kAXDocumentAttribute.toCFString(),
                value = output.ptr,
            )
            println("$result ${output.value}")
        }
}


fun String.toCFString(): CFStringRef = CFBridgingRetain(this)!!.reinterpret()

在我的情況下,第二個AXUIElementCopyAttributeValue返回-25212 = kAXErrorNoValue ,我不確定是什么問題,但我在 swift/kotlin 代碼中得到相同的結果

Kotlin/Native - 非法嘗試訪問非共享<object><div id="text_translate"><p>在我的應用程序中,我有兩個類,一個 swift class 繼承自 kotlin ZA2F2ED4F8EBC2CBB4DZ21A29</p><p> <strong>Swift class:</strong></p><pre> public class MySensor: RawSensor [...] public override func notifyChanged(values: KotlinFloatArray) { super.notifyChanged(values: values) } }</pre><p> <strong>Kotlin class:</strong></p><pre> package com.mycompany.myapp.mypackage.sensors.rawsensors import com.mycompany.myapp.mypackage.util.Observable abstract class RawSensor: Observable() { protected abstract val sensorDataType: RawSensorData.SensorDataType abstract val currentTime: Long protected open fun notifyChanged(values: FloatArray) { notifyObservers(RawSensorData(values, sensorDataType, currentTime)) } abstract fun start() abstract fun stop() }</pre><p> 當super.notifyChanged(values: values)時,應用程序崩潰並出現以下錯誤:</p><pre> Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared &lt;object&gt;@83cb47c8 from other thread Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared &lt;object&gt;@83cb4788 from other thread at 0 MyNativeFramework 0x00000001048815dc kfun:kotlin.Exception.&lt;init&gt;(kotlin.String?)kotlin.Exception + 84 at 1 MyNativeFramework 0x00000001048805f8 kfun:kotlin.RuntimeException.&lt;init&gt;(kotlin.String?)kotlin.RuntimeException + 84 at 2 MyNativeFramework 0x00000001048ac630 kfun:kotlin.native.IncorrectDereferenceException.&lt;init&gt;(kotlin.String)kotlin.native.IncorrectDereferenceException + 84 at 3 MyNativeFramework 0x00000001048ad7bc ThrowIllegalObjectSharingException + 496 at 4 MyNativeFramework 0x0000000104a6f1a0 _ZNK16KRefSharedHolder3refEv + 240 at 5 MyNativeFramework 0x0000000104a6eeb0 -[KotlinBase retain] + 52 at 6 libobjc.A.dylib 0x00000001aad36288 objc_retain + 88 at 7 PositionKit 0x000000010449454c $s11PositionKit21MySensorC13notifyChanged6valuesySo19MyNativeLibraryKotlinFloatArrayC_tFTo + 56 at 8 PositionKit 0x0000000104493fa0 $s11PositionKit21MySensorC5startyyFySo19CMAccelerometerDataCSg_s5Error_pSgtcfU_ + 796 at 9 PositionKit 0x00000001044941ec $sSo19CMAccelerometerDataCSgs5Error_pSgIeggg_ACSo7NSErrorCSgIeyByy_TR + 152 at 10 CoreMotion 0x00000001b7c44a8c CLClientCreateIso6709Notation + 30848 at 11 Foundation 0x00000001ab3d1c60 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1170528 at 12 Foundation 0x00000001ab2d37e8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 129000 at 13 Foundation 0x00000001ab3d3fbc 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1179580 at 14 Foundation 0x00000001ab2d3464 672CF0CB-4951-3B91-89DF-55E953AEA00F + 128100 at 15 Foundation 0x00000001ab3d49e8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1182184 at 16 Foundation 0x00000001ab3d44a8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1180840 at 17 libdispatch.dylib 0x0000000104f59a48 _dispatch_block_async_invoke2 + 144 at 18 libdispatch.dylib 0x0000000104f4b2a8 _dispatch_client_callout + 20 at 19 libdispatch.dylib 0x0000000104f5935c _dispatch_main_queue_callback_4CF + 1376 at 20 CoreFoundation 0x00000001aaf7ce20 97285ACB-7B21-393A-ABF6-03F1DBB5D2A2 + 712224 at 21 CoreFoundation 0x00000001aaf77b7c 97285ACB-7B21-393A-ABF6-03F1DBB5D2A2 + 691068 at 22 CoreFoundation 0x00000001aaf77098 CFRunLoopRunSpecific + 480 at 23 GraphicsServices 0x00000001b50e1534 GSEventRunModal + 108 at 24 UIKitCore 0x00000001af0977ac UIApplicationMain + 1940 at 25 MyTestApp 0x00000001044383a4 main + 76 at 26 libdyld.dylib 0x00000001aadf6f30 0DC9A4BA-C3E8-3487-99DB-1B5C86597AF5 + 3888 at 0 MyNativeFramework 0x00000001048815dc kfun:kotlin.Exception.&lt;init&gt;(kotlin.String?)kotlin.Exception + 84 at 1 MyNativeFramework 0x00000001048805f8 kfun:kotlin.RuntimeException.&lt;init&gt;(kotlin.String?)kotlin.RuntimeException + 84 at 2 MyNativeFramework 0x00000001048ac630 kfun:kotlin.native.IncorrectDereferenceException.&lt;init&gt;(kotlin.String)kotlin.native.IncorrectDereferenceException + 84 at 3 MyNativeFramework 0x00000001048ad7bc ThrowIllegalObjectSharingException + 496 at 4 MyNativeFramework x0000000104a6f1a0 _ZNK16KRefSharedHolder3refEv + 240 at 5 MyNativeFramework 0x0000000104a6eff4 -[KotlinBase release] + 48 at 6 libobjc.A.dylib 0x00000001aad36408 objc_release + 136 at 7 PositionKit 0x00000001044967e8 $s11PositionKit17MySensorC5startyyFySo14CMDeviceMotionCSg_s5Error_pSgtcfU_ + 684(lldb)</pre><p> <em>(名稱已更改以保護封閉源代碼)</em></p><p> 我究竟做錯了什么? 我是否必須以某種方式將浮點數組標記為共享? 我已經閱讀了 Kotlin/Native 中的數據凍結,但這似乎只適用於 kotlin 上下文中的對象。</p></div></object>

[英]Kotlin/Native - illegal attempt to access non-shared <object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何在Swift中從CFTypeRef轉換為AXUIElement "從 Kotlin Native 繼承 UIView" 無法將 kotlin 接口的 swift 實現傳遞給 kotlin 本機 帶有 Kotlin 本機和 ObjectiveC/Swift 的可選/可空值 Memory 泄漏 Kotlin iOS 中的本機庫 kotlin 語言中的快速“完成:@escaping”。 如何在 kotlin 中使用回調? Kotlin/Native - 非法嘗試訪問非共享<object><div id="text_translate"><p>在我的應用程序中,我有兩個類,一個 swift class 繼承自 kotlin ZA2F2ED4F8EBC2CBB4DZ21A29</p><p> <strong>Swift class:</strong></p><pre> public class MySensor: RawSensor [...] public override func notifyChanged(values: KotlinFloatArray) { super.notifyChanged(values: values) } }</pre><p> <strong>Kotlin class:</strong></p><pre> package com.mycompany.myapp.mypackage.sensors.rawsensors import com.mycompany.myapp.mypackage.util.Observable abstract class RawSensor: Observable() { protected abstract val sensorDataType: RawSensorData.SensorDataType abstract val currentTime: Long protected open fun notifyChanged(values: FloatArray) { notifyObservers(RawSensorData(values, sensorDataType, currentTime)) } abstract fun start() abstract fun stop() }</pre><p> 當super.notifyChanged(values: values)時,應用程序崩潰並出現以下錯誤:</p><pre> Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared &lt;object&gt;@83cb47c8 from other thread Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared &lt;object&gt;@83cb4788 from other thread at 0 MyNativeFramework 0x00000001048815dc kfun:kotlin.Exception.&lt;init&gt;(kotlin.String?)kotlin.Exception + 84 at 1 MyNativeFramework 0x00000001048805f8 kfun:kotlin.RuntimeException.&lt;init&gt;(kotlin.String?)kotlin.RuntimeException + 84 at 2 MyNativeFramework 0x00000001048ac630 kfun:kotlin.native.IncorrectDereferenceException.&lt;init&gt;(kotlin.String)kotlin.native.IncorrectDereferenceException + 84 at 3 MyNativeFramework 0x00000001048ad7bc ThrowIllegalObjectSharingException + 496 at 4 MyNativeFramework 0x0000000104a6f1a0 _ZNK16KRefSharedHolder3refEv + 240 at 5 MyNativeFramework 0x0000000104a6eeb0 -[KotlinBase retain] + 52 at 6 libobjc.A.dylib 0x00000001aad36288 objc_retain + 88 at 7 PositionKit 0x000000010449454c $s11PositionKit21MySensorC13notifyChanged6valuesySo19MyNativeLibraryKotlinFloatArrayC_tFTo + 56 at 8 PositionKit 0x0000000104493fa0 $s11PositionKit21MySensorC5startyyFySo19CMAccelerometerDataCSg_s5Error_pSgtcfU_ + 796 at 9 PositionKit 0x00000001044941ec $sSo19CMAccelerometerDataCSgs5Error_pSgIeggg_ACSo7NSErrorCSgIeyByy_TR + 152 at 10 CoreMotion 0x00000001b7c44a8c CLClientCreateIso6709Notation + 30848 at 11 Foundation 0x00000001ab3d1c60 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1170528 at 12 Foundation 0x00000001ab2d37e8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 129000 at 13 Foundation 0x00000001ab3d3fbc 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1179580 at 14 Foundation 0x00000001ab2d3464 672CF0CB-4951-3B91-89DF-55E953AEA00F + 128100 at 15 Foundation 0x00000001ab3d49e8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1182184 at 16 Foundation 0x00000001ab3d44a8 672CF0CB-4951-3B91-89DF-55E953AEA00F + 1180840 at 17 libdispatch.dylib 0x0000000104f59a48 _dispatch_block_async_invoke2 + 144 at 18 libdispatch.dylib 0x0000000104f4b2a8 _dispatch_client_callout + 20 at 19 libdispatch.dylib 0x0000000104f5935c _dispatch_main_queue_callback_4CF + 1376 at 20 CoreFoundation 0x00000001aaf7ce20 97285ACB-7B21-393A-ABF6-03F1DBB5D2A2 + 712224 at 21 CoreFoundation 0x00000001aaf77b7c 97285ACB-7B21-393A-ABF6-03F1DBB5D2A2 + 691068 at 22 CoreFoundation 0x00000001aaf77098 CFRunLoopRunSpecific + 480 at 23 GraphicsServices 0x00000001b50e1534 GSEventRunModal + 108 at 24 UIKitCore 0x00000001af0977ac UIApplicationMain + 1940 at 25 MyTestApp 0x00000001044383a4 main + 76 at 26 libdyld.dylib 0x00000001aadf6f30 0DC9A4BA-C3E8-3487-99DB-1B5C86597AF5 + 3888 at 0 MyNativeFramework 0x00000001048815dc kfun:kotlin.Exception.&lt;init&gt;(kotlin.String?)kotlin.Exception + 84 at 1 MyNativeFramework 0x00000001048805f8 kfun:kotlin.RuntimeException.&lt;init&gt;(kotlin.String?)kotlin.RuntimeException + 84 at 2 MyNativeFramework 0x00000001048ac630 kfun:kotlin.native.IncorrectDereferenceException.&lt;init&gt;(kotlin.String)kotlin.native.IncorrectDereferenceException + 84 at 3 MyNativeFramework 0x00000001048ad7bc ThrowIllegalObjectSharingException + 496 at 4 MyNativeFramework x0000000104a6f1a0 _ZNK16KRefSharedHolder3refEv + 240 at 5 MyNativeFramework 0x0000000104a6eff4 -[KotlinBase release] + 48 at 6 libobjc.A.dylib 0x00000001aad36408 objc_release + 136 at 7 PositionKit 0x00000001044967e8 $s11PositionKit17MySensorC5startyyFySo14CMDeviceMotionCSg_s5Error_pSgtcfU_ + 684(lldb)</pre><p> <em>(名稱已更改以保護封閉源代碼)</em></p><p> 我究竟做錯了什么? 我是否必須以某種方式將浮點數組標記為共享? 我已經閱讀了 Kotlin/Native 中的數據凍結,但這似乎只適用於 kotlin 上下文中的對象。</p></div></object> 在 kotlin native SharedCode iosMain 模塊中導入 swift class 未捕獲的 Kotlin 異常:kotlin.native.IncorrectDereferenceException:非法嘗試訪問非共享 為什么 Koin 在我的 KMM 應用程序中拋出“Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException”?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM