473,890 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ShellWait() Function

32,584 Recognized Expert Moderator MVP
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
  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)
  13. Private Const conUseShowWindow = &H1&
  14. Private Const conNormalPriority = &H20&
  15. Private Const conInfinite = -1&
  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
  38. Private Type typProcInfo
  39.     hProc As Long
  40.     hThread As Long
  41.     dwProcID As Long
  42.     dwThreadID As Long
  43. End Type
  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
  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
  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 19742

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

Similar topics

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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
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 invisible() { alert("invisible() called"); } }
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
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 works.
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, Optional WindowStyle As Long) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim ret As Long ' Initialize the STARTUPINFO structure:
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
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 constructors (these three I knew about), but also conditional function definitions, as described in http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions ]. An example: function fun() {
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' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
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 ShellWait("notepad.exe"). It does NOT lock up if I run ShellWait("calc.exe"). Ideas?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.