Results 1 to 5 of 5

Thread: ShellExecuteEx help!

  1. #1

    Thread Starter
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52

    Unhappy ShellExecuteEx help!

    Hi,

    I have this 'slight' problem using ShellExecuteEx (after having tried all alternatives: CreateProcess, Shell(), ShellExecute)

    When I feed it with a filename for which an association exists, it's no problem. (.txt - Notepad starts and runs...)

    But when I feed it with an executable, I can barely see the outline of the window (so it has been started!), before Windows mysteriously decides to immediately close it. I end up with a pid of a non-existent process...

    Can someone please help? Would it be part of NT4 workstation restrictions?

    Here's the code:

    Code:
    Public Declare Function ShellExecuteEx Lib "shell32.dll" (ShellExecuteInfo As ShellExecuteInfoType) As Long
    
    Public Const SEE_MASK_INVOKEIDLIST = &HC
    Public Const SEE_MASK_NOCLOSEPROCESS = &H40
    Public Const SEE_MASK_FLAG_NO_UI = &H400
    
    Private Type ShellExecuteInfoType
        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
    
    Public Function StartApplication(ApplicationName As String) As Long
        Dim theShellExecuteInfo As ShellExecuteInfoType
        Dim ret As Long
        Dim hwnd As Long
        
        With theShellExecuteInfo
            .cbSize = Len(theShellExecuteInfo)
            .fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_INVOKEIDLIST Or _
                SEE_MASK_FLAG_NO_UI
            
            .hwnd = Me.hWnd
            .lpVerb = "open"
           
            .lpFile = ApplicationName & vbNullChar
            .lpParameters = vbNullChar
            .lpDirectory = vbNullChar
            .nShow = 0
            .hInstApp = 0
            .lpIDList = 0
        End With
        
        ret = ShellExecuteEx(theShellExecuteInfo)
        StartApplication = theShellExecuteInfo.hProcess
    End Function

  2. #2
    jim mcnamara
    Guest
    When that happens, it usually means one of the parameters, the default directory, or some other element you need is missing.

    Try running your command from a .BAT file, or from the DOS prompt. You may see the error return. Also check the exit status of the process. If it is not zero, then it ended with an error. You can get the status with GetExitCodeProcess(hProcess).

  3. #3

    Thread Starter
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    Thanks!

    I laced the code with debug statements and traced the problem. I also used one 'on error goto' too many..

    But even without that - No error codes whatsoever. Now I know I've always underestimated VB programming...

  4. #4

    Thread Starter
    Member hgroot's Avatar
    Join Date
    Dec 2001
    Location
    Amsterdam
    Posts
    52
    The problem in the piece of code I posted was that it did open a process, but didn't show it (missing .nShow constant!)

    But I have this new problem:
    ShellExecuteEx returns a process handle and I need the process identifier. I've searched for some API call to get the ID for a specific handle, but I can only find routines that do the oposite - convert ID into handle...

    Do you have any suggestions?

  5. #5
    Member
    Join Date
    Sep 2001
    Location
    Brazil
    Posts
    34
    If you need to get the process identifier, I think there´s only two ways: using the GetCurrentProcessId function (that returns the process identifier of the calling process) or using the CreateProcess function instead of ShellExecuteEx, cause a PROCESS_INFORMATION structure is filled in by this way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width