From 670d8b1dcd4a026f55aab8e57da62945d464856f Mon Sep 17 00:00:00 2001 From: AmirHossein Abdolmotallebi Date: Wed, 27 Nov 2024 20:02:39 +0330 Subject: [PATCH] uninitialize COM --- .../util/osfileutil/WindowsFileUtils.kt | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/shared/utils/src/main/kotlin/ir/amirab/util/osfileutil/WindowsFileUtils.kt b/shared/utils/src/main/kotlin/ir/amirab/util/osfileutil/WindowsFileUtils.kt index f4f0416..061f30f 100644 --- a/shared/utils/src/main/kotlin/ir/amirab/util/osfileutil/WindowsFileUtils.kt +++ b/shared/utils/src/main/kotlin/ir/amirab/util/osfileutil/WindowsFileUtils.kt @@ -34,26 +34,30 @@ internal class WindowsFileUtils : FileUtilsBase() { private fun showFileInFolderViaNative( file: String, ): Boolean { - Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_APARTMENTTHREADED) - val path = Shell32Ex.INSTANCE.ILCreateFromPath(File(file).parent) - val selectedFiles = arrayOf(Shell32Ex.INSTANCE.ILCreateFromPath(file)) - val cidl = WinDef.UINT(selectedFiles.size.toLong()) try { - val res = Shell32Ex.INSTANCE.SHOpenFolderAndSelectItems( - pIdlFolder = path, - cIdl = cidl, - apIdl = selectedFiles, - dwFlags = WinDef.DWORD(0) - ) - return WinError.S_OK == res + Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_APARTMENTTHREADED) + val path = Shell32Ex.INSTANCE.ILCreateFromPath(File(file).parent) + val selectedFiles = arrayOf(Shell32Ex.INSTANCE.ILCreateFromPath(file)) + val cidl = WinDef.UINT(selectedFiles.size.toLong()) + try { + val res = Shell32Ex.INSTANCE.SHOpenFolderAndSelectItems( + pIdlFolder = path, + cIdl = cidl, + apIdl = selectedFiles, + dwFlags = WinDef.DWORD(0) + ) + return WinError.S_OK == res + } finally { + Shell32Ex.INSTANCE.ILFree(path) + selectedFiles.forEach { + Shell32Ex.INSTANCE.ILFree(it) + } + } } catch (e: Exception) { e.printStackTrace() return false } finally { - Shell32Ex.INSTANCE.ILFree(path) - selectedFiles.forEach { - Shell32Ex.INSTANCE.ILFree(it) - } + Ole32.INSTANCE.CoUninitialize() } }