tags:

views:

52

answers:

3
Win.ShellExecute 0, "open", "C:\dir\program.exe", "arguments", vbNullString, SW_SHOWNORMAL
Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL

I want google.com to open regardless of whether or not program.exe is still running. How do I fix this? I would rather avoid things like calling "start."

Both of these calls happen pretty much instantly, and the VB program continues running. However, on both Vista and XP, google.com does not open until program.exe closes. If the application which called shellexecute closes before program.exe closes, google.com will still open once program.exe is closed.

Note:

I have found that having program.exe call doevents every 100ms or so fixes the problem, but obviously this is somewhat of a hack.

Note2: Below is an example implementation of program.exe. Yes, I realize that changing program.exe will fix this (i.e. adding a doevents call).

Option Explicit    
Public Sub Main()
    Do Until False
        Sleep 100
    Loop
End Sub
A: 

I think you might have to use CreateProcess instead?

jabley
Well, I tried createprocess and it did not help.
Brian
A: 

You may want to use the Process API rather than ShellExecute

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Private Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessId As Long
      dwThreadId As Long
End Type

Private Type STARTUPINFO
      cb As Long
      lpReserved As String
      lpDesktop As String
      lpTitle As String
      dwX As Long
      dwY As Long
      dwXSize As Long
      dwYSize As Long
      dwXCountChars As Long
      dwYCountChars As Long
      dwFillAttribute As Long
      dwFlags As Long
      wShowWindow As Integer
      cbReserved2 As Integer
      lpReserved2 As Long
      hStdInput As Long
      hStdOutput As Long
      hStdError As Long
End Type

If you need to halt until it is done you this

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

The code to use look something like this

ReturnValue = CreateProcess(NullString, AppPathString, ByVal 0&, ByVal 0&, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, NullString, StartupInformation, ProcessInformation)
Do While WaitForSingleObject(ProcessInformation.hProcess, 0) <> 0
    DoEvents
Loop
RS Conley
Actually, my problem is that I *don't* want to wait until it is done, rather than that I do.
Brian
Then don't use Waitforsingleobject. I put that up there to lay out all the common options with using the Process API. Once you execute CreateProcess the other application start running and CreateProcess returns.
RS Conley
Well, I tried createprocess and it did not help.
Brian
A: 

Have you tried using the intrinsic Shell to launch Program.exe, rather than ShellExecute? (I'm assuming Win.ShellExecute is the API call of the same name - you could include the declaration in your question.)

Also, tyranid is right that giving your application a form may solve the problem. I've had odd behaviour from VB6 apps with no forms.

MarkJ
@Markj: The "real" app does have a form. And the ideal is to fix it without changing program.exe.
Brian
Good idea. I tried using both a shell32.shell object and calling the shell function. The former behaves the same and the latter, your suggestion, blocks completely until the program exits, which is even worse.
Brian
GIving my test application a form had no effect.
Brian