hello, how to send a text into cmd.exe?
from Text1.Text: -> cmd.exe?
br,
Printable View
hello, how to send a text into cmd.exe?
from Text1.Text: -> cmd.exe?
br,
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.
@k1ll3rdr4g0n
sir, do u have an sample for that?
Thanks...
br, :)
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
Shell "cmd " & Text1.Text
That would open it with the text. I thought the original question was to add text to an already open Command Prompt?
chem
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.
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...
how to send from Text1.Text to 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
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. :p
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
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. ;)
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.