May 04, 2024, 01:59:39 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


ShellExecute (EBasic SYSTEM command on steroids)

Started by Locodarwin, April 25, 2008, 12:02:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Locodarwin

April 25, 2008, 12:02:57 PM Last Edit: April 25, 2008, 12:11:33 PM by Locodarwin
Howdy!

Unhappy with the SYSTEM command in EBasic because of its limitations, I've quickly put together a subroutine replacement you can drop right into your programs.

Essentially SYSTEM is just a wrapper for WinAPI's ShellExecuteA() function, but with the parameter list greatly simplified.  Unfortunately that means if you want to do things like explicitly specify a default directory, or choose a different shell context (i.e. "edit" or "explore"), or make the resulting process invisible, you'd have to import the WinAPI function and know how to use it (or look it up on MSDN).  Well, I don't like having to go other places to look up parameters.  Like you, I have programming to do.

So with my little wrapper I've taken away most of that burden.  While my wrapper doesn't do anything special other than import the WinAPI command and call it, I do include a "Handy Help Header" which explains all you need to know in order to use it, an example that you can play around with, and much more functionality than the built-in SYSTEM command.

This is a decent candidate for a UDC (User Defined Command).  Check out the Feature Request section of this forum for more information about that proposal.


' Uncomment the following comment block to test the routine
/*
Dim iStat as UINT

Print "Attempting to ShellExecute..."

iStat = ShellExecute("cmd.exe", "/c ping 127.0.0.1", "", "open", 1)

Print "Result: ", iStat

PRINT:PRINT "Press any key to close"
DO:UNTIL INKEY$<>""
END
*/

'===============================================================================
'
' Description:      Attempts to shell execute (launch via the shell) an operation on a file or command.
' Syntax:           UINT iResult = ShellExecute(sFileOrCommand, sParam="", sWorkDir="", sType="Open", iShowFlag=5)
' Parameter(s):     sFileOrCommand - string, filename or command name to execute
' sParam - optional string, parameter for the file or command (default = "")
' sWorkDir - optional string, working directory for file or command (default = "")
' sType - optional string, the verb specifying the type of context operation to perform (default = "open")
' Some working examples are "open", "explore", "edit", "print", "properties", etc.
' iShowFlag - optional integer indicating how Windows should display the executed process (default = SW_SHOW = 5)
' Some possible values:
' 0 = SW_HIDE (the window/process is launched invisibly, and is activated)
' 2 = SW_SHOWMINIMIZED (minimized at launch)
' 3 = SW_MAXIMIZE (shown, maximized at launch, and activated)
' 4 = SW_SHOWNOACTIVATE (shown, but not activated)
' 5 = SW_SHOW (shown, activated, displayed in last used size & position)
' Requirement(s):   Emergence Basic (any version)
' Return Value(s):  On Success - Returns instance handle of launched process (a UINT > 32)
'                   On Failure - Returns a UINT value less than or equal to 32
' Some possible returned errors:
' 2 = SE_ERR_FNF (file not found)
' 3 = SE_ERR_PNF (path not found)
' 5 = SE_ERR_ACCESSDENIED (access denied)
' 8 = SE_ERR_OOM (out of memory)
' 11 = ERROR_BAD_FORMAT (invalid EXE file or error in EXE image)
' 26 = SE_ERR_SHARE (a sharing violation occured)
' 27 = SE_ERR_ASSOCINCOMPLETE (incomplete or invalid file association)
' 28 = SE_ERR_DDETIMEOUT (DDE timeout)
' 29 = SE_ERR_DDEFAIL (DDE transaction failed)
' 30 = SE_ERR_DDEBUSY (DDE busy)
' 31 = SE_ERR_NOASSOC (no association for file extension)
' 32 = SE_ERR_DLLNOTFOUND (DLL not found)
' Author(s):        SEO <locodarwin at yahoo dot com>
' Note(s):          This routine does not perform internal error checking and instead relies upon error checking provided
' by the ShellExecuteA() WinAPI function.
'
' Be careful when using a iShowFlag of 0, as the process you've launched will then be invisible and will
' have to be terminated through task manager (or programmatically) if it does not terminate itself.
'
' Also, the string value for sType represents verbs that are possible for that file, in context.  Using
' a verb that doesn't apply to the file or command launched will result in an error.  Windows will do its
' best to infer what you mean in all cases, based on file associations and file types.
'
'===============================================================================
Sub ShellExecute(sFileOrCommand as STRING, Opt sParam as STRING, Opt sWorkDir as STRING, Opt sType="Open" as STRING, Opt iShowFlag=5 as INT), UINT

DECLARE IMPORT, ShellExecuteA(hwnd as UINT, pOp as POINTER, pFile as POINTER, pParam as POINTER, pDir as POINTER, nShow as INT), UINT

UINT iRet
iRet = ShellExecuteA(0, sType, sFileOrCommand, sParam, sWorkDir, iShowFlag)
Return iRet
EndSub


Feel free to use it as you see fit.  Please include the Handy Help Header as a courtesy if you do.  :)

-S