防止窗口排队shellexecute请求

时间:2009-11-18 19:45:48

标签: winapi vb6

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

无论program.exe是否仍在运行,我都希望google.com能够打开。我该如何解决?我宁愿避免像叫“开始”这样的事情。

这两个调用都会立即发生,VB程序继续运行。但是,在Vista和XP上,google.com在program.exe关闭之前不会打开。如果在program.exe关闭之前调用shellexecute的应用程序关闭,则一旦program.exe关闭,google.com仍会打开。

注意:

我发现每100ms左右有一次program.exe调用doevents修复问题,但显然这有点像黑客。

注2: 下面是program.exe的示例实现。是的,我意识到更改program.exe将解决此问题(即添加doevents电话)。

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

3 个答案:

答案 0 :(得分:0)

我认为您可能不得不使用CreateProcess?

答案 1 :(得分:0)

您可能希望使用Process API而不是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

如果您需要暂停,直到完成,

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

使用的代码看起来像这样

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

答案 2 :(得分:0)

您是否尝试使用内在Shell启动Program.exe而不是ShellExecute? (我假设Win.ShellExecute是同名的API调用 - 您可以在问题中包含声明。)

此外,tyranid是正确的,为您的应用程序提供表单可以解决问题。我有VB6应用程序的奇怪行为,没有任何形式。