Hello everyone, I am trying to click on a running cmd.exe to pause the process that is running at that moment. But I can't.
Can anybody help me?.
Thank you very much.
My code:
.Const
.Data?
.Data
hInst HINSTANCE NULL
szClassName DB 'EasyCode2MainWindow', 0
szTitle DB 'Main window', 0
Handle DD 0
Buscar DB "ConsoleWindowClass", 0
.Code
start:
Invoke GetModuleHandle, NULL
Mov hInst, Eax
Invoke GetCommandLine
Invoke EnvioTecla
Invoke ExitProcess, Eax
EnvioTecla Proc
Invoke FindWindowEx, 0, 0, Offset Buscar, NULL
Mov Handle, Eax
.If Eax != NULL
Invoke PostMessage, Handle, WM_LBUTTONDOWN, 0, 0
Invoke PostMessage, Handle, WM_LBUTTONUP, 0, 0
.EndIf
Ret
EnvioTecla EndP
End start
Sorry, I think I have not focused the question well, what I want is to stop the cmd.exe process. But I think you can't...
You could simply press Ctrl C...
Tal vez quiera usted decir algo así?
.386
.MODEL flat, STDCALL
OPTION casemap :none
INCLUDE windows.inc
INCLUDE kernel32.inc
INCLUDE msvcrt.inc
INCLUDELIB kernel32.lib
INCLUDELIB msvcrt.lib
.DATA
Continuar DB "Pulsa Espacio para pausar, ESC para terminar...", 13, 10, 0
.CODE
WaitKeyCrt PROC
@Bucle:
INVOKE crt_printf, OFFSET Continuar ; MASM le añade "add esp, 4"
invoke crt__kbhit
cmp eax, 0
jz @Bucle ; Repetimos el bucle mientras no pulsemos ninguna tecla
INVOKE crt__getch ; Vaciamos el keybuffer
cmp eax, 27
jz @Fin
cmp eax, 32
jnz @Bucle
@Pausa:
invoke crt__kbhit
cmp eax, 0
jz @Pausa ; Repetimos el bucle mientras no pulsemos ninguna tecla
INVOKE crt__getch ; Vaciamos el keybuffer
jmp @Bucle
@Fin:
RET
WaitKeyCrt ENDP
start:
INVOKE WaitKeyCrt
INVOKE crt__exit, 0
END start
C:\masm32\bin\ml.exe /c /coff /Cp /IC:\masm32\Include ConWM03.ASM
C:\masm32\bin\link.exe /SUBSYSTEM:CONSOLE /LIBPATH:c:\masm32\lib ConWM03.obj
.
Muchas gracias por contestar Caballero, veo que somos hispanos los dos quizas?. Imagina poner en una cmd "dir c: /S" para que liste todos los directorios de tu unidad C, esto llevara un rato... pues lo que quiero simular es como si le dieras un clic a la cmd, para que pare y nos siga buscando... no se si me explico bien. O de alguna forma poder pausar un proceso cmd.exe que este en ejecucion. De nuevo muchas gracias.
Sí, de la antigua Hispania :thumbsup:
dir /p
Si no es esto a lo que te refieres, pues ya no sé....
Hi Fraile,
As mentioned earlier, you can stop the process by simply pressing Ctrl C. If, however, you only want to pause the process, there is a simple method. While your dir C:\*.* is running...
- click on the DOS window's system menu in the upper left corner
- click on "Modify->Select"
The process will be suspended until you hit Return. Check in Task Manager: cpu usage will be 0%.
Hi JJ2007, the idea is good, but how do I do it from assembler?
Thank you very much.
This code activates the window for me, now I have to send the keystrokes, ctrl+a
Invoke FindWindowEx, 0, 0, Offset Buscar, NULL
Mov Handle, Eax
.If Eax != NULL
Invoke SetForegroundWindow, Handle
.EndIf
Hi Fraile,
you can try the SendInput (https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput) API.
Thanks a lot, Greenhorn, I'll give it a try. But I think, the console (cmd.exe), is a bit finicky. Anyway I try it. a cordial greeting.
any example in masm for sendinput?
Thanks a lot.
Based on the example but untested ...
ifndef MOUSEINPUT
MOUSEINPUT STRUCT
_dx LONG ?
dy LONG ?
mouseData dd ?
dwFlags dd ?
time dd ?
dwExtraInfo ULONG_PTR ?
MOUSEINPUT ENDS
PMOUSEINPUT typedef ptr MOUSEINPUT
LPMOUSEINPUT typedef ptr MOUSEINPUT
endif
ifndef KEYBDINPUT
KEYBDINPUT STRUCT
wVk dw ?
wScan dw ?
dwFlags dd ?
time dd ?
dwExtraInfo ULONG_PTR ?
KEYBDINPUT ENDS
PKEYBDINPUT typedef ptr KEYBDINPUT
LPKEYBDINPUT typedef ptr KEYBDINPUT
endif
ifndef HARDWAREINPUT
HARDWAREINPUT STRUCT
uMsg dd ?
wParamL dw ?
wParamH dw ?
HARDWAREINPUT ENDS
PHARDWAREINPUT typedef ptr HARDWAREINPUT
LPHARDWAREINPUT typedef ptr HARDWAREINPUT
endif
INPUT_MOUSE equ 0
INPUT_KEYBOARD equ 1
INPUT_HARDWARE equ 2
ifndef INPUT
INPUT STRUCT
type dd ?
UNION
mi MOUSEINPUT <>
ki KEYBDINPUT <>
hi HARDWAREINPUT <>
ENDS
INPUT ENDS
PINPUT typedef ptr INPUT
LPINPUT typedef ptr INPUT
endif
.Data
hInst HINSTANCE NULL
szClassName DB 'EasyCode2MainWindow', 0
szTitle DB 'Main window', 0
Handle DD 0
Buscar DB "ConsoleWindowClass", 0
.code
start:
Invoke GetModuleHandle, NULL
Mov hInst, Eax
Invoke GetCommandLine
Invoke EnvioTecla
Invoke ExitProcess, Eax
EnvioTecla Proc
LOCAL inputs[4]: INPUT
Invoke FindWindowEx, 0, 0, Offset Buscar, NULL
Mov Handle, Eax
.If Eax != NULL
Invoke SetForegroundWindow, Handle
.If Eax != 0
Invoke RtlZeroMemory, addr inputs, sizeof INPUT * 4
Mov inputs[0].type, INPUT_KEYBOARD
Mov inputs[0].ki.wVk, VK_CONTROL
Mov inputs[sizeof INPUT].type, INPUT_KEYBOARD
Mov inputs[sizeof INPUT].ki.wVk, VK_A
Mov inputs[2 * sizeof INPUT].type, INPUT_KEYBOARD
Mov inputs[2 * sizeof INPUT].ki.wVk, VK_A
Mov inputs[2 * sizeof INPUT].ki.dwFlags, KEYEVENTF_KEYUP
Mov inputs[3 * sizeof INPUT].type, INPUT_KEYBOARD
Mov inputs[3 * sizeof INPUT].ki.wVk, VK_CONTROL
Mov inputs[3 * sizeof INPUT].ki.dwFlags, KEYEVENTF_KEYUP
Invoke SendInput, 4, addr inputs, sizeof INPUT
.EndIf
.EndIf
Ret
EnvioTecla EndP
End start
Don't forget the includes ...
I'm not sure if the structures are already defined in MASM32 includes.
Thank you very much, Greenhorn. I've managed to do it, basically like you. but something fails me, it stays blocked, the key.
Code new:
.Const
.Data?
KEYBOARDINPUT struc
dtype DWORD ?
wVk WORD ?
wScan WORD ?
dwFlags DWORD ?
ttime DWord ?
dwExtraInfo DWORD ?
dummy1 db 8 dup (?)
KEYBOARDINPUT EndS
.Data
hInst HINSTANCE NULL
szClassName DB 'EasyCode2MainWindow', 0
szTitle DB 'Main window', 0
PrgSend DB "cmd.exe", 0
Handle DD 0
Buscar DB "ConsoleWindowClass", 0
Keys KEYBOARDINPUT <>
.Code
start:
Invoke GetModuleHandle, NULL
Mov hInst, Eax
Invoke GetCommandLine
Invoke EnvioTecla
Invoke ExitProcess, Eax
EnvioTecla Proc
Invoke FindWindowEx, 0, 0, Offset Buscar, NULL
Mov Handle, Eax
.If Eax != NULL
Invoke SetForegroundWindow, Handle
Mov Keys.dtype, INPUT_KEYBOARD
Mov Keys.wScan, 0
Mov Keys.ttime, 0
Mov Keys.dwExtraInfo, 0
Mov Keys.wVk, VK_CONTROL
Mov Keys.dwFlags, 0
Invoke SendInput, 1, Addr Keys, SizeOf Keys
Mov Keys.wVk, 'A'
Mov Keys.dwFlags, 0
Invoke SendInput, 1, Addr Keys, SizeOf Keys
Mov Keys.wVk, 'A'
Mov Keys.dwFlags, WM_KEYUP
Invoke SendInput, 1, Addr Keys, SizeOf Keys
Mov Keys.wVk, VK_CONTROL
Mov Keys.dwFlags, WM_KEYUP
Invoke SendInput, 1, Addr Keys, SizeOf Keys
.EndIf
Ret
EnvioTecla EndP
You must zero-out the whole structure(s) once before filling it.
I would send it as an array of structures. And you should send the key strokes only if the console window you are looking for is activated and has the keyboard focus, so check the return value of SetForegroundWindow.
Didn't my example work for you ?
Sorry, I didn't get to test it, because I already got the sendinput function working. But I try what you're telling me.
Thank you very much.