开发者

Prevent windows from queuing shellexecute requests

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 fi开发者_C百科x 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


I think you might have to use CreateProcess instead?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜