Shell.bas 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. Option Compare Database
  2. Private Const STARTF_USESHOWWINDOW& = &H1
  3. Private Const NORMAL_PRIORITY_CLASS = &H20&
  4. Private Const INFINITE = -1&
  5. Private Type STARTUPINFO
  6. cb As Long
  7. lpReserved As String
  8. lpDesktop As String
  9. lpTitle As String
  10. dwX As Long
  11. dwY As Long
  12. dwXSize As Long
  13. dwYSize As Long
  14. dwXCountChars As Long
  15. dwYCountChars As Long
  16. dwFillAttribute As Long
  17. dwFlags As Long
  18. wShowWindow As Integer
  19. cbReserved2 As Integer
  20. lpReserved2 As Long
  21. hStdInput As Long
  22. hStdOutput As Long
  23. hStdError As Long
  24. End Type
  25. Private Type PROCESS_INFORMATION
  26. hProcess As Long
  27. hThread As Long
  28. dwProcessID As Long
  29. dwThreadID As Long
  30. End Type
  31. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  32. hHandle As Long, ByVal dwMilliseconds As Long) As Long
  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. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
  41. hObject As Long) As Long
  42. Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
  43. Dim proc As PROCESS_INFORMATION
  44. Dim start As STARTUPINFO
  45. Dim ret As Long
  46. ' Initialize the STARTUPINFO structure:
  47. With start
  48. .cb = Len(start)
  49. If Not IsMissing(WindowStyle) Then
  50. .dwFlags = STARTF_USESHOWWINDOW
  51. .wShowWindow = WindowStyle
  52. End If
  53. End With
  54. ' Start the shelled application:
  55. ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
  56. NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  57. ' Wait for the shelled application to finish:
  58. ret& = WaitForSingleObject(proc.hProcess, INFINITE)
  59. ret& = CloseHandle(proc.hProcess)
  60. End Sub
  61. Public Function cmd(ByVal command As String, Optional WindowStyle As Long = vbHide, Optional dir As String = "")
  62. If Len(dir) = 0 Then dir = CurrentProject.Path
  63. Call ShellWait("cmd.exe /r cd " & dir & " & " & command, WindowStyle)
  64. End Function