1

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.

Magellus
  • 21
  • 5
  • Check what's the return value of `Process.GetProcessesByName("osk")`. Most likely the process name is wrong and your `for each` does nothing. – Alejandro Oct 11 '22 at 20:54
  • Thank Alejandro, actually the GetProcessesByName work fine. I get 1 process named osk and it goes in the loop. My belief is that there is another process attach to it called something else and i don't kill it. Or the 64-bit osk cannot be killed this way. – Magellus Oct 12 '22 at 13:19
  • It could always be that the program launches yet another process from other file, or that you don't have permissions to kill it (it can happen if you elevated `osk`, you need to be elevated too to kill it, for example). 64 bits or not is irrelevant here. – Alejandro Oct 12 '22 at 13:34
  • Thank Alejandro for that answer. I would like to dig in that direction but i am not well educated in that subject. Could you point me out some ressource about that so i can try something on my side? Thank you for your time. – Magellus Oct 12 '22 at 18:13

1 Answers1

1

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()
Magellus
  • 21
  • 5