By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Getting handle on Handles?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.