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% = 32Dim hInstChild As Integer'Shell program, if Shell worked, enter loophInstChild = Shell(strExeName, intCmdShow)If hInstChild >= HINSTANCE_ERROR Then    While GetModuleUsage(hInstChild)        DoEvents    WendEnd 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 LongDeclare Function WaitForSingleObject Lib "Kernel32" _    (ByVal hHandle As Long, _    ByVal dwMilliseconds As Long) As LongConst SYNCHRONIZE = &H100000Const INFINITE = &HFFFFFFFFPrivate Function SyncShell(ByVal pathname As String, _    windowstyle As Integer) As BooleanDim ProcessID As LongDim ProcessHandle As Long' In VB4, an error occurs if Shell' fails to start the programOn Error GoTo SyncShell_Error' Shell the program, get its handle, ' and wait for it to terminateProcessID = Shell(pathname, windowstyle)ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID)WaitForSingleObject ProcessHandle, INFINITESyncShell = TrueExit FunctionSyncShell_Error:    On Error GoTo 0    SyncShell = False    Exit FunctionEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: