469,366 Members | 2,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Getting handle on Handles?

I'm trying to create my own shellexecute program named shell4OD and am
having problems with handles. Here is my code ...
=====
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

Public function Shell4OD( byVal cmd as string, byVal parameters as
string, byVal Dir as string, byVal showHide as integer) As Boolean
Dim procHandle As Long, shellHnd As Long, taskID As Long

' hmmm shellHnd = ???? <=== here is my problem

taskID = ShellExecute( shellHnd, _
vbNull, cmd, parameters, Dir, showHide)
' get the process handle
procHandle = OpenProcess(&H100000, True, taskID)
Shell4OD = WaitForProcess( procHandle)
' close the handle
CloseHandle procHandle
end function

' Wait for a number of milliseconds, and return the running status
' of a process. If argument is omitted, wait untill the process
' terminates.
Function WaitForProcess(taskId As Long, Optional msecs As Long = -1) _
As Boolean
Dim procHandle As Long
' get the process handle
procHandle = OpenProcess(&H100000, True, taskId)
' check for its signaled status, return to caller
WaitForProcess = WaitForSingleObject(procHandle, msecs) <> -1
' close the handle
CloseHandle procHandle
End Function
====== end of code ====

Jul 17 '05 #1
1 7012
The first parameter of ShellExecute should be the handle of the window to
which dialog boxes thrown by the call are owned. Normally, most people use
GetDesktopWindow() to make the desktop the owner of any dialogs. You can, if
you prefer, use the hwnd of your form .. it really doesn't matter to the
call. The only 'issue' is that if the desktop is the owner and an error
dialog is thrown on starting, your users could click your app which would
gain focus, possibly covering the error dialog. Making the form own the
dialogs causes them to be displayed modally to your application, preventing
inadvertent switching away.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Phil B Brubaker" <ph*****@Lanset.com> wrote in message
news:3d********************************@4ax.com...
: I'm trying to create my own shellexecute program named shell4OD and am
: having problems with handles. Here is my code ...
: =====
: 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
:
: Public function Shell4OD( byVal cmd as string, byVal parameters as
: string, byVal Dir as string, byVal showHide as integer) As Boolean
: Dim procHandle As Long, shellHnd As Long, taskID As Long
:
: ' hmmm shellHnd = ???? <=== here is my problem
:
: taskID = ShellExecute( shellHnd, _
: vbNull, cmd, parameters, Dir, showHide)
: ' get the process handle
: procHandle = OpenProcess(&H100000, True, taskID)
: Shell4OD = WaitForProcess( procHandle)
: ' close the handle
: CloseHandle procHandle
: end function
:
: ' Wait for a number of milliseconds, and return the running status
: ' of a process. If argument is omitted, wait untill the process
: ' terminates.
: Function WaitForProcess(taskId As Long, Optional msecs As Long = -1) _
: As Boolean
: Dim procHandle As Long
: ' get the process handle
: procHandle = OpenProcess(&H100000, True, taskId)
: ' check for its signaled status, return to caller
: WaitForProcess = WaitForSingleObject(procHandle, msecs) <> -1
: ' close the handle
: CloseHandle procHandle
: End Function
: ====== end of code ====
:
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Eric Walker | last post: by
6 posts views Thread by Jon Slaughter | last post: by
12 posts views Thread by Ken Brubaker | last post: by
2 posts views Thread by Jonathan Boivin | last post: by
33 posts views Thread by JamesB | last post: by
1 post views Thread by =?Utf-8?B?QU1lcmNlcg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.