Getting handle on Handles?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil B Brubaker

    #1

    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.dl l" Alias "ShellExecu teA" _
    (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(&H1 00000, 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(&H1 00000, True, taskId)
    ' check for its signaled status, return to caller
    WaitForProcess = WaitForSingleOb ject(procHandle , msecs) <> -1
    ' close the handle
    CloseHandle procHandle
    End Function
    ====== end of code ====

  • Randy Birch

    #2
    Re: Getting handle on Handles?

    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
    GetDesktopWindo w() 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

    Please respond only to the newsgroups so all can benefit.


    "Phil B Brubaker" <philBru@Lanset .com> wrote in message
    news:3ds5ovscca of56eumhopkpin9 pl1t9mol6@4ax.c om...
    : 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.dl l" Alias "ShellExecu teA" _
    : (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(&H1 00000, 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(&H1 00000, True, taskId)
    : ' check for its signaled status, return to caller
    : WaitForProcess = WaitForSingleOb ject(procHandle , msecs) <> -1
    : ' close the handle
    : CloseHandle procHandle
    : End Function
    : ====== end of code ====
    :


    Comment

    Working...