473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

ShellWait() Function

NeoPa
32,556 Expert Mod 16PB
Introduction :

The following code (module) provides the facility to execute a program, but synchronously. The Shell() function executes a command asynchronously, so returns control immediately after the command has been invoked, whether the command has completed execution or not. This is often not appropriate or convenient, so here is a version which causes the invoking code to wait until the command has terminated before resuming execution.

Solution :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. 'Windows API Variable Prefixes
  5. 'cb = Count of Bytes (32-bit)
  6. 'w  = Word (16-bit)
  7. 'dw = Double Word (32-bit)
  8. 'lp = Long Pointer (32-bit)
  9. 'b  = Boolean (32-bit)
  10. 'h  = Handle (32-bit)
  11. 'ul = Unsigned Long (32-bit)
  12.  
  13. Private Const conUseShowWindow = &H1&
  14. Private Const conNormalPriority = &H20&
  15. Private Const conInfinite = -1&
  16.  
  17. Private Type typStartupInfo
  18.     cbLen As Long
  19.     lpReserved As String
  20.     lpDesktop As String
  21.     lpTitle As String
  22.     dwX As Long
  23.     dwY As Long
  24.     dwXSize As Long
  25.     dwYSize As Long
  26.     dwXCount As Long
  27.     dwYCount As Long
  28.     dwFillAtt As Long
  29.     dwFlags As Long
  30.     wShowWindow As Integer
  31.     cbReserved2 As Integer
  32.     lpReserved2 As Long
  33.     hStdIn As Long
  34.     hStdOut As Long
  35.     hStdErr As Long
  36. End Type
  37.  
  38. Private Type typProcInfo
  39.     hProc As Long
  40.     hThread As Long
  41.     dwProcID As Long
  42.     dwThreadID As Long
  43. End Type
  44.  
  45. Private Declare Function CreateProcessA Lib "kernel32" ( _
  46.     ByVal lpApplicationName As Long, _
  47.     ByVal lpCommandLine As String, _
  48.     ByVal lpProcessAttributes As Long, _
  49.     ByVal lpThreadAttributes As Long, _
  50.     ByVal bInheritHandles As Long, _
  51.     ByVal dwCreationFlags As Long, _
  52.     ByVal lpEnvironment As Long, _
  53.     ByVal lpCurrentDirectory As Long, _
  54.     lpStartupInfo As typStartupInfo, _
  55.     lpProcessInformation As typProcInfo) As Long
  56. Private Declare Function WaitForSingleObject Lib "kernel32" ( _
  57.     ByVal hHandle As Long, _
  58.     ByVal dwMilliseconds As Long) As Long
  59. Private Declare Function CloseHandle Lib "kernel32" ( _
  60.     ByVal hObject As Long) As Long
  61.  
  62. 'ShellWait() executes a command synchronously (Shell() works asynchronously).
  63. Public Sub ShellWait(strCommand As String, _
  64.                      Optional intWinStyle As Integer = vbNormalFocus)
  65.     Dim objProcInfo As typProcInfo
  66.     Dim objStart As typStartupInfo
  67.  
  68.     'Initialize the typStartupInfo structure:
  69.     With objStart
  70.         .cbLen = Len(objStart)
  71.         .dwFlags = conUseShowWindow
  72.         .wShowWindow = intWinStyle
  73.     End With
  74.     'Start the shelled application:
  75.     Call CreateProcessA(lpApplicationName:=0&, _
  76.                         lpCommandLine:=strCommand, _
  77.                         lpProcessAttributes:=0&, _
  78.                         lpThreadAttributes:=0&, _
  79.                         bInheritHandles:=1&, _
  80.                         dwCreationFlags:=conNormalPriority, _
  81.                         lpEnvironment:=0&, _
  82.                         lpCurrentDirectory:=0&, _
  83.                         lpStartupInfo:=objStart, _
  84.                         lpProcessInformation:=objProcInfo)
  85.     'Wait for the shelled application to finish
  86.     Call WaitForSingleObject(hHandle:=objProcInfo.hProc, _
  87.                              dwMilliseconds:=conInfinite)
  88.     Call CloseHandle(hObject:=objProcInfo.hProc)
  89. End Sub
Sep 29 '08 #1
0 19452

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
4
by: Les Desser | last post by:
Can anyone explain why the string "C:\Program Files\WinZip\wzunzip" -t ZipFileName does nothing when run via the Shell function, but cmd /c "C:\Program Files\WinZip\wzunzip" -t ZipFileName ...
2
by: MLH | last post by:
How to avoid the 'User-defined Type not defined' problem in the following code snippet. How do I set up the User-defined Type to make this compile? Public Sub ShellWait(Pathname As String,...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
0
by: MLH | last post by:
I was wondering what the differences in resource requirements between calc.exe and notepad.exe might be and if such differences might explain why Access 97 locks up when I run...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.