Skip to content

Commit d9e91a9

Browse files
committed
Improve file association refresh mechanism: use P/Invoke for SHChangeNotify to avoid RunDLL popup and add fallback to rundll32
1 parent b4fe428 commit d9e91a9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

scripts/init.ps1

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,21 +247,31 @@ function set_default_app {
247247
function refresh_file_associations {
248248
# Refresh the shell icon cache and notify the system of changes
249249
try {
250-
# Use rundll32 to refresh file associations
251-
Start-Process -FilePath "rundll32.exe" -ArgumentList "shell32.dll,SHChangeNotify,0x8000000,0,0,0" -Wait -NoNewWindow
252-
log_message "Refreshed file associations and shell icon cache"
253-
254-
# Also try to refresh the desktop
250+
# Try to call SHChangeNotify directly via P/Invoke instead of rundll32 to avoid RunDLL popup
255251
$code = @'
256-
[System.Runtime.InteropServices.DllImport("shell32.dll")]
257-
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
252+
using System;
253+
using System.Runtime.InteropServices;
254+
namespace Win32 {
255+
public static class Shell32 {
256+
[DllImport("shell32.dll")]
257+
public static extern void SHChangeNotify(UInt32 wEventId, UInt32 uFlags, IntPtr dwItem1, IntPtr dwItem2);
258+
}
259+
}
258260
'@
259-
Add-Type -MemberDefinition $code -Namespace Win32 -Name Shell32
261+
Add-Type -TypeDefinition $code -ErrorAction Stop
260262
[Win32.Shell32]::SHChangeNotify(0x8000000, 0, [IntPtr]::Zero, [IntPtr]::Zero)
261-
log_message "Sent SHChangeNotify to refresh file associations"
263+
log_message "Refreshed file associations and sent SHChangeNotify"
262264
}
263265
catch {
264-
log_message "Warning: Could not refresh file associations cache: $($_.Exception.Message)"
266+
# Fallback: attempt rundll32 with correct argument format but keep it hidden and non-interactive
267+
try {
268+
$args = "shell32.dll,SHChangeNotify 0x8000000,0"
269+
Start-Process -FilePath "rundll32.exe" -ArgumentList $args -Wait -NoNewWindow -WindowStyle Hidden
270+
log_message "Refreshed file associations using rundll32 fallback"
271+
}
272+
catch {
273+
log_message "Warning: Could not refresh file associations cache: $($_.Exception.Message)"
274+
}
265275
}
266276
}
267277

0 commit comments

Comments
 (0)