Controlling command prompt of a remote computer
Basically what I'm trying to do is:
1) run application (for example command prompt) using shell command
2) show command prompt's window in a frame of a server application
3) send that window via CSocket(you can explain for winsock if that's easier for you) to client application
4) show that same command prompt's window in a frame of a client's application.
I want to send commands to command prompt in a server application on one computer from a client application on another computer.
This is what I managed to do for now:
SERVER CODE:
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Private WithEvents cSck As CSocket
Private Sub Command1_Click()
Dim dosHwnd As Long
dosHwnd = FindWindow(vbNullString, "C:\WINDOWS\System32\cmd.exe")
Call SetParent(dosHwnd, Frame1.hwnd)
Call MoveWindow(dosHwnd, 0, 0, 800, 600, True)
cSck.SendData Frame1.hwnd
End Sub
Private Sub Form_Load()
Set cSck = New CSocket
cSck.Protocol = sckTCPProtocol
cSck.LocalPort = 1001
cSck.Listen
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim dosHwnd As Long
dosHwnd = FindWindowEx(Frame1.hwnd, 0, vbNullString, "C:\WINDOWS\System32\cmd.exe")
Call CloseWindow(dosHwnd)
End Sub
Private Sub cSck_OnConnectionRequest(ByVal requestID As Long)
If cSck.State <> sckClosed Then _
cSck.CloseSocket
cSck.Accept requestID
End Sub
Private Sub cSck_OnDataArrival(ByVal bytesTotal As Long)
Dim strData As String
cSck.GetData strData
txtOutput.Text = strData
Shell strData, vbNormalFocus
End Sub
AND A CLIENT'S CODE:
Private WithEvents cSck As CSocket
Private Sub cmdConnect_Click()
Set cSck = New CSocket
cSck.Protocol = sckTCPProtocol
cSck.RemoteHost = "HostName"
cSck.RemotePort = 1001
cSck.Connect
End Sub
Private Sub Command1_Click()
cSck.SendData "c:\windows\system32\cmd.exe"
End Sub
Private Sub cSck_OnDataArrival(ByVal bytesTotal As Long)
Dim content As Data
cSck.GetData content
Frame1.hwnd = content
End Sub
I always get the "Can't assign to read only property" error. Is there any another way to do this? If there is please help!
Thanks in advance!:confused: