ShellExec (ShellExecuteEx and Exec replacement)

Source-code only - please, don't post questions here.
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

ShellExec (ShellExecuteEx and Exec replacement)

Postby RayBritton » May 22, 2007 10:20

Hello, this function can be used to open/print/find/explore files/folders, this is windows only. Please inform me of any bugs or suggestions. There are lots of options, most are optional.

Code: Select all

#include once "windows.bi"
#include once "win/shellapi.bi"
#include once "vbcompat.bi"

'ShellExec(ProgramPath, WorkingDir, Verb, Parameters, Show, Surpress, Fullscreen, ProgramName, NumOfPages, Height, Width, Depth)

'RayBritton
'No licence, use however you want, just don't claim you made it.

'Minimum is just ShellExec(ProgramPath)

'Required:
'ProgramPath    -   A string containing the path of the program (including the program file)

'Optional:
'WorkingDir     -   A string containing the working directory for the program (can be automatically worked out)
'Verb           -   Either "open" "edit" "print" "find" "explore" or ""  (note "" causes the default to be used, this is normally open but it does depend on the system
'Parameters     -   A string of the parameters to be passed to the program
'Show           -   0 - default   1 - maximised   2 - minimised   3 - restored
'Surpress       -   Integer, 1 stops the incorrect verb warnings  2 allows the verb no matter what <> causes verb and file checking
'fullscreen     -   If the app should take focus and refullscreen itself when the other program closes, this needs to be 1
'programname    -   If fullscreen =1 then you need to type in the window title
'num_of_pages   -   Number of pages for program (only needed if fullscreen = 1) (default=1)
'height         -   Height of desktop  (if fullscreen = 1)
'width          -   widht of desktop (if fullscreen = 1)
'depth          -   depth of desktop (if fullscreen = 1)

'To obtain height, width and depth run screeninfo BEFORE the screen or screenres statement

'Note: fullscreen can

'Return values
'-4 No programname supplied
'-2 Verb isn't recongised
'-1 Verb entered is incorrect for the file type
'2  File not found
'3  Path not found
'5  Access denied
'8  Not enough memory
'26 Sharing voliation
'27 Assication for that file type is incomplete/corrupt
'28 DDE timed out
'29 DDE failed
'30 DDE bust
'31 No assocation for that file type
'32 DLL not found

declare function ShellExec(byref _programpath as string, byref _workingdir as string, byref _verb as string, byref _
_parameters as string, byval _show as integer, byval _surpress as integer, byval _fullscreen as integer, byref _programname as string, byval _nop as integer, byval _height as integer, byval _width as integer, byval _depth as integer) as long

function ShellExec (byref _programpath as string, byref _workingdir as string, byref _verb as string, byref _parameters as string, byval _show as integer, byval _surpress as integer, byval _fullscreen as integer, byref _programname as string, byval _nop as integer, byval _height as integer, byval _width as integer, byval _depth as integer) as long
dim as long returned
dim as integer temp_int, i, last_slash,h,w,depth

dim as string work_dir
dim as hwnd ret, owner, focus
dim sei as ShellExecuteInfo


print _height,_width,_depth
sleep

sei.lpfile=strptr(_programpath)

if _surpress=1 then
    if _verb="open" or _verb="edit" or _verb="print" or _verb="find" or _verb="explore" then
        sei.lpverb=strptr(_verb)
    elseif _verb="" then
        sei.lpverb=@"open"
    else
        return -2
    end if
elseif _surpress=2 then
    sei.lpverb=strptr(_verb)
else
    if _verb="open" or _verb="edit" or _verb="print" or _verb="find" or _verb="explore" then
        if _verb="print" or _verb="find" or _verb="explore" then
            if right(_programpath,3)="exe" then
                return -1
            end if
        end if
        sei.lpverb=strptr(_verb)
    elseif _verb="" then
        sei.lpverb=@"open"
    else
        return -2
    end if
    sei.lpverb=strptr(_verb)
end if

if _workingdir="" then
    for temp_int = 1 to len(_programpath)
        if mid(_programpath,i,1) ="\" then last_slash=temp_int
    next
    work_dir = left(_workingdir,last_slash)
    sei.lpdirectory = strptr(work_dir)
else
    sei.lpdirectory = strptr(_workingdir)
end if

select case _show
case 0
    sei.nshow= SW_SHOWDEFAULT
case 1
    sei.nshow= SW_SHOWMAXIMIZED
case 2
    sei.nshow= SW_SHOWMINIMIZED
case 3
    sei.nshow= SW_RESTORE
end select

with sei
    .cbSize = Len(sei)
    .fMask = SEE_MASK_NOCLOSEPROCESS
    .hwnd = Null
    .lpParameters = strptr(_parameters)
end with   

if fileexists(_programpath)=0 then return 2



if returned=0 then
    if _fullscreen = 1 then
        if _programname<>"" then
            if _nop=0 then _nop=1
            ret=findwindow(vbNullString,strptr(_programname))
            owner = getwindow(ret,GW_HWNDFIRST)           
            returned=ShellExecuteEx(@sei)
            do
                focus=getforegroundwindow()
            loop until focus=owner
            screenres h,w,depth,_nop,&h01
            return returned
        else
            return -4
        end if
    else
        returned=ShellExecuteEx(@sei)
        return returned
    end if
else
    return returned
end if

end function
stevie1401
Posts: 134
Joined: Apr 19, 2007 16:20
Contact:

Postby stevie1401 » May 23, 2007 15:48

Can you give an example how to use this function?
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Postby RayBritton » May 23, 2007 16:24

sure:

Code: Select all

ShellExec("C:\program files\foo\foo.exe", "C:\program files\foo", "open", "", 1, 0, 1, "bar", 1, 800, 600, 8)

ShellExec("C:\program files\foo\foo.exe")
'that's the minimum that can done

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests