Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ejecutar bat en DOS desde una aplicacion VB6.

595 views
Skip to first unread message

@lejandro.(¯`·..o>

unread,
Sep 4, 2003, 12:10:41 PM9/4/03
to
HOla, necesita de su ayuda.

Deseo ejecutar un .bat desde Win2000 :

La forma 1 y la forma 2 no funcionan, pero si funciona la forma 2 en NT.
Alguien podra decirme como desde win2000 puedo ejecutar un .bat desde VB6.

Este es el codigo que uso.


Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String,
ByVal nCmdShow As Long) As Long
Private Sub Command1_Click()
Dim resultado As Long

'forma 1
resultado = Shell("D:\COPIA\prueba\factura.bat", vbHide)

'forma 2
WinExec "D:\COPIA\prueba\Factura.bat", 10

End Sub


Muchas gracias.

--
@lejandro.-


Adrian Di Ruggiero

unread,
Sep 4, 2003, 1:29:05 PM9/4/03
to
Hola !
Aqui te paso una serie de funciones y procedimientos.
Copia todo en un .BAS (sigo con la explicacion al final)

Option Explicit

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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 Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject
Lib "kernel32" (ByVal hHandle As Long, ByVal
dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32"
(ByVal lpApplicationName As String, ByVal lpCommandLine As
String, ByVal lpProcessAttributes As Long, ByVal
lpThreadAttributes As Long, ByVal bInheritHandles As Long,
ByVal dwCreationFlags As Long, ByVal lpEnvironment As
Long, ByVal lpCurrentDirectory As String, lpStartupInfo As
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION)
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal
hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32"
(ByVal hProcess As Long, lpExitCode As Long) As Long
Declare Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As
Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare Function FindWindow Lib "user32"
Alias "FindWindowA" (ByVal lpClassName As Long, ByVal
lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal
hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId
Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
As Long
Private Declare Function GetWindow Lib "user32" (ByVal
hwnd As Long, ByVal wCmd As Long) As Long

Private Const NILL = 0&
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_HIDE = 0
Private Const SW_MINIMIZE = 6
Private Const SW_NORMAL = 1
Private Const SW_MAXIMIZE = 3

Private Const GW_HWNDNEXT = 2

Function Rat(ByVal cFind As String, ByVal cWhere As
String) As Integer
'**************************************************
'*** Propósito: Determinar la última ocurrencia de
'*** un string en otro.
'*** Parámetros: cFind: Cadena a buscar
'*** cWhere: Cadena en donde buscar
'*** Retorna: Posición dentro de la cadena, o 0 si no
se encuentra
'*** Autor: Adrián Di Ruggiero
'*** Modificaciones/Observaciones:
'*** Ejemplo: n = Rat
("\", "c:\windows\system\shell32.exe")
'*** ? n -> 18
'**************************************************
On Error GoTo ErrHandler

Dim cBuffer As String, nCount As Integer

cBuffer = ""
For nCount = Len(cWhere) To 1 Step -1
cBuffer = cBuffer + Mid$(cWhere, nCount, 1)
Next
nCount = InStr(cBuffer, cFind)
If nCount <> 0 Then
Rat = (Len(cBuffer) - nCount) + 1
Else
Rat = 0
End If

End Function


Function InstanceToWnd(ByVal target_pid As Long) As Long
'*** Purpose :
'*** Parameters :
'*** Comments :

Dim test_hwnd As Long, test_pid As Long,
test_thread_id As Long
'Find the first window
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
'Check if the window isn't a child
If GetParent(test_hwnd) = 0 Then
'Get the window's thread
test_thread_id = GetWindowThreadProcessId
(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
'retrieve the next window
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop

End Function

Public Function ExecCmd(cmdline As String, Optional
intWaitSeconds As Integer = -1) As Long
'*** Purpose : Execute a shelled process.
'*** Parameters : cmdline : Program to run.
' intWaitSeconds: Optional. Seconds to
wait until program ends.
'*** Comments :

Dim proc As PROCESS_INFORMATION, Ret As Long
Dim start As STARTUPINFO, dwMilliseconds As Long
Dim strCurDir As String

If intWaitSeconds = -1 Then
dwMilliseconds = INFINITE
Else
dwMilliseconds = 1000& * intWaitSeconds
End If
If InStr(cmdline, "\") = 0 Then
strCurDir = App.Path
Else
strCurDir = Left(cmdline, Rat("\", cmdline) - 1)
End If
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = SW_HIDE
End With

' Start the shelled application:
Ret = CreateProcessA(vbNullString, cmdline, 0&, 0&,
1&, _
NORMAL_PRIORITY_CLASS, 0&, strCurDir, start, proc)

' Wait for the shelled application to finish:
Ret = WaitForSingleObject(proc.hProcess,
dwMilliseconds)
Call GetExitCodeProcess(proc.hProcess, Ret)

Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = Ret

If Ret <> 0 Then
'Kill process
Ret = InstanceToWnd(proc.dwProcessID)
Ret = SendMessage(Ret, WM_SYSCOMMAND, SC_CLOSE,
NILL)
End If
End Function
=================
Para ejecutar tu batch, invoca a la funcion "ExecCmd"
indicando en el primer parametro la ruta completa a tu
batch, y el segundo es el tiempo que le daras antes de
considerarlo "caido" (por timeout)

'Call Batch File
lngRet = ExecCmd(strNTBatchFile, intNTBatchTimeout)

El valor que devuelve sera 0 si todo anduvo bien, o el
numero de error que sucedio.
Cualquier cosa, mandame un mail.

Adrian


>-----Mensaje original-----

>.
>

José Manuel Agüero

unread,
Sep 4, 2003, 2:49:55 PM9/4/03
to
Hola, @lejandro:

La solución de Adrian tiene buena pinta. Sólo para una comprobación rápida, prueba:

resultado = Shell("cmd.exe /c D:\COPIA\prueba\factura.bat", vbHide)

No se si los NT anteriores al XP incluyen COMMAND.COM, pero por probar...:

resultado = Shell("command.com /c D:\COPIA\prueba\factura.bat", vbHide)

Saludos.


"@lejandro.(¯`·..o>" <janowN...@tutopia.com> escribió en el mensaje news:uVg3p7vc...@TK2MSFTNGP10.phx.gbl...

@lejandro.(¯`·..o>

unread,
Sep 5, 2003, 9:46:11 AM9/5/03
to
Gracias a ambos.

--
@lejandro.-


0 new messages