From d51340686fde35ec801672de5efeb9903558da12 Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Mon, 11 May 2020 12:56:25 +0800 Subject: [PATCH] [misc] improve ime process detection logic --- win/setup.ps1 | 1 - win/wubi-no-shift.ps1 | 34 +++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/win/setup.ps1 b/win/setup.ps1 index 14140c8..2b694d0 100644 --- a/win/setup.ps1 +++ b/win/setup.ps1 @@ -20,5 +20,4 @@ if (Test-Path $sshconf -PathType Leaf) { $wubiAction = New-ScheduledTaskAction -Execute 'Powershell.exe' ` -Argument "-NoProfile -WindowStyle Hidden -File $PSScriptRoot\wubi-no-shift.ps1" $wubiTrigger = New-ScheduledTaskTrigger -AtLogOn -$wubiTrigger.Delay = 'PT3S' Register-ScheduledTask -Action $wubiAction -Trigger $wubiTrigger -TaskName "Wubi No Shift" -Description "Disable Shift key toggling CN/EN" -RunLevel Highest \ No newline at end of file diff --git a/win/wubi-no-shift.ps1 b/win/wubi-no-shift.ps1 index b27b925..11167c1 100644 --- a/win/wubi-no-shift.ps1 +++ b/win/wubi-no-shift.ps1 @@ -32,19 +32,27 @@ Add-Type -MemberDefinition @' '@ -Name Kernel32 -Namespace Pinvoke -$ps = Get-Process -Name $ChsIME -foreach ($p in $ps) { - $hModule = $p.Modules | Where-Object {$_.ModuleName -eq $ChsIMEExe} - if (!$hModule) { - continue +$i = 0 +while ($i++ -lt 10) { + + $ps = Get-Process -Name $ChsIME + foreach ($p in $ps) { + $hModule = $p.Modules | Where-Object {$_.ModuleName -eq $ChsIMEExe} + if (!$hModule) { + continue + } + $hModule = $hModule[0] + $addr = [IntPtr]::Add($hModule.BaseAddress, $offsetAddr) + [Int32]$n = 0 + $pidd = $p.id + if ([Pinvoke.Kernel32]::WriteProcessMemory($p.Handle[0], $addr, @(0x31, 0xc0), 2, [ref]$n)) { + echo "$pidd is patched" + } else { + throw [System.Exception] "Failed to patch $pidd" + } } - $hModule = $hModule[0] - $addr = [IntPtr]::Add($hModule.BaseAddress, $offsetAddr) - [Int32]$n = 0 - $pidd = $p.id - if ([Pinvoke.Kernel32]::WriteProcessMemory($p.Handle[0], $addr, @(0x31, 0xc0), 2, [ref]$n)) { - echo "$pidd is patched" - } else { - throw [System.Exception] "Failed to patch $pidd" + if ($ps) { + break } + Start-Sleep -Milliseconds 100 }