OA_Shell.bas 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Option Compare Database
  2. '****
  3. '*
  4. '* Shell: use shell commands
  5. '*
  6. '****
  7. Private Const STARTF_USESHOWWINDOW& = &H1
  8. Private Const NORMAL_PRIORITY_CLASS = &H20&
  9. Private Const INFINITE = -1&
  10. Private Type STARTUPINFO
  11. cb As Long
  12. lpReserved As String
  13. lpDesktop As String
  14. lpTitle As String
  15. dwX As Long
  16. dwY As Long
  17. dwXSize As Long
  18. dwYSize As Long
  19. dwXCountChars As Long
  20. dwYCountChars As Long
  21. dwFillAttribute As Long
  22. dwFlags As Long
  23. wShowWindow As Integer
  24. cbReserved2 As Integer
  25. lpReserved2 As Long
  26. hStdInput As Long
  27. hStdOutput As Long
  28. hStdError As Long
  29. End Type
  30. Private Type PROCESS_INFORMATION
  31. hProcess As Long
  32. hThread As Long
  33. dwProcessID As Long
  34. dwThreadID As Long
  35. End Type
  36. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  37. hHandle As Long, ByVal dwMilliseconds As Long) As Long
  38. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  39. lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
  40. lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  41. ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  42. ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  43. lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  44. PROCESS_INFORMATION) As Long
  45. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
  46. hObject As Long) As Long
  47. Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
  48. ' run a shell command and waits for its ending before return
  49. Dim proc As PROCESS_INFORMATION
  50. Dim start As STARTUPINFO
  51. Dim ret As Long
  52. ' Initialize the STARTUPINFO structure:
  53. With start
  54. .cb = Len(start)
  55. If Not IsMissing(WindowStyle) Then
  56. .dwFlags = STARTF_USESHOWWINDOW
  57. .wShowWindow = WindowStyle
  58. End If
  59. End With
  60. ' Start the shelled application:
  61. ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
  62. NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  63. ' Wait for the shelled application to finish:
  64. ret& = WaitForSingleObject(proc.hProcess, INFINITE)
  65. ret& = CloseHandle(proc.hProcess)
  66. End Sub
  67. Public Function cmd(ByVal command As String, Optional WindowStyle As Long = vbHide, Optional in_dir As String = "")
  68. ' runs a comand with windows command line
  69. If Len(in_dir) = 0 Then in_dir = CurrentProject.path
  70. Call ShellWait("cmd.exe /r cd " & in_dir & " & " & command, WindowStyle)
  71. End Function