0

When I use the code down below to start a cmd window and move it into a panel, everything works OK. But when I replace cmd with conhost, it fails with the following error: 'The system cannot find the file specified', even when I specify the complete path. By the way, the reason why I'm trying to start conhost instead of cmd is just for testing purposes, I'm trying to find a way to find the PID of the conhost process for a process running through cmd. Thanks for any help in advance!

Kind regards, Eric van Loon

Imports System.Runtime.InteropServices
Public Class Form1
    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim proc As Process
        proc = Process.Start("cmd")
        proc.WaitForExit(600)
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
        SendMessage(proc.MainWindowHandle, 274, 61488, 0)
    End Sub
End Class
Eric van Loon
  • 103
  • 1
  • 9
  • You don't really need to start conhost (even if you could). That Process becomes a child Process of your app's Process when you parent a Console Window. You have to enumerate your child processes after the re-parenting thing -- I suppose you want to get that Process because it's the actual handler of the Console Window – Jimi Nov 03 '22 at 16:15
  • 1
    `%SystemRoot%\System32\conhost.exe` exists on 64-bit Windows only as 64-bit application. There is no 32-bit `%SystemRoot%\SysWOW64\conhost.exe`. The application running the posted VB.Net code is obviously a 32-bit executable which means the Windows [File System Redirector](https://learn.microsoft.com/en-us/windows/win32/winprog64/file-system-redirector) is active and therefore redirects each access to `%SystemRoot%\System32` to `%SystemRoot%\SysWOW64`. That is the reason why `conhost.exe` cannot be found while 32-bit `%SystemRoot%\SysWOW64\cmd.exe` is found and executed by this code. – Mofi Nov 03 '22 at 17:16
  • 1
    There could be used `%SystemRoot%\Sysnative\conhost.exe` to run on 64-bit Windows the 64-bit `conhost.exe` from 32-bit application. But this would not work on 32-bit Windows. Calling first [Wow64DisableWow64FsRedirection function](https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-wow64disablewow64fsredirection) and then starting `conhost.exe` would be better according to Microsoft. See the Stack Overflow search results for [[vb.net\] Wow64DisableWow64FsRedirection](https://stackoverflow.com/search?q=%5Bvb.net%5D+Wow64DisableWow64FsRedirection+). – Mofi Nov 03 '22 at 17:22
  • This tells you what processes are sharing a console - https://winsourcecode.blogspot.com/2019/05/listconsoleexe-list-processes-in.html. This lists all windows on the system - https://winsourcecode.blogspot.com/2019/05/winlistexe-list-open-windows-and-their.html. `ConHost` doesn't have a Window. – Lundt Nov 03 '22 at 22:34
  • Hi @Jimi, the final idea is to start a non-GUI (character based) application, move it into my panel and give it the focus, just like you explained to me in [one of your other answers](https://stackoverflow.com/questions/74219064/how-to-move-a-form-to-the-foreground-when-clicking-a-window-of-another-app-paren). I found out however, that installing a hook into cmd.exe is not working, probably because I should hook into conhost.exe instead. However, I don't know how I can retrieve the ProcessId of this conhost.exe, which is started by cmd.exe. Thanks again for your help! Kind regards, Eric – Eric van Loon Nov 04 '22 at 08:35
  • 1
    Eric looney `Conhost` doesn't have a window. – Lundt Nov 04 '22 at 09:44

1 Answers1

0

Like explained by Mofi:

%SystemRoot%\System32\conhost.exe exists on 64-bit Windows only as 64-bit application. There is no 32-bit %SystemRoot%\SysWOW64\conhost.exe. The application running the posted VB.Net code is obviously a 32-bit executable which means the Windows File System Redirector is active and therefore redirects each access to %SystemRoot%\System32 to %SystemRoot%\SysWOW64. That is the reason why conhost.exe cannot be found while 32-bit %SystemRoot%\SysWOW64\cmd.exe is found and executed by this code.

Eric van Loon
  • 103
  • 1
  • 9