OA_Shell.bas 2.5 KB

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