Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 21, 1997

A Better Substitute for GetModuleUsage

To synchronously shell an application from a 16-bit VB application, some people write code like this:
 Const HINSTANCE_ERROR% = 32
Dim hInstChild As Integer
'Shell program, if Shell worked, enter loop
hInstChild = Shell(strExeName, intCmdShow)
If hInstChild >= HINSTANCE_ERROR Then
    While GetModuleUsage(hInstChild)
        DoEvents
    Wend
End If
This code relies on a 16-bit-only API function, GetModuleUsage. Several 32-bit workarounds have been published, but here is the simplest and most reliable solution I've seen:

 Declare Function OpenProcess Lib "Kernel32" _
    (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "Kernel32" _
    (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
Private Function SyncShell(ByVal pathname As String, _
    windowstyle As Integer) As Boolean
Dim ProcessID As Long
Dim ProcessHandle As Long
' In VB4, an error occurs if Shell
' fails to start the program
On Error GoTo SyncShell_Error
' Shell the program, get its handle, 
' and wait for it to terminate
ProcessID = Shell(pathname, windowstyle)
ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID)
WaitForSingleObject ProcessHandle, INFINITE
SyncShell = True
Exit Function
SyncShell_Error:
    On Error GoTo 0
    SyncShell = False
    Exit Function
End Function
Bob Voges
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date