|
-
Nov 11th, 2002, 03:35 PM
#1
[Resolved] ShellExcecute - wait
Good Morning (Night) all,
I want to set the mouse to 'HourGlass' until ShellExcecute has
loaded the appropriate app and file, then I want to set the mouse back to 'Default' (I know the sytax etc for the mouse).
So, what I'm after is the Return, from that API call, to determine the app has loaded. - I guess something similar to SendKeys [wait]
VB Code:
ShellExecute Me.hwnd, vbNullString, sPathName, vbNullString, "C:\", SW_SHOWNORMAL
Regards,
Bruce.
Last edited by Bruce Fox; Nov 14th, 2002 at 07:11 PM.
-
Nov 11th, 2002, 03:41 PM
#2
Lively Member
Here's a ShellWait that I use sometimes for similiar situations...
VB Code:
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 Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Public Function ShellWait(Pathname As String, Optional WindowStyle As Long)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = &H1
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
&H20&, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, -1&)
ret& = CloseHandle(proc.hProcess)
End Function
Hope it helps
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 11th, 2002, 03:44 PM
#3
Thanks for the reply technitaes,
I was hoping for something less intense 
Cheers,
Bruce.
-
Nov 11th, 2002, 03:48 PM
#4
Frenzied Member
That's the standard way to do a 'shell and wait', it's in MSDN this way.
ShellExecute and ShellExecuteEx don't allow you to wait on process termination of the shelled process.
-
Nov 11th, 2002, 03:52 PM
#5
Thanks Jim,
I kinda figured that'd be the case, I'm just plugging it into a Module now 
Regards,
Bruce.
-
Nov 11th, 2002, 03:55 PM
#6
Lively Member
Well, the thing about it is you can copy and past this into a module, save it as something like "modShellWait.bas", copy it to a back up directory, then copy it into any programs you write in the future that need the functionality, and all you need to do at that point is call the public function ...
VB Code:
MousePointer = vbHourglass
ShellWait "C:\SomeDir\SomeFile.bat"
MousePointer = vbDefault
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 11th, 2002, 03:58 PM
#7
Yep, thats what I've done 
Thanks again for your time. Just testing it now.
Bruce.
-
Nov 11th, 2002, 04:47 PM
#8
Hmmmm,
Dosn't seem to be working.
I can hear the CD run up (thats where the source file is), however,
there never is an instance of the associated app!
I have steped thru the API to no avail 
I'm using:
VB Code:
Call modStartApp.ShellWait(sPathName)
Running on Windows 2000
Last edited by Bruce Fox; Nov 11th, 2002 at 05:07 PM.
-
Nov 11th, 2002, 05:20 PM
#9
Lively Member
Hmmm...
What OS are you running? I haven't tested it on any machines beyond 98SE/ME. I think we're running an app that's using it on an NT box, but I'd have to double check to be sure.
As you can tell, we're a little behind in updating our machines. The new bells and whistles haven't appeared cost effective as of yet.
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 11th, 2002, 05:24 PM
#10
Win 2000 (in an Access 2000 db VBA).
-
Nov 11th, 2002, 05:43 PM
#11
Lively Member
Well, it could be the Win2k or it could be the Access db. I've only run it in VB6 apps on the afore mentioned OS's.
Wait...
I remember my boss having a little trouble with an app he was using this in. He's out today, but I can check with him tomorrow and see if he remembers what it was and how he fixed it. Just so I'll know for reference, is the app you calling a Windows GUI program, some kind of batch file/dos file, or something else?
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 11th, 2002, 05:49 PM
#12
The files range from .txt, pdf, .jpg's etc.
I have the file (path) and FileName listed in a Recorset, the user
can dblClick on the FileName and have the file open with its associated
application. (The ShellExcecute worked well, however, I was
adding the Hourgalss as a user prompt etc as previously discussed).
Thanks again technitaes.
Bruce.
-
Nov 11th, 2002, 05:56 PM
#13
There shouldn't be any problems running the code on Win2k. The problem is the following part of the code:
VB Code:
If Not IsMissing(WindowStyle) Then
.dwFlags = &H1
.wShowWindow = WindowStyle
End If
Since WindowStyle is declared as an optional Long argument it is never missing. The IsMissing can only be used with Variants. If you don't pass a value to WindowStyle the default value will be 0 which is the same as vbHide. So the code is executing the shelled app but the window is hidden. Try using this call instead:
VB Code:
Call modStartApp.ShellWait(sPathName, vbNormalFocus)
But as I have understood your original question you just want to show the hourglass while the shelled application is loading. This code will stop the execution of your application until the shelled application is closed.
Best regards
-
Nov 11th, 2002, 06:02 PM
#14
Ahhh...
Thanks Joacim, Correct - I don't want to wait till the opened app is closed....
(For what its worth, I had set the WindowStyle to no avail)
To re-ittereate I need the Hourglass just for the duration the
(associated) app is loading.
Regards to All Con
-
Nov 11th, 2002, 07:10 PM
#15
*Thought* Can I ShellExcecute, capture the Hwnd and then use
an API to monitor the Hwnd till it's loaded (checking in a Do-Loop).
Bruce.
-
Nov 11th, 2002, 09:31 PM
#16
Ideas?
-
Nov 12th, 2002, 06:59 AM
#17
-
Nov 12th, 2002, 09:04 AM
#18
Lively Member
Bruce,
Sorry I misunderstood exactly what you were trying to accomplish. You can use your loop idea though, to check for the loading of another program by using the FindWindow API.
VB Code:
'Module General Declarations
Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
'Same Module
Public Function ApplicationLoaded(ByVal Title As String) As Boolean
Dim intWindowHandle As Integer
intWindowHandle = FindWindow(0&, Title)
ApplicationLoaded = (intWindowHandle <> 0)
End Function
'
'Some Form ...
MousePointer = vbHourglass
Shell "Notepad.exe"
Do While Not ApplicationLoaded("Untitled - NotePad")
Me.Refresh
Loop
MousePointer = vbDefault
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 12th, 2002, 03:15 PM
#19
technitaes,
Thanks again for your response.
That was the approach I've been playing with. However, I must use
ShellExcecute as I'm not sure what the app will be.
Having said that, I can't use that method with that ApplicationLoaded API.
So, I'm stuck now trying to incorporate an API that can give me that hwnd of the ShellExec app, and loop until that has loaded.
Again, appreciate your help 
Bruce.
-
Nov 12th, 2002, 09:35 PM
#20
ideas, thoughts?
-
Nov 12th, 2002, 11:35 PM
#21
Frenzied Member
How exactly can you tell when a program is done loading?
I'm assuming that it's different app everytime so you can't wait for the splash screen since not all will definetley have one.
-
Nov 13th, 2002, 12:08 AM
#22
Hyperactive Member
hmmm...see this thread, try the code I mentionned and tell me how it goes...
http://www.vbforums.com/showthread.p...47#post1258647
-
Nov 13th, 2002, 04:28 AM
#23
Thanks Vbud, But that too looks like it waits till the (opened) app closes...
Regards,
Bruce.
-
Nov 13th, 2002, 10:52 AM
#24
Lively Member
Hey Bruce,
I just had a thought. If you're Shelling these files out to a normal window, the new app should take system focus away from you program. You might simply try setting the MousePointer to an hourglass before the shell call, then use the Form_LostFocus event to return the MousePointer to default.
You'd need to test it out a fair bit, and probably put in some safety checks (like turning the hourglass off when a certain control is clicked or when a function key [F5?] is pressed or something). It might just do the trick though and you could forget worrying about all the API business.
Just a thought...
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 13th, 2002, 12:46 PM
#25
Originally posted by technitaes
I just had a thought. If you're Shelling these files out to a normal window, the new app should take system focus away from you program. You might simply try setting the MousePointer to an hourglass before the shell call, then use the Form_LostFocus event to return the MousePointer to default.
Pretty good idea, except that VB doesn't raise the Form_LostFocus event when another application gets focus
-
Nov 13th, 2002, 01:51 PM
#26
Lively Member
Originally posted by Joacim Andersson
Pretty good idea, except that VB doesn't raise the Form_LostFocus event when another application gets focus
Ahh Yes, Joacim Andersson. What a bummer... VB doesn't quite do what you'd expect it to all the time now does it!
So OK. Bruce, this has just about got me fed up! I hate it when I know something MUST be possible, but I just can't seem to solve it. So try this...
VB Code:
'In a modules General Declarations...
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'In the click/dblclick event you're writing...
Dim lhWnd As Long
lhWnd = GetActiveWindow() 'Should be the same as Me.hWnd
MousePointer = vbHourglass
ShellExecute Me.hWnd, vbNullString, sFileName, vbNullString, sDefDir, vbNormalFocus
Do While lhWnd = GetActiveWindow()
DoEvents
Loop
MousePointer = vbDefault
Since the first call to GetActiveWindow() actually returns the same value as Me.hWnd, you could forego the lhWnd variable and shorten the code to ...
VB Code:
'in the click/dblclick event...
MousePointer = vbHourglass
ShellExecute Me.hWnd, vbNullString, sFileName, vbNullString, sDefDir, vbNormalFocus
Do While Me.hWnd = GetActiveWindow()
DoEvents
Loop
MousePointer = vbDefault
Disclaimer, if the user selects another application while waiting on the shelled program to load then the Hourglass will return to default. Likewise if the program associated with the file has a splash screen, as soon as it appears (and thus becomes the active window) then the Hourglass will return to default.
I've actually tested this!
Hope it helps!
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 13th, 2002, 05:00 PM
#27
Legendary 
Thanks technitaes for ALL your efforts, much appreciated.
This is the winning combination - Inital testing going well..
VB Code:
Screen.MousePointer = vbHourglass
ShellExecute Me.hWnd, vbNullString, sFileName, vbNullString, sDefDir, vbNormalFocus
Do While Me.hWnd = GetActiveWindow()
DoEvents
Loop
Screen.MousePointer = vbDefault
Regards,
Bruce.
-
Nov 13th, 2002, 05:42 PM
#28
You could utilize the WaitForInputIdle() API, i.e.
VB Code:
Option Explicit
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Declare Function ShellExecuteEx Lib "shell32.dll" (lpExecInfo As SHELLEXECUTEINFO) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Sub ShellWait(ByVal sFile As String)
Dim tSEI As SHELLEXECUTEINFO
Dim lResult As Long
With tSEI
.fMask = SEE_MASK_NOCLOSEPROCESS
.lpFile = sFile
.lpVerb = "OPEN"
.nShow = 1
.cbSize = Len(tSEI)
End With
' Launch the File in with it's associated application (if applicable.)
Call ShellExecuteEx(tSEI)
' Wait for the application to Idle (Finish Loading.)
Do
lResult = WaitForInputIdle(tSEI.hProcess, 10)
DoEvents
Loop While lResult <> 0
End Sub
-
Nov 13th, 2002, 05:53 PM
#29
Thanks Aaron,
Looks like a few ideas are begining to surface now 
I was surprised that I had difficulty finding (via Search) similar posts.
Regards,
Bruce.
-
Nov 13th, 2002, 07:19 PM
#30
Feedback.
Not too sure if this is working as expected 
VB Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Const SW_SHOWNORMAL = 1
Private Sub Command0_Click()
Debug.Print Me.hwnd & vbTab & GetActiveWindow()
ShellExecute Me.hwnd, vbNullString, "D:\COSYS RMN.jpg", vbNullString, "C:\", vbMinimizedNoFocus 'SW_SHOWNORMAL
Debug.Print Me.hwnd & vbTab & GetActiveWindow()
Do While Me.hwnd = GetActiveWindow()
Debug.Print Me.hwnd & vbTab & GetActiveWindow()
DoEvents
Loop
End Sub
-
Nov 13th, 2002, 07:51 PM
#31
Aaron,
That example seems to work well.
technitaes: GetActiveWindow() never seems to be the same
as the Me.Hwnd? (Hence, bypassing the Do.. While)
Again, Thanks guys.
Bruce.
-
Nov 14th, 2002, 10:59 AM
#32
Lively Member
Bruce,
Sorry it didn't seem to work for you. I tried out both of the pieces of code I posted, and it worked every time (used a call to an .exe, .txt, .htm, and .gif all of which obviously use different programs to load). Not sure what the difference might have been.
Aaron,
Sweet! Glad someone was able to come up with a solution that would work for Bruce.
tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
To format your VB code in this forum use tags like this [vbcode] your code here[/vbcode]
-
Nov 14th, 2002, 07:08 PM
#33
technitaes,
In an app like Access, with sub forms etc, you need to use Application.hWndAccessApp instead of Me.Hwnd!
That was the problem ...
It now works well 
So, many thanks for your time.
It looks like we now have two methods to acheive this goal!
Thanks again,
Regards,
Bruce.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|