Iniciar Sesión

Ver la Versión Completa : [Sintaxis] Cerrar ventana de consola...


Fito
14 de junio de 2019, 14:10
Hola Amigos:

Para ciertos procesos me queda muy cómodo hacer display y que me vaya mostrando en la consola. Hay alguna manera de poder decirle por programa que la consola se cierre?

Saludos.

Fito...

Kuk
14 de junio de 2019, 20:19
Fito, por programa no se puede.

Puedes intentarlo vía WinAPI, con la función FindWindow y luego pasarle WM_CLOSE por CallWindowProc.

dmosca
27 de agosto de 2019, 20:45
Kuk buenas tardes.

tambien necesito cerrar la consola, pero no tengo experiencia con WinApi...

por favor podrias explicar como hacer el cierre de consola o donde tengo info para investigar

muchas gracias
saludos

fastpho
29 de agosto de 2019, 15:28
Buenas voy a subir un ejemplo de como cerrar programas abierto , por ejemplo la calculadora de windows , la idea era para ver si se podia cerrar la consola de cobol , pero al interntar cerrarla fuerza la salida
Special-Name

symbolic constant
gw_hwndfirst is 0
gw_hwndnext is 2
.


Working
*
* You must pass this subroutine 4 parameters:
*
* TITLE-BAR-STRING = PIC X(1024) = The text of the title bar of the window to locate
*
*
* FOUND-WINDOW-SW PIC 9(4) COMP-5 = 0 = Did not find the window
* = 1 = Found the window
*
* FOUND-WINDOW-HANDLE PIC S(9) COMP-5 = If the window is found, this is the handle to it
*
* RETURN-VALUE PIC S9(9) COMP-5 = Success or failure
* = 0 = Normal completion
* = 1 = Did not pass a title bar string
* = 2 = Could not get the handle to this window
* = 3 = Could not get the handle of the first window

01 get-this-window-handle is global.
05 this-window-handle pic s9(9) comp-5 value 0.
05 filler pic x(12) value spaces.

01 title-bar-search-string pic x(1025) value spaces is global.
01 title-bar-index pic 9(9) comp-5 value 0 is global.

01 child-window-handle pic s9(9) comp-5 value 0 is global.
01 parent-window-handle pic s9(9) comp-5 value 0 is global.

01 length-of-title-bar-buffer pic s9(9) comp-5 is global.
01 title-bar-buffer-max pic s9(9) comp-5 is global.
01 title-bar-buffer pic x(1025) value spaces is global.

01 hwnd-type pic 9(9) comp-5 is global.

01 null-value pic s9(9) comp-5 value 0 is global.
*----------
01 title-bar-string pic x(1024) is global external.
01 found-window-sw pic 9(4) comp-5 is global external.
01 found-window-handle pic s9(9) comp-5 is global external.
01 return-value pic s9(9) comp-5 is global external.




WORKING-STORAGE SECTION.
* You must pass this subroutine 4 parameters:
*
* TITLE-BAR-STRING = PIC X(1024) = The text of the title bar of the window to locate
*
*
* FOUND-WINDOW-SW PIC 9(4) COMP-5 = 0 = Did not find the window
* = 1 = Found the window
*
* FOUND-WINDOW-HANDLE PIC S(9) COMP-5 = If the window is found, this is the handle to it
*
* RETURN-VALUE PIC S9(9) COMP-5 = Success or failure
* = 0 = Normal completion
* = 1 = Did not pass a title bar string
* = 2 = Could not get the handle to this window
* = 3 = Could not get the handle of the first window

PROCEDURE DIVISION.
move 0 to return-value
move 0 to found-window-sw
move 0 to found-window-handle

compute title-bar-buffer-max = function length(title-bar-buffer)
MOVE title-bar-string TO "Caption" OF CmStatic1
INVOKE CmStatic1 "Refresh"
* Let's make sure they passed the title bar string
*
if title-bar-string not > " "
move 1 to return-value
INVOKE POW-SELF "CloseForm"
exit program
end-if
*
* Let's get the handle of this window
*
call "GetTopWindow" with STDCALL using
by value null-value
returning this-window-handle
*
* Now, let's find the length of the search string
*
move title-bar-string to title-bar-search-string
perform varying title-bar-index from 1025 by -1
until title-bar-search-string(title-bar-index:1) not = " "
continue
end-perform
*
* Now, let's get the first window
*
move gw_hwndfirst to hwnd-type
call "GetWindow" with STDCALL using
by value this-window-handle
by value hwnd-type
returning child-window-handle
if child-window-handle = 0
move 3 to return-value
INVOKE POW-SELF "CloseForm"
exit program
end-if

move gw_hwndnext to hwnd-type
*
* Now, let's see if this is the target window, if not, loop through all open windows
*
perform until child-window-handle = 0
*
* Let's get the title bar text of this window
*
call "GetWindowTextA" with STDCALL using
by value child-window-handle
by reference title-bar-buffer
by value title-bar-buffer-max
returning length-of-title-bar-buffer *> Note, the length returned does NOT include the NULL byte
*
* If the window has a title bar, is it the one we're looking for?
*
if length-of-title-bar-buffer > 0
if title-bar-search-string(1:title-bar-index) = title-bar-buffer(1:title-bar-index)
move 1 to found-window-sw
move child-window-handle to found-window-handle
move "encontrado" to "Caption" OF CmStatic2
INVOKE CmStatic2 "Refresh"
move 0 to return-value
INVOKE POW-SELF "CloseForm"
exit program
end-if
end-if
*
* Get the next window
*
move child-window-handle to this-window-handle

call "GetWindow" with STDCALL using
by value this-window-handle
by value hwnd-type
returning child-window-handle

end-perform
*
* Didn't find the window
*
move "no encontrado" to "Caption" OF CmStatic2
INVOKE CmStatic2 "Refresh"

move 0 to return-value
move 0 to found-window-sw
move 0 to found-window-handle
INVOKE POW-SELF "CloseForm"
exit program
.



Estas lineas deberian ir en prg que in mi caso se llama findwindow.dll
y lo que hace es buscar el titulo de la ventana a buscar en el parametro title-bar-string
Luego desde otro programa lleno esa variable y en un timer sale a buscar si existe tal programa.
objeto timer coloco estas lineas

move 0 to found-window-sw
move 0 to found-window-handle
move 0 to return-value
* MOVE "Console : PRUEBA" TO title-bar-string
MOVE "Calculadora" TO title-bar-string
INVOKE POW-SELF "OpenForm" USING "FINDWINDOW" "FINDWINDOW.DLL".


if return-value not = 0
* display "Error - FINDWINDOW failed"
move return-value to display-return-value
* display "Return-value = " , display-return-value

EXIT PROGRAM
end-if.
if found-window-sw = 1
display "EL PROGRAMA SE ESTA EJECUTANDO"
display "found-window-handle" , found-window-handle
EXIT PROGRAM

end-if.
if found-window-sw = 0
display "EL PROGRAMA NO SE ESTA EJECUTANDO"
display "found-window-handle" , found-window-handle
EXIT PROGRAM

end-if.

Genero un boton que cierra tal programa

ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
*private Const SC_CLOSE = &HF060&
*Private Const WM_SYSCOMMAND = &H112
01 null-value pic s9(9) comp-5 value 0 .
PROCEDURE DIVISION.
*cierro el programa

call "SendMessageA" with STDCALL using
by value found-window-handle
by value WM_SYSCOMMAND
by value SC_CLOSE
by value null-value.


Espero haber sido claro ....
Saludos

dmosca
29 de agosto de 2019, 20:34
:ojo1::ojo1:Gracias por responder

voy a aplicarlo...

informo como resulta.

saludos

---------- Post añadido : 15:34 ---------- Post anterior : 14:43 ----------

1ª consulta

que libreria debo cargar para poder compilar?

fastpho
29 de agosto de 2019, 22:20
dmosca, La libreria es user32.lib y me olvide de poner el valor
de symbolic constant
SC_CLOSE is 61536
WM_SYSCOMMAND is 274
.

dmosca
29 de agosto de 2019, 22:30
fastpho: este es el error:

Compile resource C:\Cuadros\Main\Debug\Main.rc ...

Linking C:\Cuadros\Debug\Main.exe ...
Cerrar.obj : error LNK2001: unresolved external symbol _GETWINDOWTEXTA@12
Cerrar.obj : error LNK2001: unresolved external symbol _GETWINDOW@8
Cerrar.obj : error LNK2001: unresolved external symbol _GETTOPWINDOW@4
C:\Cuadros\Debug\Main.exe : fatal error LNK1120: 3 unresolved externals

** The build has failed **

fastpho
29 de agosto de 2019, 23:48
dmosca, en las opcion de compilacion te debe faltar ALPHAL(WORD)