Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Use ShellExecuteEx() for properties and shell (and wait)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Use ShellExecuteEx() for properties and shell (and wait)

    The following example shows how to show the file or folder properties dialog.
    The 2nd button is an alternative to the Shell statement aka Createprocess() API.
    In some cases it is better to use this code, for example when you want to deal with applications having elevated settings (having a manifest file with special level set).
    These settings may appear the application to require administration rights.
    Executing these kind of applications from an application not having these rights will not be allowed by Windows.
    ShellExecuteEx() is allowed to run such elevated applications from a non elevated application.
    To make sure the program is held during the execution the processid is used to wait until the 2nd app terminates.

    Code:
    #Include "win32api.inc"
    
    CallBack Function Form1_Proc()
    
        Local sFile     As String
        Local shInfo    As SHELLEXECUTEINFO
        Local sVerb     As String
    
        Select Case CbMsg
        Case %WM_Command
    
            Select Case CbCtl
            Case 101
    
                Select Case CbCtlMsg
                Case %BN_Clicked
    
                    Control Get Text CbHndl, 100 To sFile
    
                    sVerb = "properties"
                    shInfo.cbSize = SizeOf( SHELLEXECUTEINFO )
                    shInfo.fMask = %SEE_MASK_INVOKEIDLIST
                    shInfo.lpFile = StrPtr( sFile )
                    shInfo.hWnd = CbHndl
                    shInfo.nShow = %SW_Show
                    shInfo.lpVerb = StrPtr( sVerb )
                    Call ShellExecuteEx( shInfo )
    
                End Select
    
            Case 102
    
                Select Case CbCtlMsg
                Case %BN_Clicked
    
                    Control Get Text CbHndl, 100 To sFile
    
                    shInfo.cbSize = SizeOf( SHELLEXECUTEINFO )
                    shInfo.fMask = %SEE_MASK_NOCLOSEPROCESS
                    shInfo.lpFile = StrPtr( sFile )
                    shInfo.hWnd = CbHndl
                    shInfo.nShow = %SW_Show
                    shInfo.hInstApp = 0
                    'shInfo.lpParameters = StrPtr( sParams )
    
                    Call ShellExecuteEx( shInfo )
                    If shInfo.hProcess Then Call WaitForSingleObject( shInfo.hProcess, %INFINITE )
    
                End Select
    
            End Select
    
        End Select
    
    End Function
    
    Function WinMain( ByVal hInstance As Long, ByVal hPrevInstance As Long, ByVal lpszCmdLine As Asciiz Ptr, ByVal iCmdShow As Long ) As Long
    
        Local hDlg As Long
    
        Dialog New 0, "Form1",,, 300, 150, %WS_Visible Or %WS_Border Or %WS_ThickFrame Or %WS_SysMenu Or %WS_Caption Or %WS_DlgFrame Or %WS_MaximizeBox Or %WS_MinimizeBox, 0 To hDlg
    
        Control Add "Edit", hDlg, 100, "C:\windows\system32\calc.exe", 11, 10, 257, 15, %WS_Child Or %WS_TabStop Or %WS_Visible Or %ES_Left Or %ES_AutoHScroll, %WS_Ex_ClientEdge
        Control Add "Button", hDlg, 101, "Show properties", 11, 34, 67, 21, %WS_Child Or %WS_TabStop Or %WS_Visible Or %BS_Notify Or %BS_Center Or %BS_VCenter Or %BS_Multiline, 0
        Control Add "Button", hDlg, 102, "Run and wait", 11, 59, 67, 21, %WS_Child Or %WS_TabStop Or %WS_Visible Or %BS_Notify Or %BS_Center Or %BS_VCenter Or %BS_Multiline, 0
    
        Dialog Show Modal hDlg, Call Form1_Proc
    
    End Function
    hellobasic
Working...
X