Ver Mensaje Individual
  #4 (permalink)  
Antiguo 01/06/2013, 01:19
Avatar de erbuson
erbuson
 
Fecha de Ingreso: noviembre-2009
Mensajes: 701
Antigüedad: 14 años, 5 meses
Puntos: 53
Respuesta: Shell y devolución de codigos

Veras, yo tuve una necesidad similar, y te dejo aqui el código del módulo que utilizo desde hace unos años y hasta ahora funciona a la perfección.

Código vb:
Ver original
  1. Option Explicit
  2.  
  3. Private Type STARTUPINFO
  4.     cb As Long
  5.     lpReserved As String
  6.     lpDesktop As String
  7.     lpTitle As String
  8.     dwX As Long
  9.     dwY As Long
  10.     dwXSize As Long
  11.     dwYSize As Long
  12.     dwXCountChars As Long
  13.     dwYCountChars As Long
  14.     dwFillAttribute As Long
  15.     dwFlags As Long
  16.     wShowWindow As Integer
  17.     cbReserved2 As Integer
  18.     lpReserved2 As Long
  19.     hStdInput As Long
  20.     hStdOutput As Long
  21.     hStdError As Long
  22. End Type
  23.  
  24. Private Type PROCESS_INFORMATION
  25.     hProcess As Long
  26.     hThread As Long
  27.     dwProcessId As Long
  28.     dwThreadId As Long
  29. End Type
  30.  
  31. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  32.  
  33. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  34.     lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
  35.     lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  36.     ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  37.     ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  38.     lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  39.     PROCESS_INFORMATION) As Long
  40.  
  41.  
  42. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  43. Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
  44.  
  45. Private Const NORMAL_PRIORITY_CLASS = &H20&
  46. Private Const HIGH_PRIORITY_CLASS = &H80
  47.  
  48. Private Const INFINITE = -1&
  49.  
  50. Public Function ExecCmd(cmdline As String) As Long
  51.     Dim proc As PROCESS_INFORMATION
  52.     Dim start As STARTUPINFO
  53.     Dim ret As Long
  54.     start.cb = Len(start)
  55.     start.dwFlags = &H1
  56.     start.wShowWindow = 0
  57.     ret = CreateProcessA(0&, cmdline, 0&, 0&, 1&, HIGH_PRIORITY_CLASS, 0&, 0&, start, proc)
  58.     'Espera a que finalice la aplicación lanzada y devuelve su código de error
  59.    ret = WaitForSingleObject(proc.hProcess, INFINITE)
  60.     Call GetExitCodeProcess(proc.hProcess, ret)
  61.     Call CloseHandle(proc.hThread)
  62.     Call CloseHandle(proc.hProcess)
  63.     ExecCmd = ret
  64. End Function

En mi caso, la siguiente funcion forma parte del módulo pero fue por mi necesidad, puede servirte de ejemplo, esto es lo que debes adaptar a tu sistema:


Código vb:
Ver original
  1. Public Function ErrorDeNIF(ByVal QueNif As String) As Integer
  2.   Dim Retorno As Long
  3.   Retorno = ExecCmd("java -jar valnif.jar " & QueNif)
  4.   Select Case Retorno
  5.     Case Is = 0:  ErrorDeNIF = 0  ' Correcto
  6.    Case Is = 1:  ErrorDeNIF = 1  ' Incorrecto o no encontró valnif.jar
  7.    Case Is = -1: ErrorDeNIF = 2  ' Java no se ha ejecutado
  8.  End Select
  9. End Function

Espero te sirva tanto como a mi

Saludos
__________________
Agradecer a quien te enseñó, es enseñar lo que de él aprendiste.
Recuerda: Decir gracias, poco cuesta y mucho vale ...