3

I've managed to find from other questions some data that allows me to achieve the next code:

Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("user32.dll")> _
    Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim hwnd As IntPtr
        hwnd = FindWindow(vbNullChar, "C:\\WINDOWS\\system32\\cmd.exe")

        If hwnd.Equals(IntPtr.Zero) Then
            MessageBox.Show("Got null handle")
        Else
            SetParent(hwnd, Me.Handle)
            MoveWindow(hwnd, 0, 0, Me.Width, Me.Height, False)
        End If
    End Sub
End Class

My problem is that I can't manage to find the DOS console window.

The question in C# Embedding a DOS console in a windows form

Community
  • 1
  • 1
Alrik
  • 374
  • 3
  • 14

1 Answers1

1

Using bring a console window to front in c# as a basis, you can modify your code:

<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindowByCaption(ByVal zeroOnly As IntPtr, ByVal lpWindowName As String) As IntPtr
End Function

''in frmLoad:
hwnd = FindWindowByCaption(IntPtr.Zero, "c:\WINDOWS\system32\cmd.exe")

As Jon Skeet said:

It's hacky, it's horrible, but it works for me (thanks, pinvoke.net!):

And Cody Gray is also correct with this:

You probably can't manage to find it because it won't always have this title: C:\\WINDOWS\\system32\\cmd.exe. Mine doesn't, for example.

So it works, but is flaky.

Community
  • 1
  • 1
Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • This works perfectly (Thanks), but you have to put the caption of the screen as it is in the user's language for example in Spanish you have to put "Administrador: C:\WINDOWS\system32\cmd.exe" if you don't know the exact name of the window... xD (Thanks again). – Alrik Jan 12 '12 at 15:55
  • 1
    @Alrik: No, that's not how you do it either. I run an English version of Windows, and "Administrator: C:\WINDOWS\system32\cmd.exe" is still not the title of my command prompt window. Hard-coding things like this is going to get you into trouble. Not at all recommended. Find the window using something other than the caption. – Cody Gray - on strike Jan 13 '12 at 00:34
  • I'll try other options, I suppose that there are other kind of functions that can be used in the same case that avoids using the window name maybe the PID. – Alrik Jan 13 '12 at 11:47