I am trying to kill instance of Osk.exe programmatically.
I have a dialogue that allow user to start osk with a button, and if they do not close it themself I close it via the code in closing form.
My code look like this for creation and closing.
Public Sub ClavierCommandExecute()
Dim path64 = "C:\Windows\winsxs\amd64_microsoft-windows-osk_31bf3856ad364e35_10.0.19041.1_none_60ade0eff94c37fc\osk.exe"
Dim path32 = "C:\windows\system32\osk.exe"
Dim Path = If(Environment.Is64BitOperatingSystem, path64, path32)
Me.ProcessusClavier = Process.Start(Path)
End Sub
Public Sub FermerCommandExecute()
Dim processOSK = Process.GetProcessesByName("osk")
For Each proc In processOSK
proc.Kill()
Next
Me.Close()
End Sub
The thing is, if I do this that way, the osk process continue running background. I can tell it because when i lock my laptop, it open back the osk. If it can help i am still on windows 10 64-bit.
But if I close it manually with the close button or even through the task manager, everything work fine.
It would not be a problem normaly, but i feel it created a memory leak by not being killed properly.
Ok, the problem come from the fact that i needed to be administrator to kill properly OSK.exe.
I replace the simple processOSK.kill() by that code wich allow me to kill it trough taskill as an administrator.
Dim process As System.Diagnostics.Process = New System.Diagnostics.Process()
Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
startInfo.FileName = "taskkill.exe"
startInfo.Arguments = "/im osk.exe"
startInfo.Verb = "runas"
process.StartInfo = startInfo
process.Start()