|
-
Jul 30th, 2005, 03:31 AM
#1
Thread Starter
Fanatic Member
sending text into cmd.exe?
hello, how to send a text into cmd.exe?
from Text1.Text: -> cmd.exe?
br,
-
Jul 30th, 2005, 04:04 AM
#2
Fanatic Member
Re: sending text into cmd.exe?
I think this is possible with a window hook, although I havn't worked with window hooks personally. I'm pretty sure thats what you would have to do.
-
Jul 30th, 2005, 04:24 AM
#3
Thread Starter
Fanatic Member
Re: sending text into cmd.exe?
@k1ll3rdr4g0n
sir, do u have an sample for that?
Thanks...
br,
-
Jul 30th, 2005, 04:34 AM
#4
Re: sending text into cmd.exe?
Use a program such as Spy++ (I think that does it), to get the Classname of the Command Prompt text input window. Then use the FindWindow, SendMessage and the WM_SETTEXT/WM_CHAR messages. There are many posts about how to input text into another program. Search the forums 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 30th, 2005, 04:40 AM
#5
Re: sending text into cmd.exe?
Shell "cmd " & Text1.Text
-
Jul 30th, 2005, 04:44 AM
#6
Re: sending text into cmd.exe?
That would open it with the text. I thought the original question was to add text to an already open Command Prompt?
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jul 30th, 2005, 04:49 AM
#7
Re: sending text into cmd.exe?
I didn't, so we will have to wait for nokmaster to clarify that.
Also, I have a feeling WM_SETTEXT probably won't work in this case because of the way consoles operate, not sure however.
-
Jul 30th, 2005, 05:12 AM
#8
Thread Starter
Fanatic Member
Re: sending text into cmd.exe?
open first the cmd.exe and then in Text1.Text you input: NAME and then send it to cmd.exe
that's what i looking....
thanks...
-
Jul 30th, 2005, 11:19 PM
#9
Thread Starter
Fanatic Member
Re: sending text into cmd.exe?
how to send from Text1.Text to cmd.exe?
-
Jul 30th, 2005, 11:43 PM
#10
Re: sending text into cmd.exe?
Sending messages to the command window will not work unless you create a pipe window via APIs so you can interact with the Windows message stream.
VB Code:
'Redirects output from console program to textbox.
'Requires two textboxes and one command button.
'Set MultiLine property of Text2 to true.
'
'Original bcx version of this program was made by
' dl
'VB port was made by Jernej Simoncic
'
'Note: don't run plain DOS programs with this example
'under Windows 95,98 and ME, as the program freezes when
'execution of program is finnished.
Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, _
lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type OVERLAPPED
ternal As Long
ternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2
Private Sub Command1_Click()
Command1.Enabled = False
Redirect Text1.Text, Text2
Command1.Enabled = True
End Sub
Private Sub Form_Load()
Text1.Text = "ping"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Command1.Enabled = False Then Cancel = True
End Sub
Sub Redirect(cmdLine As String, objTarget As Object)
Dim i%, t$
Dim pa As SECURITY_ATTRIBUTES
Dim pra As SECURITY_ATTRIBUTES
Dim tra As SECURITY_ATTRIBUTES
Dim pi As PROCESS_INFORMATION
Dim sui As STARTUPINFO
Dim hRead As Long
Dim hWrite As Long
Dim bRead As Long
Dim lpBuffer(1024) As Byte
pa.nLength = Len(pa)
pa.lpSecurityDescriptor = 0
pa.bInheritHandle = True
pra.nLength = Len(pra)
tra.nLength = Len(tra)
If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
sui.cb = Len(sui)
GetStartupInfo sui
sui.hStdOutput = hWrite
sui.hStdError = hWrite
sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
sui.wShowWindow = SW_HIDE
If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
SetWindowText objTarget.hwnd, ""
Do
Erase lpBuffer()
If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
DoEvents
Else
CloseHandle pi.hThread
CloseHandle pi.hProcess
Exit Do
End If
CloseHandle hWrite
Loop
CloseHandle hRead
End If
End If
End Sub
Last edited by RobDog888; Jul 30th, 2005 at 11:49 PM.
Reason: Fix API dlclaration so it wraps and doesnt mess up the page too much. :D
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 30th, 2005, 11:43 PM
#11
Re: sending text into cmd.exe?
I think penagate answered that in post #5, unless chem was right about what you are looking. Please clarify what you need. Thanks.
EDIT: Dangit RD!! Why do people always double post me with a much better response than mine.
-
Jul 31st, 2005, 05:51 AM
#12
Thread Starter
Fanatic Member
Re: sending text into cmd.exe?
anyone can add with this code?
VB Code:
x = shell("command", 0)
this will create a dos session, With windows handle of "x", which will be hidden from the program user.
Once created To End the session the standard "exit" can be sent to the command line.
'
Public Sub Dos_Send(CommandWindowName as string, SendToDos as string)
clipboard.Clear
'clear the clipboard (obvious)
Clipboard.SetText SendToDos ' + Chr$(13)
'send the relevent key/command to the cl
' ipboard
'note that the Chr$(13) is requried to i
' f the "SendToDos" text is a dos command
' or 'requires the pressing of the Enter/R
' eturn key after the relevent string has
' been 'sent.
AppActivate CommandWindowName
'CommandWindowName is MS-DOS Prompt, or
' whatever appears in your task bar.
'See the AppActivate for more info.
'A good alternative is to use the handle
' (hwind) of the dos window
SendKeys "% ep", 1
'this sends the clipboard to dos
'Now wasn't that simple?
End Sub
-
Jul 31st, 2005, 10:33 AM
#13
Re: sending text into cmd.exe?
I wouldnt suggest SendKeys since it is flaky and depends on the window maintaining focus. If the user accidentally tabs or mouseclicks, presses a key, the focus will change and the SendKeys will fail.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jul 31st, 2005, 12:50 PM
#14
Re: sending text into cmd.exe?
The above sendkey code also assumes that the system menu of a command line window has the &Edit > &Paste names which might not be true for all localized version of Windows. I happens to use an English (US) version of Windows but most people in my country use a Swedish version and the SendKeys string should in that case be "% rl" (I think) or it will not work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|