Discussion:
Cerrar aplicacion abierta con ShellExecute
(demasiado antiguo para responder)
hba
2004-05-13 07:12:43 UTC
Permalink
Hola. Tiene que ser una tontería pero no encuentro nada.

El tema es que en nuestra aplicación hay una apartado que sirve para
registrar documentos de cualquier tipo (word, excel, pdf, ficheros de texto,
imagenes, etc) Luego tiene un botón de imprimir y para ciertos tipos de
documento (doc, xls, txt) tiene integrado un visor que permite su impresion
pero para otros (pdf) pues no.

Lo que hacemos es un shellexecute(me.hwnd,"print","fichero",0&,"C:\",1)

La pregunta... ¿como cierro luego el acrobat reader???

Gracias y saludos
Rubén Vigón
2004-05-13 07:19:44 UTC
Permalink
Determining the end of a shelled application using «WaitForSingleObject»
http://vbnet.mvps.org/faq/main/waitforsingleobject2.htm

Determining the end of a shelled application using «GetExitCodeProcess»
http://vbnet.mvps.org/faq/main/getexitcprocess.htm

Determining the end of a shelled application using «GetModuleUsage»
http://vbnet.mvps.org/faq/main/getmoduleusage.htm

Un saludo!

Rubén Vigón
Microsoft MVP Visual Basic
http://www.mvp-access.com/rubenvigon
Mikel
2004-05-13 07:43:55 UTC
Permalink
Yo estoy en una situación parecida. Necesito cerrar un programa sin mandarle
por sendkeys altF4 y lo intento através de closehandle pero me devuelve el
error de handle no válido, aún siendo el handle que le mando el de la
aplicación que quiero cerrar. ¿Seria necesario usar WaitForSingleObject o
GetExitCodeProcess?
Saludos
hba
2004-05-13 07:57:16 UTC
Permalink
¿como has conseguido el handle? por que lo que devuelve el shellexecute no
tienen nada que ver con el handle.

Gracias
Post by Mikel
Yo estoy en una situación parecida. Necesito cerrar un programa sin mandarle
por sendkeys altF4 y lo intento através de closehandle pero me devuelve el
error de handle no válido, aún siendo el handle que le mando el de la
aplicación que quiero cerrar. ¿Seria necesario usar WaitForSingleObject o
GetExitCodeProcess?
Saludos
Mikel
2004-05-13 08:05:01 UTC
Permalink
Tengo el retval que me devuelve la shell y tengo el handle que lo cojo
haciendo un getforegroundwindow tras hacer un appactivate. Los dos valores
son correctos. Tampoco estaria mal si hubiera una manera de cerrar un
programa através del retval de su shell.
unknown
2004-05-13 08:20:59 UTC
Permalink
Post by Mikel
Tengo el retval que me devuelve la shell y tengo el handle que lo cojo
haciendo un getforegroundwindow tras hacer un appactivate. Los dos
valores son correctos. Tampoco estaria mal si hubiera una manera de
cerrar un programa através del retval de su shell.
Private Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccessas As Long, ByVal _
bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hprocess As Long, ByVal uExitCode As Long) As Long

Private retval As Long

Private Sub Command1_Click()

retval = Shell("calc.exe")
End Sub

Private Sub Command2_Click()
Dim hprocess As Long

hprocess = OpenProcess(PROCESS_QUERY_INFORMATION Or _
PROCESS_ALL_ACCESS, 0, retval)
TerminateProcess hprocess, 1
End Sub
Mikel
2004-05-13 08:23:02 UTC
Permalink
Sendmessage todoterreno lo soluciona todo

R = SendMessage(H, WM_CLOSE, 0, 0&)

Salu2
hba
2004-05-13 09:45:17 UTC
Permalink
Gracias a todos, pero creo que no me habéis entendido

no quiero usar el shell
estoy usando el shellexecute para poder enviarle el "print"

¿Alguna idea de como pillar el handle cuando se ha abierto con el
shellexecute?

Gracias.
Post by Mikel
Tengo el retval que me devuelve la shell y tengo el handle que lo cojo
haciendo un getforegroundwindow tras hacer un appactivate. Los dos valores
son correctos. Tampoco estaria mal si hubiera una manera de cerrar un
programa através del retval de su shell.
Mikel
2004-05-13 10:35:59 UTC
Permalink
Prueba a hacerlo con un GetForegroundWindow para coger el handle, eso si haz
una appactivate del retval primero y si ves q no le da tiempo a coger el
handle metele un sleep entre los dos.
Saludos!
Rubén Vigón
2004-05-13 10:45:47 UTC
Permalink
Utiliza «ShellExecuteEx» en lugar de «ShellExecute»; ésta función recibe una estructura de tipo SHELLEXECUTEINFO cuyo componente «hProcess» recibe el manejador del proceso (cuando el componente «fMask» se establece a SEE_MASK_NOCLOSEPROCESS); una vez obtenido el «hProcess», puedes esperar a que termine el proceso y cerrarlo mediante:

Dim exitCode As Long
Do
Call GetExitCodeProcess(hProcess, exitCode)
DoEvents
Loop While exitCode = STATUS_PENDING
Call CloseHandle(hProcess)

[...] hProcess
Handle to the newly started application. This member is set on return and is always NULL if fMask is not set to SEE_MASK_NOCLOSEPROCESS

Un saludo!

Rubén Vigón
Microsoft MVP Visual Basic
http://www.mvp-access.com/rubenvigon

Loading...