Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > API de Windows
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 12-01-2009
Charly911 Charly911 is offline
Miembro
 
Registrado: ene 2009
Ubicación: Bs As, Argentina
Posts: 11
Poder: 0
Charly911 Va por buen camino
Cool Ejecutar scripts .vbs -> Solucionado

Hola gente del foro, primero que nada me quiero presentar.
Soy Cristian, 19 años, Argentino, primer post
De programacion se poco, y lo que se lo aprendi por mi cuenta leyendo, copiando, probando....

Estoy haciendo un pequeño programa que ejecuta scripts de visual basic para instalar programas. Es una lista con CheckBox que revisa una por una y ejecuta un script a la vez, y espera a que termine el instalador para ejecutar el siguiente. El programa esta casi listo, ya que lo probe con ejecutables y funciono a la perfeccion. Pero luego lo probe con los scripts y no anda
Para la ejecucion use la funcion CreateProcess, ya que con esta puedo verificar si el proceso esta o no en ejecucion.

Codigo:

Código Delphi [-]
unit PostSateging;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, jpeg, ExtCtrls, SHELLAPI;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    CheckBox10: TCheckBox;
    CheckBox11: TCheckBox;
    Button1: TButton;
    CheckBox12: TCheckBox;
    procedure Button1Click(Sender: TObject);
          private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Form2: TForm2;

implementation

{$R *.dfm}


 function WinExecAndWait32(FileName:String; Visibility:integer):integer; //Esta funcion la copie de la web...
 var
   zAppName:array[0..512] of char;
   zCurDir:array[0..255] of char;
   WorkDir:String;
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
   Resultado,exitCode: DWord;
 begin
   StrPCopy(zAppName,FileName);
   GetDir(0,WorkDir);
   StrPCopy(zCurDir,WorkDir);
   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
   StartupInfo.cb := Sizeof(StartupInfo);

   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow := Visibility;
   CreateProcess(nil,
     zAppName,                      { pointer to command line string }
     nil,                           { pointer to process security attributes}
     nil,                           { pointer to thread security attributes}
     false,                         { handle inheritance flag }
     CREATE_NEW_CONSOLE or          { creation flags }
     NORMAL_PRIORITY_CLASS,
     nil,                           { pointer to new environment block }
     nil,                           { pointer to current directory name }
     StartupInfo,                   { pointer to STARTUPINFO }
     ProcessInfo);

   {Espera a que termine la ejecucion}
   {Wait until execution finish}
   Repeat
     exitCode := WaitForSingleObject( ProcessInfo.hProcess,1000);
     Application.ProcessMessages;
   Until (exitCode <> WAIT_TIMEOUT);
   GetExitCodeProcess(ProcessInfo.hProcess,Resultado);
   MessageBeep(0);
   CloseHandle(ProcessInfo.hProcess );
   Result:=Resultado;
 end;


procedure TForm1.Button1Click(Sender: TObject);
begin
if CheckBox1.Checked then WinExecAndWait32('C:\Documents and Settings\Charly\Escritorio\hola.vbs'{C:\Packages\ACCSS002003ENGC1\ACCSS002003ENGC1-JM.vbs'},1);
if CheckBox2.Checked then WinExecAndWait32('C:\Packages\OLPCA601312ENGC1\OLPCA601312ENGC1.vbs',1);
if CheckBox3.Checked then WinExecAndWait32('C:\Packages\Citrix 10.1\ica32pkg.vbs',1);
if CheckBox4.Checked then WinExecAndWait32('C:\Packages\install_flash_player\install_flash_player.vbs',1);
if CheckBox5.Checked then WinExecAndWait32('C:\Packages\ORACL000817ENGC1\ORACL000817ENGC1_VE_dep.vbs',1);
if CheckBox6.Checked then WinExecAndWait32('C:\Packages\DVLPR000045ENGC2\DVLPR000045ENGC2_VE_dep.vbs',1);
if CheckBox7.Checked then WinExecAndWait32('C:\Packages\PDFCT000093ENGC1\PDFCT000093ENGC1_deploy.vbs',1);
if CheckBox8.Checked then WinExecAndWait32('C:\Packages\PRJCT002003ENGC1P1\PRJCT002003ENGC1P1_deploy.vbs',1);
if CheckBox9.Checked then WinExecAndWait32('C:\Packages\SAPGU000640ENGC3\SAPGU00064ENGC3_VE_dep.vbs',1);
if CheckBox10.Checked then WinExecAndWait32('C:\Packages\SISLG000010SPAC1\SISLG000010SPAC1_deploy.vbs',1);
if CheckBox11.Checked then WinExecAndWait32('C:\Packages\Scripts\CONF_LOC.vbs',1);
if CheckBox12.Checked then WinExecAndWait32('C:\Packages\Scripts\RestartSystem.vbs',1);
end;

end.
Les agradeceria que me indiquen si es posible ejecutar este tipo de archivos con esta funcion, o si hay otra que me sirva.

Cristian.

-----------------------------------

Edit ->

Como la funcion CreateProcess solo puede ejecutar archivos .exe lo que hay que hacer para ejecutar cualquier archivo, en este caso un .vbs, se debe ejecutar el programa que lo abre diciendole que abra el archivo.
El codigo quedaria asi:

Código Delphi [-]
unit PostSateging;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, jpeg, ExtCtrls, SHELLAPI;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    CheckBox10: TCheckBox;
    CheckBox11: TCheckBox;
    Button1: TButton;
    CheckBox12: TCheckBox;
    procedure Button1Click(Sender: TObject);
          private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Form2: TForm2;

implementation

{$R *.dfm}


 function WinExecAndWait32(FileName:String; Visibility:integer):integer;
 var
   zAppName:array[0..512] of char;
   zCurDir:array[0..255] of char;
   WorkDir:String;
   StartupInfo:TStartupInfo;
   ProcessInfo:TProcessInformation;
   Resultado,exitCode: DWord;
 begin
   StrPCopy(zAppName,FileName);
   GetDir(0,WorkDir);
   StrPCopy(zCurDir,WorkDir);
   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
   StartupInfo.cb := Sizeof(StartupInfo);

   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow := Visibility;
   CreateProcess(nil,
     zAppName,                      { pointer to command line string }
     nil,                           { pointer to process security attributes}
     nil,                           { pointer to thread security attributes}
     false,                         { handle inheritance flag }
     CREATE_NEW_CONSOLE or          { creation flags }
     NORMAL_PRIORITY_CLASS,
     nil,                           { pointer to new environment block }
     nil,                           { pointer to current directory name }
     StartupInfo,                   { pointer to STARTUPINFO }
     ProcessInfo);

   {Espera a que termine la ejecucion}
   {Wait until execution finish}
   Repeat
     exitCode := WaitForSingleObject( ProcessInfo.hProcess,1000);
     Application.ProcessMessages;
   Until (exitCode <> WAIT_TIMEOUT);
   GetExitCodeProcess(ProcessInfo.hProcess,Resultado);
   MessageBeep(0);
   CloseHandle(ProcessInfo.hProcess );
   Result:=Resultado;
 end;


procedure TForm1.Button1Click(Sender: TObject);
begin
if CheckBox1.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\ACCSS002003ENGC1\ACCSS002003ENGC1-JM.vbs',1);
if CheckBox2.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\OLPCA601312ENGC1\OLPCA601312ENGC1.vbs',1);
if CheckBox3.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\Citrix 10.1\ica32pkg.vbs',1);
if CheckBox4.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\install_flash_player\install_flash_player.vbs',1);
if CheckBox5.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\ORACL000817ENGC1\ORACL000817ENGC1_VE_dep.vbs',1);
if CheckBox6.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\DVLPR000045ENGC2\DVLPR000045ENGC2_VE_dep.vbs',1);
if CheckBox7.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\PDFCT000093ENGC1\PDFCT000093ENGC1_deploy.vbs',1);
if CheckBox8.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\PRJCT002003ENGC1P1\PRJCT002003ENGC1P1_deploy.vbs',1);
if CheckBox9.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\SAPGU000640ENGC3\SAPGU00064ENGC3_VE_dep.vbs',1);
if CheckBox10.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\SISLG000010SPAC1\SISLG000010SPAC1_deploy.vbs',1);
if CheckBox11.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\Scripts\CONF_LOC.vbs',1);
if CheckBox12.Checked then WinExecAndWait32('C:\WINDOWS\system32\wscript.exe C:\Packages\Scripts\RestartSystem.vbs',1);
end;

end.

Cristian.

Última edición por Charly911 fecha: 12-01-2009 a las 13:15:05.
Responder Con Cita
  #2  
Antiguo 12-01-2009
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Poder: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
No se que quieres decir con "no anda".
- Significa que da error: ¿Qué error?
- Significa que se queda colgado: ¿Responde a algo?, ¿ejecuta alguno de los scrips?
- Alguna otra cosa?

Por lo que veo, solo debería ejecutarte el primero, porque en el bucle repeat...until solo sales si el código de salida es WAIT_TIMEOUT.
Supongo que la condición debería ser until (exitcode<>"Está Procesando") o until (exitcode<>0) o until (exitcode<>-1) algo así.

Código Delphi [-]
{Wait until execution finish}
   Repeat
     exitCode := WaitForSingleObject( ProcessInfo.hProcess,1000);
     Application.ProcessMessages;
   Until (exitCode <> WAIT_TIMEOUT);
Responder Con Cita
  #3  
Antiguo 12-01-2009
Charly911 Charly911 is offline
Miembro
 
Registrado: ene 2009
Ubicación: Bs As, Argentina
Posts: 11
Poder: 0
Charly911 Va por buen camino
Justo posteaste cuando estaba editando la solucion :P
El bucle anda bien, ya lo probe.
Fijate lo que edite, ahi estan los cambios...

Cristian.
Responder Con Cita
  #4  
Antiguo 12-01-2009
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Poder: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
Del help de Delphi 6:
Cita:
The WaitForSingleObject function returns when one of the following occurs:

· The specified object is in the signaled state.
· The time-out interval elapses.


DWORD WaitForSingleObject(

HANDLE hHandle, // handle of object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds
);


Parameters

hHandle

Identifies the object. For a list of the object types whose handles can be specified, see the following Remarks section.
Windows NT: The handle must have SYNCHRONIZE access. For more information, see Access Masks and Access Rights.

dwMilliseconds

Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.



Return Values

If the function succeeds, the return value indicates the event that caused the function to return.
If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError.
The return value on success is one of the following values:

Value Meaning
WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
WAIT_OBJECT_0 The state of the specified object is signaled.
WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled.


Remarks

The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters an efficient wait state. The thread consumes very little processor time while waiting for the object state to become signaled or the time-out interval to elapse.
Before returning, a wait function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one.
Responder Con Cita
  #5  
Antiguo 14-01-2009
Charly911 Charly911 is offline
Miembro
 
Registrado: ene 2009
Ubicación: Bs As, Argentina
Posts: 11
Poder: 0
Charly911 Va por buen camino
duilioisola:

Te agradezco por tu ayuda, pero como comente en el primer post, se muy poco de programacion y no comprendo muy bien que es lo que hace la funcion...
Igualmente ya la probe en repetidas ocaciones y funciona a la perfeccion

Un saludo Cristian.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Generacion de scripts AMO Oracle 2 10-10-2005 17:55:15
Ejecutar automaticamente scripts en tiempo de ejecucion meosre MySQL 1 30-09-2005 06:42:25
Scripts? JMGR Varios 2 14-06-2005 21:03:49
Componente para ejecutar scripts gendelphi Firebird e Interbase 1 02-09-2004 20:15:46
scripts en phpmyadmin __cadetill PHP 39 22-04-2004 00:10:20


La franja horaria es GMT +2. Ahora son las 20:06:22.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi